Skip to content

Commit d1d6441

Browse files
committed
Add high-level test for handlers
1 parent 5c8eb41 commit d1d6441

File tree

1 file changed

+279
-0
lines changed

1 file changed

+279
-0
lines changed
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
// Copyright (C) 2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Diagnostics;
8+
using System.Linq;
9+
using System.Text;
10+
using NUnit.Framework;
11+
using Xtensive.Core;
12+
using Xtensive.Orm.Providers;
13+
using Xtensive.Sql;
14+
using Xtensive.Orm.Tests.Storage.ConnectionHandlersModel;
15+
16+
namespace Xtensive.Orm.Tests.Storage.ConnectionHandlersModel
17+
{
18+
public class MyConnectionHandler : IConnectionHandler
19+
{
20+
private Guid instanceMarker;
21+
22+
public readonly Guid UniqueInstanceIdentifier;
23+
24+
public int ConnectionOpeningCounter;
25+
public int ConnectionInitializationCounter;
26+
public int ConnectionOpenedCounter;
27+
public int ConnectionOpeningFailedCounter;
28+
29+
public void ConnectionOpening(ConnectionEventData eventData)
30+
{
31+
instanceMarker = UniqueInstanceIdentifier;
32+
ConnectionOpeningCounter++;
33+
}
34+
35+
public void ConnectionInitialization(ConnectionInitEventData eventData)
36+
{
37+
ConnectionInitializationCounter++;
38+
if (instanceMarker != UniqueInstanceIdentifier) {
39+
throw new Exception("Not the same instance");
40+
}
41+
}
42+
43+
public void ConnectionOpened(ConnectionEventData eventData)
44+
{
45+
ConnectionOpenedCounter++;
46+
if (instanceMarker != UniqueInstanceIdentifier) {
47+
throw new Exception("Not the same instance");
48+
}
49+
}
50+
51+
public void ConnectionOpeningFailed(ConnectionErrorEventData eventData)
52+
{
53+
ConnectionOpeningFailedCounter++;
54+
if (instanceMarker != UniqueInstanceIdentifier) {
55+
throw new Exception("Not the same instance");
56+
}
57+
}
58+
59+
public MyConnectionHandler()
60+
{
61+
UniqueInstanceIdentifier = Guid.NewGuid();
62+
}
63+
}
64+
65+
public class NoDefaultConstructorHandler : IConnectionHandler
66+
{
67+
#pragma warning disable IDE0060 // Remove unused parameter
68+
public NoDefaultConstructorHandler(int dummyParameter)
69+
#pragma warning restore IDE0060 // Remove unused parameter
70+
{
71+
}
72+
}
73+
74+
public class NonPublicDefaultConstructorHandler : IConnectionHandler
75+
{
76+
private NonPublicDefaultConstructorHandler()
77+
{
78+
}
79+
}
80+
81+
#region Performance Test handlers
82+
83+
public class PerfHandler1 : IConnectionHandler { }
84+
public class PerfHandler2 : IConnectionHandler { }
85+
public class PerfHandler3 : IConnectionHandler { }
86+
public class PerfHandler4 : IConnectionHandler { }
87+
public class PerfHandler5 : IConnectionHandler { }
88+
public class PerfHandler6 : IConnectionHandler { }
89+
public class PerfHandler7 : IConnectionHandler { }
90+
public class PerfHandler8 : IConnectionHandler { }
91+
public class PerfHandler9 : IConnectionHandler { }
92+
public class PerfHandler10 : IConnectionHandler { }
93+
public class PerfHandler11 : IConnectionHandler { }
94+
public class PerfHandler12 : IConnectionHandler { }
95+
public class PerfHandler13 : IConnectionHandler { }
96+
public class PerfHandler14 : IConnectionHandler { }
97+
public class PerfHandler15 : IConnectionHandler { }
98+
public class PerfHandler16 : IConnectionHandler { }
99+
public class PerfHandler17 : IConnectionHandler { }
100+
public class PerfHandler18 : IConnectionHandler { }
101+
public class PerfHandler19 : IConnectionHandler { }
102+
public class PerfHandler20 : IConnectionHandler { }
103+
public class PerfHandler21 : IConnectionHandler { }
104+
public class PerfHandler22 : IConnectionHandler { }
105+
public class PerfHandler23 : IConnectionHandler { }
106+
public class PerfHandler24 : IConnectionHandler { }
107+
public class PerfHandler25 : IConnectionHandler { }
108+
109+
#endregion
110+
111+
public static class StaticCounter
112+
{
113+
public static int OpeningReached;
114+
public static int OpenedReached;
115+
}
116+
117+
public class DummyEntity : Entity
118+
{
119+
[Field, Key]
120+
public int Id { get; private set; }
121+
122+
[Field]
123+
public int Value { get; set; }
124+
125+
public DummyEntity(Session session)
126+
: base(session)
127+
{
128+
}
129+
}
130+
}
131+
132+
namespace Xtensive.Orm.Tests.Storage
133+
{
134+
public class ConnectionHandlerTest
135+
{
136+
[Test]
137+
public void DomainRegistryTest()
138+
{
139+
var domainConfig = DomainConfigurationFactory.Create();
140+
domainConfig.Types.Register(typeof(DummyEntity));
141+
domainConfig.Types.Register(typeof(MyConnectionHandler));
142+
143+
Assert.That(domainConfig.Types.ConnectionHandlers.Count(), Is.EqualTo(1));
144+
}
145+
146+
[Test]
147+
public void NoDefaultConstructorTest()
148+
{
149+
var domainConfig = DomainConfigurationFactory.Create();
150+
domainConfig.UpgradeMode = DomainUpgradeMode.Recreate;
151+
domainConfig.Types.Register(typeof(DummyEntity));
152+
domainConfig.Types.Register(typeof(NoDefaultConstructorHandler));
153+
154+
Domain domain = null;
155+
_ = Assert.Throws<NotSupportedException>(() => domain = Domain.Build(domainConfig));
156+
domain.DisposeSafely();
157+
}
158+
159+
[Test]
160+
public void NonPublicDefaultConstructorTest()
161+
{
162+
var domainConfig = DomainConfigurationFactory.Create();
163+
domainConfig.UpgradeMode = DomainUpgradeMode.Recreate;
164+
domainConfig.Types.Register(typeof(DummyEntity));
165+
domainConfig.Types.Register(typeof(NonPublicDefaultConstructorHandler));
166+
167+
using var domain = Domain.Build(domainConfig);
168+
}
169+
170+
[Test]
171+
public void SessionConnectionHandlersTest()
172+
{
173+
var domainConfig = DomainConfigurationFactory.Create();
174+
domainConfig.UpgradeMode = DomainUpgradeMode.Recreate;
175+
domainConfig.Types.Register(typeof(DummyEntity));
176+
domainConfig.Types.Register(typeof(MyConnectionHandler));
177+
178+
Guid? first = null;
179+
using (var domain = Domain.Build(domainConfig))
180+
using (var session = domain.OpenSession()) {
181+
var nativeHandler = (SqlSessionHandler) session.Handler;
182+
var extension = nativeHandler.Connection.Extensions.Get<ConnectionHandlersExtension>();
183+
var handlerInstance = (MyConnectionHandler)extension.Handlers.First();
184+
Assert.That(handlerInstance.ConnectionOpeningCounter, Is.Not.EqualTo(0));
185+
Assert.That(handlerInstance.ConnectionOpenedCounter, Is.Not.EqualTo(0));
186+
first = handlerInstance.UniqueInstanceIdentifier;
187+
}
188+
189+
Guid? second = null;
190+
using (var domain = Domain.Build(domainConfig))
191+
using (var session = domain.OpenSession()) {
192+
var nativeHandler = (SqlSessionHandler) session.Handler;
193+
var extension = nativeHandler.Connection.Extensions.Get<ConnectionHandlersExtension>();
194+
var handlerInstance = (MyConnectionHandler) extension.Handlers.First();
195+
Assert.That(handlerInstance.ConnectionOpeningCounter, Is.Not.EqualTo(0));
196+
Assert.That(handlerInstance.ConnectionOpenedCounter, Is.Not.EqualTo(0));
197+
second = handlerInstance.UniqueInstanceIdentifier;
198+
}
199+
200+
Assert.That(first != null && second != null && first != second, Is.True);
201+
}
202+
203+
[Test]
204+
[TestCase(0)]
205+
[TestCase(1)]
206+
[TestCase(2)]
207+
public void ConnectionExtensionExistanceTest(int includeHandlersCount)
208+
{
209+
var domainConfig = DomainConfigurationFactory.Create();
210+
domainConfig.UpgradeMode = DomainUpgradeMode.Recreate;
211+
212+
foreach (var handler in GetHandlers(includeHandlersCount)) {
213+
domainConfig.Types.Register(handler);
214+
}
215+
216+
using (var domain = Domain.Build(domainConfig))
217+
using (var session = domain.OpenSession()) {
218+
var nativeHandler = (SqlSessionHandler) session.Handler;
219+
var extensions = nativeHandler.Connection.Extensions;
220+
if (includeHandlersCount > 0) {
221+
Assert.That(extensions.Count, Is.EqualTo(1));
222+
var extension = extensions.Get<ConnectionHandlersExtension>();
223+
Assert.That(extension, Is.Not.Null);
224+
Assert.That(extension.Handlers.Count, Is.EqualTo(includeHandlersCount));
225+
}
226+
else {
227+
Assert.That(extensions.Count, Is.EqualTo(0));
228+
}
229+
}
230+
}
231+
232+
[Explicit]
233+
[TestCase(0)]
234+
[TestCase(5)]
235+
[TestCase(10)]
236+
[TestCase(15)]
237+
[TestCase(20)]
238+
[TestCase(25)]
239+
public void SessionOpeningPerformanceTest(int includeHandlersCount)
240+
{
241+
var domainConfig = DomainConfigurationFactory.Create();
242+
domainConfig.UpgradeMode = DomainUpgradeMode.Recreate;
243+
244+
foreach (var handler in GetHandlers(includeHandlersCount)) {
245+
domainConfig.Types.Register(handler);
246+
}
247+
248+
var watch = new Stopwatch();
249+
using (var domain = Domain.Build(domainConfig)) {
250+
watch.Start();
251+
for (var i = 0; i < 1000000; i++) {
252+
domain.OpenSession().Dispose();
253+
}
254+
watch.Stop();
255+
}
256+
Console.WriteLine(watch.ElapsedTicks / 1000000);
257+
}
258+
259+
private IEnumerable<Type> GetHandlers(int neededCount)
260+
{
261+
if (neededCount > 25) {
262+
throw new Exception();
263+
}
264+
265+
var all = new Type[] {
266+
typeof(PerfHandler1), typeof(PerfHandler2), typeof(PerfHandler3), typeof(PerfHandler4),
267+
typeof(PerfHandler5), typeof(PerfHandler6), typeof(PerfHandler7), typeof(PerfHandler8),
268+
typeof(PerfHandler9), typeof(PerfHandler10), typeof(PerfHandler11), typeof(PerfHandler12),
269+
typeof(PerfHandler13), typeof(PerfHandler14), typeof(PerfHandler15), typeof(PerfHandler16),
270+
typeof(PerfHandler17), typeof(PerfHandler18), typeof(PerfHandler19), typeof(PerfHandler20),
271+
typeof(PerfHandler21), typeof(PerfHandler22), typeof(PerfHandler23), typeof(PerfHandler24),
272+
typeof(PerfHandler25)
273+
};
274+
for (var i = 0; i < neededCount; i++) {
275+
yield return all[i];
276+
}
277+
}
278+
}
279+
}

0 commit comments

Comments
 (0)