@@ -260,6 +260,76 @@ describe('ChannelConnection', () => {
260
260
expect ( protocol . reset ) . toHaveBeenCalled ( )
261
261
expect ( protocol . resetFailure ) . toHaveBeenCalled ( )
262
262
} )
263
+
264
+ it ( 'should not call protocol.reset() when there is an ongoing reset' , ( ) => {
265
+ const channel = {
266
+ _open : true
267
+ }
268
+
269
+ const protocol = {
270
+ reset : jest . fn ( ) ,
271
+ resetFailure : jest . fn ( ) ,
272
+ isLastMessageReset : jest . fn ( ( ) => true )
273
+ }
274
+ const protocolSupplier = ( ) => protocol
275
+ const connection = spyOnConnectionChannel ( { channel, protocolSupplier } )
276
+
277
+ connection . _resetOnFailure ( )
278
+
279
+ expect ( protocol . reset ) . toHaveBeenCalledTimes ( 1 )
280
+ expect ( protocol . resetFailure ) . not . toHaveBeenCalled ( )
281
+
282
+ connection . _resetOnFailure ( )
283
+
284
+ expect ( protocol . reset ) . toHaveBeenCalledTimes ( 1 )
285
+ expect ( protocol . resetFailure ) . not . toHaveBeenCalled ( )
286
+ } )
287
+
288
+ it ( 'should call protocol.reset() when after a previous reset completed' , ( ) => {
289
+ const channel = {
290
+ _open : true
291
+ }
292
+
293
+ const protocol = {
294
+ reset : jest . fn ( observer => observer . onComplete ( ) ) ,
295
+ resetFailure : jest . fn ( )
296
+ }
297
+ const protocolSupplier = ( ) => protocol
298
+ const connection = spyOnConnectionChannel ( { channel, protocolSupplier } )
299
+
300
+ connection . _resetOnFailure ( )
301
+
302
+ expect ( protocol . reset ) . toHaveBeenCalledTimes ( 1 )
303
+ expect ( protocol . resetFailure ) . toHaveBeenCalledTimes ( 1 )
304
+
305
+ connection . _resetOnFailure ( )
306
+
307
+ expect ( protocol . reset ) . toHaveBeenCalledTimes ( 2 )
308
+ expect ( protocol . resetFailure ) . toHaveBeenCalledTimes ( 2 )
309
+ } )
310
+
311
+ it ( 'should call protocol.reset() when after a previous reset fail' , ( ) => {
312
+ const channel = {
313
+ _open : true
314
+ }
315
+
316
+ const protocol = {
317
+ reset : jest . fn ( observer => observer . onError ( new Error ( 'some error' ) ) ) ,
318
+ resetFailure : jest . fn ( )
319
+ }
320
+ const protocolSupplier = ( ) => protocol
321
+ const connection = spyOnConnectionChannel ( { channel, protocolSupplier } )
322
+
323
+ connection . _resetOnFailure ( )
324
+
325
+ expect ( protocol . reset ) . toHaveBeenCalledTimes ( 1 )
326
+ expect ( protocol . resetFailure ) . toHaveBeenCalledTimes ( 1 )
327
+
328
+ connection . _resetOnFailure ( )
329
+
330
+ expect ( protocol . reset ) . toHaveBeenCalledTimes ( 2 )
331
+ expect ( protocol . resetFailure ) . toHaveBeenCalledTimes ( 2 )
332
+ } )
263
333
} )
264
334
265
335
describe ( 'when connection is not open' , ( ) => {
@@ -340,6 +410,110 @@ describe('ChannelConnection', () => {
340
410
} )
341
411
} )
342
412
413
+ describe ( '.resetAndFlush()' , ( ) => {
414
+ it ( 'should call protocol.reset() onComplete' , async ( ) => {
415
+ const channel = {
416
+ _open : true
417
+ }
418
+
419
+ const protocol = {
420
+ reset : jest . fn ( observer => observer . onComplete ( ) ) ,
421
+ resetFailure : jest . fn ( )
422
+ }
423
+ const protocolSupplier = ( ) => protocol
424
+ const connection = spyOnConnectionChannel ( { channel, protocolSupplier } )
425
+
426
+ await connection . resetAndFlush ( ) . catch ( ( ) => { } )
427
+
428
+ expect ( protocol . reset ) . toHaveBeenCalled ( )
429
+ } )
430
+
431
+ it ( 'should call protocol.reset() onError' , async ( ) => {
432
+ const channel = {
433
+ _open : true
434
+ }
435
+
436
+ const protocol = {
437
+ reset : jest . fn ( observer => observer . onError ( ) ) ,
438
+ resetFailure : jest . fn ( )
439
+ }
440
+ const protocolSupplier = ( ) => protocol
441
+ const connection = spyOnConnectionChannel ( { channel, protocolSupplier } )
442
+
443
+ await connection . resetAndFlush ( ) . catch ( ( ) => { } )
444
+
445
+ expect ( protocol . reset ) . toHaveBeenCalled ( )
446
+ } )
447
+
448
+ it ( 'should not call protocol.reset() when there is an ongoing reset' , async ( ) => {
449
+ const channel = {
450
+ _open : true
451
+ }
452
+
453
+ const protocol = {
454
+ reset : jest . fn ( observer => {
455
+ setTimeout ( ( ) => observer . onComplete ( ) , 100 )
456
+ } ) ,
457
+ resetFailure : jest . fn ( ) ,
458
+ isLastMessageReset : jest . fn ( ( ) => true )
459
+ }
460
+ const protocolSupplier = ( ) => protocol
461
+ const connection = spyOnConnectionChannel ( { channel, protocolSupplier } )
462
+
463
+ const completeFirstResetAndFlush = connection . resetAndFlush ( )
464
+
465
+ expect ( protocol . reset ) . toHaveBeenCalledTimes ( 1 )
466
+
467
+ await connection . resetAndFlush ( )
468
+
469
+ expect ( protocol . reset ) . toHaveBeenCalledTimes ( 1 )
470
+
471
+ await completeFirstResetAndFlush
472
+ } )
473
+
474
+ it ( 'should call protocol.reset() when after a previous reset completed' , async ( ) => {
475
+ const channel = {
476
+ _open : true
477
+ }
478
+
479
+ const protocol = {
480
+ reset : jest . fn ( observer => observer . onComplete ( ) ) ,
481
+ resetFailure : jest . fn ( )
482
+ }
483
+ const protocolSupplier = ( ) => protocol
484
+ const connection = spyOnConnectionChannel ( { channel, protocolSupplier } )
485
+
486
+ await connection . resetAndFlush ( )
487
+
488
+ expect ( protocol . reset ) . toHaveBeenCalledTimes ( 1 )
489
+
490
+ await connection . resetAndFlush ( )
491
+
492
+ expect ( protocol . reset ) . toHaveBeenCalledTimes ( 2 )
493
+ } )
494
+
495
+ it ( 'should call protocol.reset() when after a previous reset fail' , async ( ) => {
496
+ const channel = {
497
+ _open : true
498
+ }
499
+
500
+ const protocol = {
501
+ reset : jest . fn ( observer => observer . onError ( new Error ( 'some error' ) ) ) ,
502
+ resetFailure : jest . fn ( )
503
+ }
504
+ const protocolSupplier = ( ) => protocol
505
+ const connection = spyOnConnectionChannel ( { channel, protocolSupplier } )
506
+
507
+ await connection . resetAndFlush ( ) . catch ( ( ) => { } )
508
+
509
+ expect ( protocol . reset ) . toHaveBeenCalledTimes ( 1 )
510
+
511
+ await connection . resetAndFlush ( ) . catch ( ( ) => { } )
512
+
513
+ expect ( protocol . reset ) . toHaveBeenCalledTimes ( 2 )
514
+ } )
515
+ } )
516
+
343
517
function spyOnConnectionChannel ( {
344
518
channel,
345
519
errorHandler,
0 commit comments