forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncLocalSessionContextFixture.cs
66 lines (57 loc) · 1.63 KB
/
AsyncLocalSessionContextFixture.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
using System.Threading.Tasks;
using NHibernate.Cfg;
using NHibernate.Context;
using NUnit.Framework;
namespace NHibernate.Test.ConnectionTest
{
[TestFixture]
public class AsyncLocalSessionContextFixture : ConnectionManagementTestCase
{
protected override ISession GetSessionUnderTest()
{
var session = OpenSession();
session.BeginTransaction();
return session;
}
protected override void Configure(Configuration configuration)
{
base.Configure(cfg);
cfg.SetProperty(Environment.CurrentSessionContextClass, "async_local");
}
[Test]
public async Task AsyncLocalIsolation()
{
using (var session = OpenSession())
{
CurrentSessionContext.Bind(session);
AssertCurrentSession(session, "Unexpected session after outer bind.");
await SubBind(session);
AssertCurrentSession(session, "Unexpected session after end of SubBind call.");
}
}
private async Task SubBind(ISession firstSession)
{
AssertCurrentSession(firstSession, "Unexpected session at start of SubBind.");
using (var session = OpenSession())
{
CurrentSessionContext.Bind(session);
AssertCurrentSession(session, "Unexpected session after inner bind.");
await Dummy();
AssertCurrentSession(session, "Unexpected session after dummy await.");
}
}
private Task Dummy()
{
return Task.FromResult(0);
}
private void AssertCurrentSession(ISession session, string message)
{
Assert.That(
Sfi.GetCurrentSession(),
Is.EqualTo(session),
"{0} {1} instead of {2}.", message,
Sfi.GetCurrentSession().GetSessionImplementation().SessionId,
session.GetSessionImplementation().SessionId);
}
}
}