1
1
package cache
2
2
3
3
import (
4
+ "github.com/stretchr/testify/assert"
4
5
"log"
5
6
"os"
6
7
"testing"
@@ -15,104 +16,169 @@ const asyncTestDir = "./async-test-data"
15
16
func TestAsyncCache_Cleanup_Of_Expired_Transactions (t * testing.T ) {
16
17
graceTime := 100 * time .Millisecond
17
18
asyncCache := newAsyncTestCache (t , graceTime )
19
+ defer func () {
20
+ asyncCache .Close ()
21
+ os .RemoveAll (asyncTestDir )
22
+ }()
18
23
19
24
key := & Key {
20
25
Query : []byte ("SELECT async cache" ),
21
26
}
22
-
23
- if done := asyncCache .IsDone (key ); ! done {
27
+ status , err := asyncCache .Status (key )
28
+ assert .NoError (t , err )
29
+ if ! status .IsAbsent () {
24
30
t .Fatalf ("unexpected behaviour: transaction isnt done while it wasnt even started" )
25
31
}
26
32
27
- if err := asyncCache .Register (key ); err != nil {
33
+ if err := asyncCache .Create (key ); err != nil {
28
34
t .Fatalf ("unexpected error: %s failed to register transaction" , err )
29
35
}
30
36
31
- if done := asyncCache .IsDone (key ); done {
37
+ status , err = asyncCache .Status (key )
38
+ assert .NoError (t , err )
39
+ if ! status .IsPending () {
32
40
t .Fatalf ("unexpected behaviour: transaction isnt finished" )
33
41
}
34
42
35
43
time .Sleep (graceTime * 2 )
36
44
37
- if done := asyncCache .IsDone (key ); ! done {
45
+ status , err = asyncCache .Status (key )
46
+ assert .NoError (t , err )
47
+ if status .IsPending () {
38
48
t .Fatalf ("unexpected behaviour: transaction grace time elapsed and yet it was still pending" )
39
49
}
40
-
41
- asyncCache .Close ()
42
- os .RemoveAll (asyncTestDir )
43
50
}
44
51
45
52
func TestAsyncCache_AwaitForConcurrentTransaction_GraceTimeWithoutTransactionCompletion (t * testing.T ) {
46
- graceTime := 300 * time .Millisecond
53
+ graceTime := 100 * time .Millisecond
47
54
asyncCache := newAsyncTestCache (t , graceTime )
48
55
56
+ defer func () {
57
+ asyncCache .Close ()
58
+ os .RemoveAll (asyncTestDir )
59
+ }()
60
+
49
61
key := & Key {
50
62
Query : []byte ("SELECT async cache AwaitForConcurrentTransaction" ),
51
63
}
52
64
53
- if done := asyncCache .IsDone (key ); ! done {
65
+ status , err := asyncCache .Status (key )
66
+ assert .NoError (t , err )
67
+ if ! status .IsAbsent () {
54
68
t .Fatalf ("unexpected behaviour: transaction isnt done while it wasnt even started" )
55
69
}
56
70
57
- if err := asyncCache .Register (key ); err != nil {
71
+ if err := asyncCache .Create (key ); err != nil {
58
72
t .Fatalf ("unexpected error: %s failed to register transaction" , err )
59
73
}
60
74
61
- if done := asyncCache .IsDone (key ); done {
75
+ status , err = asyncCache .Status (key )
76
+ assert .NoError (t , err )
77
+ if ! status .IsPending () {
62
78
t .Fatalf ("unexpected behaviour: transaction isnt finished" )
63
79
}
64
80
65
81
startTime := time .Now ()
66
- done := asyncCache .AwaitForConcurrentTransaction (key )
82
+ _ , err = asyncCache .AwaitForConcurrentTransaction (key )
83
+ assert .NoError (t , err )
67
84
elapsedTime := time .Since (startTime )
68
85
69
86
// in order to let the cleaner swipe the transaction
70
- time .Sleep (100 * time .Millisecond )
71
- if done == asyncCache .IsDone (key ) && done {
87
+ time .Sleep (150 * time .Millisecond )
88
+ status , err = asyncCache .Status (key )
89
+ assert .NoError (t , err )
90
+ if ! status .IsAbsent () {
72
91
t .Fatalf ("unexpected behaviour: transaction awaiting time elapsed %s" , elapsedTime .String ())
73
92
}
74
-
75
- asyncCache .Close ()
76
- os .RemoveAll (asyncTestDir )
77
93
}
78
94
79
95
func TestAsyncCache_AwaitForConcurrentTransaction_TransactionCompletedWhileAwaiting (t * testing.T ) {
80
96
graceTime := 300 * time .Millisecond
81
97
asyncCache := newAsyncTestCache (t , graceTime )
82
98
99
+ defer func () {
100
+ asyncCache .Close ()
101
+ os .RemoveAll (asyncTestDir )
102
+ }()
83
103
key := & Key {
84
104
Query : []byte ("SELECT async cache AwaitForConcurrentTransactionCompleted" ),
85
105
}
86
106
87
- if err := asyncCache .Register (key ); err != nil {
107
+ if err := asyncCache .Create (key ); err != nil {
88
108
t .Fatalf ("unexpected error: %s failed to register transaction" , err )
89
109
}
90
110
91
111
errs := make (chan error )
92
112
go func () {
93
113
time .Sleep (graceTime / 2 )
94
- if err := asyncCache .Unregister (key ); err != nil {
114
+ if err := asyncCache .Complete (key ); err != nil {
95
115
errs <- err
96
116
} else {
97
117
errs <- nil
98
118
}
99
119
}()
100
120
101
121
startTime := time .Now ()
102
- done := asyncCache .AwaitForConcurrentTransaction (key )
122
+ transactionState , err := asyncCache .AwaitForConcurrentTransaction (key )
123
+ if err != nil {
124
+ t .Fatalf ("unexpected error: %s failed to unregister transaction" , err )
125
+ }
126
+
103
127
elapsedTime := time .Since (startTime )
104
128
105
- err : = <- errs
129
+ err = <- errs
106
130
if err != nil {
107
131
t .Fatalf ("unexpected error: %s failed to unregister transaction" , err )
108
132
}
109
133
110
- if done != asyncCache . IsDone ( key ) || ! done || elapsedTime >= graceTime {
134
+ if ! transactionState . IsCompleted () || elapsedTime >= graceTime {
111
135
t .Fatalf ("unexpected behaviour: transaction awaiting time elapsed %s" , elapsedTime .String ())
112
136
}
137
+ }
138
+
139
+ func TestAsyncCache_AwaitForConcurrentTransaction_TransactionFailedWhileAwaiting (t * testing.T ) {
140
+ graceTime := 300 * time .Millisecond
141
+ asyncCache := newAsyncTestCache (t , graceTime )
142
+
143
+ defer func () {
144
+ asyncCache .Close ()
145
+ os .RemoveAll (asyncTestDir )
146
+ }()
147
+
148
+ key := & Key {
149
+ Query : []byte ("SELECT async cache AwaitForConcurrentTransactionCompleted" ),
150
+ }
151
+
152
+ if err := asyncCache .Create (key ); err != nil {
153
+ t .Fatalf ("unexpected error: %s failed to register transaction" , err )
154
+ }
155
+
156
+ errs := make (chan error )
157
+ go func () {
158
+ time .Sleep (graceTime / 2 )
159
+ if err := asyncCache .Fail (key ); err != nil {
160
+ errs <- err
161
+ } else {
162
+ errs <- nil
163
+ }
164
+ }()
165
+
166
+ startTime := time .Now ()
167
+ transactionState , err := asyncCache .AwaitForConcurrentTransaction (key )
168
+ if err != nil {
169
+ t .Fatalf ("unexpected error: %s failed to unregister transaction" , err )
170
+ }
171
+
172
+ elapsedTime := time .Since (startTime )
173
+
174
+ err = <- errs
175
+ if err != nil {
176
+ t .Fatalf ("unexpected error: %s failed to unregister transaction" , err )
177
+ }
113
178
114
- asyncCache .Close ()
115
- os .RemoveAll (asyncTestDir )
179
+ if ! transactionState .IsFailed () || elapsedTime >= graceTime {
180
+ t .Fatalf ("unexpected behaviour: transaction awaiting time elapsed %s" , elapsedTime .String ())
181
+ }
116
182
}
117
183
118
184
func newAsyncTestCache (t * testing.T , graceTime time.Duration ) * AsyncCache {
@@ -152,7 +218,7 @@ func TestAsyncCache_FilesystemCache_instantiation(t *testing.T) {
152
218
if err := os .RemoveAll (testDirAsync ); err != nil {
153
219
log .Fatalf ("cannot remove %q: %s" , testDirAsync , err )
154
220
}
155
- _ , err := NewAsyncCache (fileSystemCfg )
221
+ _ , err := NewAsyncCache (fileSystemCfg , 1 * time . Second )
156
222
if err != nil {
157
223
t .Fatalf ("could not instanciate filsystem async cache because of the following error: %s" , err )
158
224
}
@@ -168,7 +234,7 @@ func TestAsyncCache_FilesystemCache_wrong_instantiation(t *testing.T) {
168
234
},
169
235
Expire : config .Duration (time .Minute ),
170
236
}
171
- _ , err := NewAsyncCache (fileSystemCfg )
237
+ _ , err := NewAsyncCache (fileSystemCfg , 1 * time . Second )
172
238
if err == nil {
173
239
t .Fatalf ("the instanciate of filsystem async cache should have crashed" )
174
240
}
@@ -185,7 +251,7 @@ func TestAsyncCache_RedisCache_instantiation(t *testing.T) {
185
251
Expire : config .Duration (cacheTTL ),
186
252
}
187
253
188
- _ , err := NewAsyncCache (redisCfg )
254
+ _ , err := NewAsyncCache (redisCfg , 1 * time . Second )
189
255
if err != nil {
190
256
t .Fatalf ("could not instanciate redis async cache because of the following error: %s" , err )
191
257
}
@@ -201,21 +267,21 @@ func TestAsyncCache_RedisCache_wrong_instantiation(t *testing.T) {
201
267
},
202
268
}
203
269
204
- _ , err := NewAsyncCache (redisCfg )
270
+ _ , err := NewAsyncCache (redisCfg , 1 * time . Second )
205
271
if err == nil {
206
272
t .Fatalf ("the redis instanciation should have crashed" )
207
273
}
208
274
}
209
275
210
- func TestAsyncCache_Unkown_instantiation (t * testing.T ) {
276
+ func TestAsyncCache_Unknown_instantiation (t * testing.T ) {
211
277
var redisCfg = config.Cache {
212
278
Name : "test" ,
213
279
Mode : "Unkown Mode" ,
214
280
Redis : config.RedisCacheConfig {},
215
281
Expire : config .Duration (cacheTTL ),
216
282
}
217
283
218
- _ , err := NewAsyncCache (redisCfg )
284
+ _ , err := NewAsyncCache (redisCfg , 1 * time . Second )
219
285
if err == nil {
220
286
t .Fatalf ("The instanciation should have crash" )
221
287
}
0 commit comments