11
11
using System . Collections ;
12
12
using System . Collections . Generic ;
13
13
using System . Linq ;
14
- using System . Text ;
15
- using System . Threading . Tasks ;
16
14
using NHibernate . Cache ;
17
15
using NHibernate . Cfg ;
18
16
using NHibernate . Impl ;
19
17
using NHibernate . Test . SecondLevelCacheTests ;
20
18
using NSubstitute ;
21
19
using NUnit . Framework ;
22
- using Environment = System . Environment ;
23
20
24
21
namespace NHibernate . Test . SecondLevelCacheTest
25
22
{
23
+ using System . Threading . Tasks ;
26
24
using System . Threading ;
27
25
[ TestFixture ]
28
26
public class InvalidationTestsAsync : TestCase
@@ -34,26 +32,40 @@ public class InvalidationTestsAsync : TestCase
34
32
protected override void Configure ( Configuration configuration )
35
33
{
36
34
base . Configure ( configuration ) ;
37
- configuration . Properties [ Cfg . Environment . CacheProvider ] = typeof ( HashtableCacheProvider ) . AssemblyQualifiedName ;
38
- configuration . Properties [ Cfg . Environment . UseQueryCache ] = "true" ;
35
+ configuration . Properties [ Environment . CacheProvider ] = typeof ( HashtableCacheProvider ) . AssemblyQualifiedName ;
36
+ configuration . Properties [ Environment . UseQueryCache ] = "true" ;
39
37
}
40
38
41
39
[ Test ]
42
40
public async Task InvalidatesEntitiesAsync ( )
43
41
{
44
42
var cache = Substitute . For < UpdateTimestampsCache > ( Sfi . Settings , new Dictionary < string , string > ( ) ) ;
45
- ( ( SessionFactoryImpl ) ( Sfi as DebugSessionFactory ) . ActualFactory ) . SetPropertyUsingReflection ( x => x . UpdateTimestampsCache , cache ) ;
43
+ ( ( SessionFactoryImpl ) ( Sfi as DebugSessionFactory ) . ActualFactory ) . SetPropertyUsingReflection (
44
+ x => x . UpdateTimestampsCache ,
45
+ cache ) ;
46
+
47
+ //"Received" assertions can not be used since the collection is reused and cleared between calls.
48
+ //The received args are cloned and stored
49
+ var preInvalidations = new List < IReadOnlyCollection < string > > ( ) ;
50
+ var invalidations = new List < IReadOnlyCollection < string > > ( ) ;
51
+
52
+ cache
53
+ . When ( x=> x . PreInvalidate ( Arg . Any < IReadOnlyCollection < string > > ( ) ) )
54
+ . Do ( x=> preInvalidations . Add ( ( ( IReadOnlyCollection < string > ) x [ 0 ] ) . ToList ( ) ) ) ;
55
+ cache
56
+ . When ( x => x . Invalidate ( Arg . Any < IReadOnlyCollection < string > > ( ) ) )
57
+ . Do ( x => invalidations . Add ( ( ( IReadOnlyCollection < string > ) x [ 0 ] ) . ToList ( ) ) ) ;
46
58
47
- var items = new List < Item > ( ) ;
48
59
using ( ISession session = OpenSession ( ) )
49
60
{
50
61
using ( ITransaction tx = session . BeginTransaction ( ) )
51
62
{
52
63
foreach ( var i in Enumerable . Range ( 1 , 10 ) )
53
64
{
54
- var item = new Item { Id = i } ;
65
+ var item = new Item { Id = i } ;
55
66
await ( session . SaveAsync ( item ) ) ;
56
67
}
68
+
57
69
await ( tx . CommitAsync ( ) ) ;
58
70
}
59
71
@@ -64,6 +76,7 @@ public async Task InvalidatesEntitiesAsync()
64
76
var item = await ( session . GetAsync < Item > ( i ) ) ;
65
77
item . Name = item . Id . ToString ( ) ;
66
78
}
79
+
67
80
await ( tx . CommitAsync ( ) ) ;
68
81
}
69
82
@@ -74,15 +87,25 @@ public async Task InvalidatesEntitiesAsync()
74
87
var item = await ( session . GetAsync < Item > ( i ) ) ;
75
88
await ( session . DeleteAsync ( item ) ) ;
76
89
}
90
+
77
91
await ( tx . CommitAsync ( ) ) ;
78
92
}
79
93
}
94
+
80
95
//Should receive one preinvalidation and one invalidation per commit
81
- await ( cache . Received ( 3 ) . PreInvalidateAsync ( Arg . Is < object [ ] > ( x => x . Length == 1 && ( string ) x [ 0 ] == "Item" ) , CancellationToken . None ) ) ;
82
- await ( cache . Received ( 3 ) . InvalidateAsync ( Arg . Is < object [ ] > ( x => x . Length == 1 && ( string ) x [ 0 ] == "Item" ) , CancellationToken . None ) ) ;
96
+ Assert . That ( preInvalidations . Count , Is . EqualTo ( 3 ) ) ;
97
+ Assert . That ( preInvalidations . All ( x => x . Count == 1 && x . First ( ) == "Item" ) , Is . True ) ;
98
+
99
+ Assert . That ( invalidations . Count , Is . EqualTo ( 3 ) ) ;
100
+ Assert . That ( invalidations . All ( x => x . Count == 1 && x . First ( ) == "Item" ) , Is . True ) ;
83
101
84
102
}
85
103
104
+ private bool IsRight ( HashSet < string > x )
105
+ {
106
+ return x . Count == 1 && x . First ( ) == "Item" ;
107
+ }
108
+
86
109
public async Task CleanUpAsync ( CancellationToken cancellationToken = default ( CancellationToken ) )
87
110
{
88
111
using ( ISession s = OpenSession ( ) )
0 commit comments