@@ -4,14 +4,14 @@ var expect = require('expect.js'),
4
4
revGuardStore = require ( '../../lib/revisionGuardStore' ) ;
5
5
6
6
describe ( 'revisionGuard' , function ( ) {
7
-
7
+
8
8
var store ;
9
-
9
+
10
10
before ( function ( done ) {
11
11
revGuardStore . create ( function ( err , s ) {
12
12
store = s ;
13
13
done ( ) ;
14
- } )
14
+ } ) ;
15
15
} ) ;
16
16
17
17
describe ( 'creating a new guard' , function ( ) {
@@ -122,9 +122,9 @@ describe('revisionGuard', function () {
122
122
} ) ;
123
123
124
124
} ) ;
125
-
125
+
126
126
describe ( 'guarding an event' , function ( ) {
127
-
127
+
128
128
var guard ;
129
129
130
130
var evt1 = {
@@ -162,7 +162,7 @@ describe('revisionGuard', function () {
162
162
} ,
163
163
revision : 3
164
164
} ;
165
-
165
+
166
166
before ( function ( ) {
167
167
guard = new RevisionGuard ( store , { queueTimeout : 200 } ) ;
168
168
guard . defineEvent ( {
@@ -178,36 +178,36 @@ describe('revisionGuard', function () {
178
178
meta : 'meta'
179
179
} ) ;
180
180
} ) ;
181
-
181
+
182
182
beforeEach ( function ( done ) {
183
183
guard . currentHandlingRevisions = { } ;
184
184
store . clear ( done ) ;
185
185
} ) ;
186
-
186
+
187
187
describe ( 'in correct order' , function ( ) {
188
-
188
+
189
189
it ( 'it should work as expected' , function ( done ) {
190
-
190
+
191
191
var guarded = 0 ;
192
-
192
+
193
193
function check ( ) {
194
194
guarded ++ ;
195
-
195
+
196
196
if ( guarded === 3 ) {
197
197
done ( ) ;
198
198
}
199
199
}
200
-
200
+
201
201
guard . guard ( evt1 , function ( err , finish ) {
202
202
expect ( err ) . not . to . be . ok ( ) ;
203
-
203
+
204
204
finish ( function ( err ) {
205
205
expect ( err ) . not . to . be . ok ( ) ;
206
206
expect ( guarded ) . to . eql ( 0 ) ;
207
207
check ( ) ;
208
208
} ) ;
209
209
} ) ;
210
-
210
+
211
211
setTimeout ( function ( ) {
212
212
guard . guard ( evt2 , function ( err , finish ) {
213
213
expect ( err ) . not . to . be . ok ( ) ;
@@ -231,9 +231,100 @@ describe('revisionGuard', function () {
231
231
} ) ;
232
232
} ) ;
233
233
} , 20 ) ;
234
-
234
+
235
235
} ) ;
236
-
236
+
237
+ describe ( 'but with slow beginning events' , function ( ) {
238
+
239
+ var specialGuard ;
240
+
241
+ before ( function ( ) {
242
+ specialGuard = new RevisionGuard ( store , { queueTimeout : 2000 , queueTimeoutMaxLoops : 15 } ) ;
243
+ specialGuard . defineEvent ( {
244
+ correlationId : 'correlationId' ,
245
+ id : 'id' ,
246
+ payload : 'payload' ,
247
+ name : 'name' ,
248
+ aggregateId : 'aggregate.id' ,
249
+ aggregate : 'aggregate.name' ,
250
+ context : 'context.name' ,
251
+ revision : 'revision' ,
252
+ version : 'version' ,
253
+ meta : 'meta'
254
+ } ) ;
255
+ } ) ;
256
+
257
+ beforeEach ( function ( done ) {
258
+ specialGuard . currentHandlingRevisions = { } ;
259
+ store . clear ( done ) ;
260
+ } ) ;
261
+
262
+ it ( 'it should work as expected' , function ( done ) {
263
+
264
+ var guarded = 0 ;
265
+
266
+ function check ( ) {
267
+ guarded ++ ;
268
+
269
+ if ( guarded === 3 ) {
270
+ done ( ) ;
271
+ }
272
+ }
273
+
274
+ var start1 = Date . now ( ) ;
275
+ specialGuard . guard ( evt1 , function ( err , finish1 ) {
276
+ var diff1 = Date . now ( ) - start1 ;
277
+ console . log ( 'guarded 1: ' + diff1 ) ;
278
+ expect ( err ) . not . to . be . ok ( ) ;
279
+
280
+ setTimeout ( function ( ) {
281
+ start1 = Date . now ( ) ;
282
+ finish1 ( function ( err ) {
283
+ diff1 = Date . now ( ) - start1 ;
284
+ console . log ( 'finished 1: ' + diff1 ) ;
285
+ expect ( err ) . not . to . be . ok ( ) ;
286
+ expect ( guarded ) . to . eql ( 0 ) ;
287
+ check ( ) ;
288
+ } ) ;
289
+ } , 250 ) ;
290
+ } ) ;
291
+
292
+ var start2 = Date . now ( ) ;
293
+ specialGuard . guard ( evt2 , function ( err , finish2 ) {
294
+ var diff2 = Date . now ( ) - start2 ;
295
+ console . log ( 'guarded 2: ' + diff2 ) ;
296
+ expect ( err ) . not . to . be . ok ( ) ;
297
+
298
+ start2 = Date . now ( ) ;
299
+ finish2 ( function ( err ) {
300
+ diff2 = Date . now ( ) - start2 ;
301
+ console . log ( 'finished 2: ' + diff2 ) ;
302
+ expect ( err ) . not . to . be . ok ( ) ;
303
+ expect ( guarded ) . to . eql ( 1 ) ;
304
+ check ( ) ;
305
+ } ) ;
306
+ } ) ;
307
+
308
+ var start3 = Date . now ( ) ;
309
+ specialGuard . guard ( evt3 , function ( err , finish3 ) {
310
+ var diff3 = Date . now ( ) - start3 ;
311
+ console . log ( 'guarded 3: ' + diff3 ) ;
312
+ expect ( err ) . not . to . be . ok ( ) ;
313
+
314
+ start3 = Date . now ( ) ;
315
+ finish3 ( function ( err ) {
316
+ diff3 = Date . now ( ) - start3 ;
317
+ console . log ( 'finished 3: ' + diff3 ) ;
318
+ expect ( err ) . not . to . be . ok ( ) ;
319
+ expect ( guarded ) . to . eql ( 2 ) ;
320
+ check ( ) ;
321
+ } ) ;
322
+ } ) ;
323
+
324
+ } ) ;
325
+
326
+ } ) ;
327
+
237
328
} ) ;
238
329
239
330
describe ( 'in wrong order' , function ( ) {
@@ -293,7 +384,7 @@ describe('revisionGuard', function () {
293
384
expect ( err ) . to . be . ok ( ) ;
294
385
expect ( err . name ) . to . eql ( 'AlreadyHandledError' ) ;
295
386
expect ( guarded ) . to . eql ( 3 ) ;
296
-
387
+
297
388
guard . guard ( evt3 , function ( err ) {
298
389
expect ( err ) . to . be . ok ( ) ;
299
390
expect ( err . name ) . to . eql ( 'AlreadyHandledError' ) ;
@@ -362,7 +453,7 @@ describe('revisionGuard', function () {
362
453
} ) ;
363
454
364
455
} ) ;
365
-
456
+
366
457
} ) ;
367
458
368
459
} ) ;
0 commit comments