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 ;
12
+ using System . Collections . Generic ;
13
+ using System . Threading ;
14
+ using NHibernate . Cache ;
15
+ using NHibernate . Cache . Access ;
16
+ using NUnit . Framework ;
17
+
18
+ namespace NHibernate . Test . CacheTest
19
+ {
20
+ using System . Threading . Tasks ;
21
+ [ TestFixture ]
22
+ public class CacheFixtureAsync
23
+ {
24
+ [ Test ]
25
+ public Task TestSimpleCacheAsync ( )
26
+ {
27
+ return DoTestCacheAsync ( new HashtableCacheProvider ( ) ) ;
28
+ }
29
+
30
+ private CacheKey CreateCacheKey ( string text )
31
+ {
32
+ return new CacheKey ( text , NHibernateUtil . String , "Foo" , null ) ;
33
+ }
34
+
35
+ public async Task DoTestCacheAsync ( ICacheProvider cacheProvider , CancellationToken cancellationToken = default ( CancellationToken ) )
36
+ {
37
+ ICache cache = cacheProvider . BuildCache ( typeof ( String ) . FullName , new Dictionary < string , string > ( ) ) ;
38
+
39
+ long longBefore = Timestamper . Next ( ) ;
40
+
41
+ await ( Task . Delay ( 15 , cancellationToken ) ) ;
42
+
43
+ long before = Timestamper . Next ( ) ;
44
+
45
+ await ( Task . Delay ( 15 , cancellationToken ) ) ;
46
+
47
+ ICacheConcurrencyStrategy ccs = new ReadWriteCache ( ) ;
48
+ ccs . Cache = cache ;
49
+
50
+ // cache something
51
+ CacheKey fooKey = CreateCacheKey ( "foo" ) ;
52
+
53
+ Assert . IsTrue ( await ( ccs . PutAsync ( fooKey , "foo" , before , null , null , false , cancellationToken ) ) ) ;
54
+
55
+ await ( Task . Delay ( 15 , cancellationToken ) ) ;
56
+
57
+ long after = Timestamper . Next ( ) ;
58
+
59
+ Assert . IsNull ( await ( ccs . GetAsync ( fooKey , longBefore , cancellationToken ) ) ) ;
60
+ Assert . AreEqual ( "foo" , await ( ccs . GetAsync ( fooKey , after , cancellationToken ) ) ) ;
61
+ Assert . IsFalse ( await ( ccs . PutAsync ( fooKey , "foo" , before , null , null , false , cancellationToken ) ) ) ;
62
+
63
+ // update it;
64
+
65
+ ISoftLock fooLock = await ( ccs . LockAsync ( fooKey , null , cancellationToken ) ) ;
66
+
67
+ Assert . IsNull ( await ( ccs . GetAsync ( fooKey , after , cancellationToken ) ) ) ;
68
+ Assert . IsNull ( await ( ccs . GetAsync ( fooKey , longBefore , cancellationToken ) ) ) ;
69
+ Assert . IsFalse ( await ( ccs . PutAsync ( fooKey , "foo" , before , null , null , false , cancellationToken ) ) ) ;
70
+
71
+ await ( Task . Delay ( 15 , cancellationToken ) ) ;
72
+
73
+ long whileLocked = Timestamper . Next ( ) ;
74
+
75
+ Assert . IsFalse ( await ( ccs . PutAsync ( fooKey , "foo" , whileLocked , null , null , false , cancellationToken ) ) ) ;
76
+
77
+ await ( Task . Delay ( 15 , cancellationToken ) ) ;
78
+
79
+ await ( ccs . ReleaseAsync ( fooKey , fooLock , cancellationToken ) ) ;
80
+
81
+ Assert . IsNull ( await ( ccs . GetAsync ( fooKey , after , cancellationToken ) ) ) ;
82
+ Assert . IsNull ( await ( ccs . GetAsync ( fooKey , longBefore , cancellationToken ) ) ) ;
83
+ Assert . IsFalse ( await ( ccs . PutAsync ( fooKey , "bar" , whileLocked , null , null , false , cancellationToken ) ) ) ;
84
+ Assert . IsFalse ( await ( ccs . PutAsync ( fooKey , "bar" , after , null , null , false , cancellationToken ) ) ) ;
85
+
86
+ await ( Task . Delay ( 15 , cancellationToken ) ) ;
87
+
88
+ long longAfter = Timestamper . Next ( ) ;
89
+
90
+ Assert . IsTrue ( await ( ccs . PutAsync ( fooKey , "baz" , longAfter , null , null , false , cancellationToken ) ) ) ;
91
+ Assert . IsNull ( await ( ccs . GetAsync ( fooKey , after , cancellationToken ) ) ) ;
92
+ Assert . IsNull ( await ( ccs . GetAsync ( fooKey , whileLocked , cancellationToken ) ) ) ;
93
+
94
+ await ( Task . Delay ( 15 , cancellationToken ) ) ;
95
+
96
+ long longLongAfter = Timestamper . Next ( ) ;
97
+
98
+ Assert . AreEqual ( "baz" , await ( ccs . GetAsync ( fooKey , longLongAfter , cancellationToken ) ) ) ;
99
+
100
+ // update it again, with multiple locks
101
+
102
+ ISoftLock fooLock1 = await ( ccs . LockAsync ( fooKey , null , cancellationToken ) ) ;
103
+ ISoftLock fooLock2 = await ( ccs . LockAsync ( fooKey , null , cancellationToken ) ) ;
104
+
105
+ Assert . IsNull ( await ( ccs . GetAsync ( fooKey , longLongAfter , cancellationToken ) ) ) ;
106
+
107
+ await ( Task . Delay ( 15 , cancellationToken ) ) ;
108
+
109
+ whileLocked = Timestamper . Next ( ) ;
110
+
111
+ Assert . IsFalse ( await ( ccs . PutAsync ( fooKey , "foo" , whileLocked , null , null , false , cancellationToken ) ) ) ;
112
+
113
+ await ( Task . Delay ( 15 , cancellationToken ) ) ;
114
+
115
+ await ( ccs . ReleaseAsync ( fooKey , fooLock2 , cancellationToken ) ) ;
116
+
117
+ await ( Task . Delay ( 15 , cancellationToken ) ) ;
118
+
119
+ long betweenReleases = Timestamper . Next ( ) ;
120
+
121
+ Assert . IsFalse ( await ( ccs . PutAsync ( fooKey , "bar" , betweenReleases , null , null , false , cancellationToken ) ) ) ;
122
+ Assert . IsNull ( await ( ccs . GetAsync ( fooKey , betweenReleases , cancellationToken ) ) ) ;
123
+
124
+ await ( Task . Delay ( 15 , cancellationToken ) ) ;
125
+
126
+ await ( ccs . ReleaseAsync ( fooKey , fooLock1 , cancellationToken ) ) ;
127
+
128
+ Assert . IsFalse ( await ( ccs . PutAsync ( fooKey , "bar" , whileLocked , null , null , false , cancellationToken ) ) ) ;
129
+
130
+ await ( Task . Delay ( 15 , cancellationToken ) ) ;
131
+
132
+ longAfter = Timestamper . Next ( ) ;
133
+
134
+ Assert . IsTrue ( await ( ccs . PutAsync ( fooKey , "baz" , longAfter , null , null , false , cancellationToken ) ) ) ;
135
+ Assert . IsNull ( await ( ccs . GetAsync ( fooKey , whileLocked , cancellationToken ) ) ) ;
136
+
137
+ await ( Task . Delay ( 15 , cancellationToken ) ) ;
138
+
139
+ longLongAfter = Timestamper . Next ( ) ;
140
+
141
+ Assert . AreEqual ( "baz" , await ( ccs . GetAsync ( fooKey , longLongAfter , cancellationToken ) ) ) ;
142
+ }
143
+
144
+ private async Task DoTestMinValueTimestampOnStrategyAsync ( ICache cache , ICacheConcurrencyStrategy strategy , CancellationToken cancellationToken = default ( CancellationToken ) )
145
+ {
146
+ CacheKey key = CreateCacheKey ( "key" ) ;
147
+ strategy . Cache = cache ;
148
+ await ( strategy . PutAsync ( key , "value" , long . MinValue , 0 , null , false , cancellationToken ) ) ;
149
+
150
+ Assert . IsNull ( await ( strategy . GetAsync ( key , long . MinValue , cancellationToken ) ) , "{0} strategy fails the test" , strategy . GetType ( ) ) ;
151
+ Assert . IsNull ( await ( strategy . GetAsync ( key , long . MaxValue , cancellationToken ) ) , "{0} strategy fails the test" , strategy . GetType ( ) ) ;
152
+ }
153
+
154
+ [ Test ]
155
+ public async Task MinValueTimestampAsync ( )
156
+ {
157
+ ICache cache = new HashtableCacheProvider ( ) . BuildCache ( "region" , new Dictionary < string , string > ( ) ) ;
158
+ ICacheConcurrencyStrategy strategy = new ReadWriteCache ( ) ;
159
+ strategy . Cache = cache ;
160
+
161
+ await ( DoTestMinValueTimestampOnStrategyAsync ( cache , new ReadWriteCache ( ) ) ) ;
162
+ await ( DoTestMinValueTimestampOnStrategyAsync ( cache , new NonstrictReadWriteCache ( ) ) ) ;
163
+ await ( DoTestMinValueTimestampOnStrategyAsync ( cache , new ReadOnlyCache ( ) ) ) ;
164
+ }
165
+ }
166
+ }
0 commit comments