Skip to content

Commit a5f6053

Browse files
Generate async files
1 parent 1405039 commit a5f6053

File tree

4 files changed

+191
-6
lines changed

4 files changed

+191
-6
lines changed

src/NHibernate.Test/Async/CacheTest/CacheFixture.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace NHibernate.Test.CacheTest
2222
public class CacheFixtureAsync: TestCase
2323
{
2424
[Test]
25-
public async Task TestSimpleCacheAsync()
25+
public async Task TestSimpleReadWriteCacheAsync()
2626
{
2727
await (DoTestCacheAsync(new HashtableCacheProvider()));
2828
}
@@ -57,7 +57,8 @@ protected CacheKey CreateCacheKey(string text)
5757

5858
Assert.IsNull(await (ccs.GetAsync(fooKey, longBefore, cancellationToken)));
5959
Assert.AreEqual("foo", await (ccs.GetAsync(fooKey, after, cancellationToken)));
60-
Assert.IsFalse(await (ccs.PutAsync(fooKey, "foo", before, null, null, false, cancellationToken)));
60+
Assert.IsFalse(await (ccs.PutAsync(fooKey, "foo", before, null, null, true, cancellationToken)));
61+
Assert.IsTrue(await (ccs.PutAsync(fooKey, "foo", before, null, null, false, cancellationToken)));
6162

6263
// update it;
6364

src/NHibernate.Test/Async/NHSpecificTest/GH2552/Fixture.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected override void Configure(NHCfg.Configuration configuration)
2727
{
2828
configuration.SetProperty(NHCfg.Environment.UseSecondLevelCache, "true");
2929
configuration.SetProperty(NHCfg.Environment.GenerateStatistics, "true");
30+
configuration.SetProperty(NHCfg.Environment.UseMinimalPuts, "true");
3031
}
3132

3233
protected override void OnTearDown()
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Linq;
12+
using NHibernate.Cache;
13+
using NHibernate.Cfg;
14+
using NHibernate.Cfg.MappingSchema;
15+
using NHibernate.Linq;
16+
using NHibernate.Mapping.ByCode;
17+
using NUnit.Framework;
18+
19+
namespace NHibernate.Test.NHSpecificTest.GH3176
20+
{
21+
using System.Threading.Tasks;
22+
[TestFixture(CacheFactory.ReadOnly, false)]
23+
[TestFixture(CacheFactory.NonstrictReadWrite, false)]
24+
[TestFixture(CacheFactory.ReadWrite, false)]
25+
[TestFixture(CacheFactory.ReadWrite, true)]
26+
public class ByCodeFixtureAsync : TestCaseMappingByCode
27+
{
28+
private readonly bool _versioned;
29+
30+
public ByCodeFixtureAsync(string cacheStrategy, bool versioned)
31+
{
32+
_versioned = versioned;
33+
CacheConcurrencyStrategy = cacheStrategy;
34+
}
35+
36+
protected override string CacheConcurrencyStrategy { get; }
37+
38+
private int _id;
39+
40+
protected override HbmMapping GetMappings()
41+
{
42+
var mapper = new ModelMapper();
43+
mapper.Class<Entity>(
44+
rc =>
45+
{
46+
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
47+
rc.Property(x => x.Name);
48+
rc.Component(
49+
x => x.Component,
50+
m =>
51+
{
52+
m.Property(x => x.Field);
53+
m.Lazy(true);
54+
});
55+
if (_versioned)
56+
rc.Version(x => x.Version, m => { });
57+
});
58+
59+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
60+
}
61+
62+
protected override void Configure(Configuration configuration)
63+
{
64+
base.Configure(configuration);
65+
configuration.Properties[Environment.CacheProvider] = typeof(HashtableCacheProvider).AssemblyQualifiedName;
66+
configuration.Properties[Environment.UseSecondLevelCache] = "true";
67+
configuration.Properties[Environment.GenerateStatistics] = "true";
68+
}
69+
70+
protected override void OnSetUp()
71+
{
72+
using (var session = OpenSession())
73+
using (var transaction = session.BeginTransaction())
74+
{
75+
var e1 = new Entity { Name = "Bob", Component = new Component() { Field = "Jim" } };
76+
session.Save(e1);
77+
_id = e1.Id;
78+
79+
var e2 = new Entity { Name = "Sally" };
80+
session.Save(e2);
81+
82+
transaction.Commit();
83+
}
84+
}
85+
86+
protected override void OnTearDown()
87+
{
88+
using (var session = OpenSession())
89+
using (var transaction = session.BeginTransaction())
90+
{
91+
session.CreateQuery("delete from Entity").ExecuteUpdate();
92+
transaction.Commit();
93+
}
94+
}
95+
96+
[Test]
97+
public async Task TestPreLoadedDataAsync()
98+
{
99+
using (var session = OpenSession())
100+
using (var transaction = session.BeginTransaction())
101+
{
102+
// Load the entities into the second-level cache.
103+
await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).ToListAsync());
104+
105+
var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync());
106+
107+
Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True);
108+
109+
var field = result.Component?.Field;
110+
111+
Assert.That(field, Is.EqualTo("Jim"));
112+
}
113+
114+
using (var session = OpenSession())
115+
using (var transaction = session.BeginTransaction())
116+
{
117+
var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync());
118+
119+
Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True);
120+
121+
var field = result.Component?.Field;
122+
123+
Assert.That(field, Is.EqualTo("Jim"));
124+
}
125+
}
126+
127+
[Test]
128+
public async Task InitializedLazyPropertyShouldBeCachedAsync()
129+
{
130+
using (var session = OpenSession())
131+
using (var transaction = session.BeginTransaction())
132+
{
133+
var e = await (session.GetAsync<Entity>(_id));
134+
Assert.That(e.Component?.Field, Is.EqualTo("Jim"));
135+
136+
Assert.That(NHibernateUtil.IsPropertyInitialized(e, "Component"), Is.True);
137+
await (transaction.CommitAsync());
138+
}
139+
140+
using (var session = OpenSession())
141+
using (var transaction = session.BeginTransaction())
142+
{
143+
var e = await (session.GetAsync<Entity>(_id));
144+
145+
Assert.That(NHibernateUtil.IsPropertyInitialized(e, "Component"), Is.True, "Lazy property is not cached");
146+
var field = e.Component?.Field;
147+
148+
Assert.That(field, Is.EqualTo("Jim"));
149+
await (transaction.CommitAsync());
150+
}
151+
}
152+
153+
[Test]
154+
public async Task TestNonPreLoadedDataAsync()
155+
{
156+
using (var session = OpenSession())
157+
using (var transaction = session.BeginTransaction())
158+
{
159+
var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync());
160+
161+
Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True);
162+
163+
var field = result.Component?.Field;
164+
165+
Assert.That(field, Is.EqualTo("Jim"));
166+
}
167+
168+
using (var session = OpenSession())
169+
using (var transaction = session.BeginTransaction())
170+
{
171+
var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync());
172+
173+
Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True);
174+
175+
var field = result.Component?.Field;
176+
177+
Assert.That(field, Is.EqualTo("Jim"));
178+
}
179+
}
180+
}
181+
}

src/NHibernate/Async/Cache/ReadWriteCache.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//------------------------------------------------------------------------------
99

1010

11+
using System;
1112
using System.Collections;
1213
using System.Collections.Generic;
1314
using System.Linq;
@@ -18,6 +19,7 @@ namespace NHibernate.Cache
1819
{
1920
using System.Threading.Tasks;
2021
using System.Threading;
22+
2123
public partial class ReadWriteCache : IBatchableCacheConcurrencyStrategy
2224
{
2325

@@ -264,7 +266,7 @@ private Task DecrementLockAsync(object key, CacheLock @lock, CancellationToken c
264266
@lock.Unlock(Cache.NextTimestamp());
265267
return Cache.PutAsync(key, @lock, cancellationToken);
266268
}
267-
catch (System.Exception ex)
269+
catch (Exception ex)
268270
{
269271
return Task.FromException<object>(ex);
270272
}
@@ -315,7 +317,7 @@ internal Task HandleLockExpiryAsync(object key, CancellationToken cancellationTo
315317
@lock.Unlock(ts);
316318
return Cache.PutAsync(key, @lock, cancellationToken);
317319
}
318-
catch (System.Exception ex)
320+
catch (Exception ex)
319321
{
320322
return Task.FromException<object>(ex);
321323
}
@@ -436,7 +438,7 @@ public Task EvictAsync(CacheKey key, CancellationToken cancellationToken)
436438
Evict(key);
437439
return Task.CompletedTask;
438440
}
439-
catch (System.Exception ex)
441+
catch (Exception ex)
440442
{
441443
return Task.FromException<object>(ex);
442444
}
@@ -452,7 +454,7 @@ public Task<bool> UpdateAsync(CacheKey key, object value, object currentVersion,
452454
{
453455
return Task.FromResult<bool>(Update(key, value, currentVersion, previousVersion));
454456
}
455-
catch (System.Exception ex)
457+
catch (Exception ex)
456458
{
457459
return Task.FromException<bool>(ex);
458460
}

0 commit comments

Comments
 (0)