forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapBasedSessionContextFixture.cs
79 lines (68 loc) · 2.01 KB
/
MapBasedSessionContextFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Collections;
using System.Threading;
using NHibernate.Cfg;
using NHibernate.Context;
using NHibernate.Engine;
using NUnit.Framework;
namespace NHibernate.Test.ConnectionTest
{
[TestFixture]
public class MapBasedSessionContextFixture : ConnectionManagementTestCase
{
protected override ISession GetSessionUnderTest()
{
ISession session = OpenSession();
session.BeginTransaction();
return session;
}
protected override void Configure(Configuration configuration)
{
base.Configure(cfg);
cfg.SetProperty(Environment.CurrentSessionContextClass, typeof(TestableMapBasedSessionContext).AssemblyQualifiedName);
}
protected override void OnSetUp()
{
TestableMapBasedSessionContext._map = null;
}
[Test]
public void MapContextThreadSafety()
{
using (var factory1 = cfg.BuildSessionFactory())
using (var session1 = factory1.OpenSession())
using (var factory2 = cfg.BuildSessionFactory())
using (var session2 = factory2.OpenSession())
{
var thread1 = new Thread(() =>
{
CurrentSessionContext.Bind(session1);
});
var thread2 = new Thread(() =>
{
CurrentSessionContext.Bind(session2);
});
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
Assert.IsTrue(CurrentSessionContext.HasBind(factory1), $"No session bound to \"{nameof(factory1)}\" factory.");
Assert.IsTrue(CurrentSessionContext.HasBind(factory2), $"No session bound to \"{nameof(factory2)}\" factory.");
}
}
}
public class TestableMapBasedSessionContext : MapBasedSessionContext
{
public TestableMapBasedSessionContext(ISessionFactoryImplementor factory) : base(factory) { }
// Context is the app with such implementation. Just for the test case.
internal static IDictionary _map;
protected override IDictionary GetMap()
{
return _map;
}
protected override void SetMap(IDictionary value)
{
// Give a fair chance to have a concurrency bug if base implementation is not thread safe.
Thread.Sleep(100);
_map = value;
}
}
}