@@ -26,7 +26,6 @@ const connectionOptionsDefaults = {
26
26
27
27
/** The absolute minimum socket API needed by Connection as of writing this test */
28
28
class FakeSocket extends EventEmitter {
29
- writableEnded : boolean ;
30
29
address ( ) {
31
30
// is never called
32
31
}
@@ -35,14 +34,6 @@ class FakeSocket extends EventEmitter {
35
34
}
36
35
destroy ( ) {
37
36
// is called, has no side effects
38
- this . writableEnded = true ;
39
- }
40
- end ( cb ) {
41
- this . writableEnded = true ;
42
- // nextTick to simulate I/O delay
43
- if ( typeof cb === 'function' ) {
44
- process . nextTick ( cb ) ;
45
- }
46
37
}
47
38
get remoteAddress ( ) {
48
39
return 'iLoveJavaScript' ;
@@ -52,20 +43,6 @@ class FakeSocket extends EventEmitter {
52
43
}
53
44
}
54
45
55
- class InputStream extends Readable {
56
- writableEnded : boolean ;
57
- constructor ( options ?) {
58
- super ( options ) ;
59
- }
60
-
61
- end ( cb ) {
62
- this . writableEnded = true ;
63
- if ( typeof cb === 'function' ) {
64
- process . nextTick ( cb ) ;
65
- }
66
- }
67
- }
68
-
69
46
describe ( 'new Connection()' , function ( ) {
70
47
let server ;
71
48
after ( ( ) => mock . cleanup ( ) ) ;
@@ -124,7 +101,7 @@ describe('new Connection()', function () {
124
101
expect ( err ) . to . be . instanceOf ( MongoNetworkTimeoutError ) ;
125
102
expect ( result ) . to . not . exist ;
126
103
127
- expect ( conn ) . property ( 'stream' ) . property ( 'writableEnded ' , true ) ;
104
+ expect ( conn ) . property ( 'stream' ) . property ( 'destroyed ' , true ) ;
128
105
129
106
done ( ) ;
130
107
} ) ;
@@ -193,7 +170,7 @@ describe('new Connection()', function () {
193
170
194
171
context ( 'when multiple hellos exist on the stream' , function ( ) {
195
172
let callbackSpy ;
196
- const inputStream = new InputStream ( ) ;
173
+ const inputStream = new Readable ( ) ;
197
174
const document = { ok : 1 } ;
198
175
const last = { isWritablePrimary : true } ;
199
176
@@ -412,7 +389,7 @@ describe('new Connection()', function () {
412
389
connection = sinon . spy ( new Connection ( driverSocket , connectionOptionsDefaults ) ) ;
413
390
const messageStreamSymbol = getSymbolFrom ( connection , 'messageStream' ) ;
414
391
kDelayedTimeoutId = getSymbolFrom ( connection , 'delayedTimeoutId' ) ;
415
- messageStream = sinon . spy ( connection [ messageStreamSymbol ] ) ;
392
+ messageStream = connection [ messageStreamSymbol ] ;
416
393
} ) ;
417
394
418
395
afterEach ( ( ) => {
@@ -425,15 +402,13 @@ describe('new Connection()', function () {
425
402
426
403
driverSocket . emit ( 'timeout' ) ;
427
404
expect ( connection . onTimeout ) . to . have . been . calledOnce ;
428
- expect ( connection . destroy ) . to . not . have . been . called ;
429
405
expect ( connection ) . to . have . property ( kDelayedTimeoutId ) . that . is . instanceOf ( NodeJSTimeoutClass ) ;
430
406
expect ( connection ) . to . have . property ( 'closed' , false ) ;
431
- expect ( driverSocket . end ) . to . not . have . been . called ;
407
+ expect ( driverSocket . destroy ) . to . not . have . been . called ;
432
408
433
409
clock . tick ( 1 ) ;
434
410
435
- expect ( driverSocket . end ) . to . have . been . calledOnce ;
436
- expect ( connection . destroy ) . to . have . been . calledOnce ;
411
+ expect ( driverSocket . destroy ) . to . have . been . calledOnce ;
437
412
expect ( connection ) . to . have . property ( 'closed' , true ) ;
438
413
} ) ;
439
414
@@ -458,88 +433,6 @@ describe('new Connection()', function () {
458
433
expect ( connection ) . to . have . property ( 'closed' , false ) ;
459
434
expect ( connection ) . to . have . property ( kDelayedTimeoutId , null ) ;
460
435
} ) ;
461
-
462
- it ( 'destroys the message stream and socket' , ( ) => {
463
- expect ( connection ) . to . have . property ( kDelayedTimeoutId , null ) ;
464
-
465
- driverSocket . emit ( 'timeout' ) ;
466
-
467
- clock . tick ( 1 ) ;
468
-
469
- expect ( connection . onTimeout ) . to . have . been . calledOnce ;
470
- expect ( connection ) . to . have . property ( kDelayedTimeoutId ) . that . is . instanceOf ( NodeJSTimeoutClass ) ;
471
-
472
- expect ( messageStream . destroy ) . to . have . been . calledOnce ;
473
- expect ( driverSocket . destroy ) . to . not . have . been . called ;
474
- expect ( driverSocket . end ) . to . have . been . calledOnce ;
475
- } ) ;
476
- } ) ;
477
-
478
- describe ( 'onError()' , ( ) => {
479
- let connection : sinon . SinonSpiedInstance < Connection > ;
480
- let clock : sinon . SinonFakeTimers ;
481
- let timerSandbox : sinon . SinonFakeTimers ;
482
- let driverSocket : sinon . SinonSpiedInstance < FakeSocket > ;
483
- let messageStream : MessageStream ;
484
- beforeEach ( ( ) => {
485
- timerSandbox = createTimerSandbox ( ) ;
486
- clock = sinon . useFakeTimers ( ) ;
487
- driverSocket = sinon . spy ( new FakeSocket ( ) ) ;
488
- // @ts -expect-error: driverSocket does not fully satisfy the stream type, but that's okay
489
- connection = sinon . spy ( new Connection ( driverSocket , connectionOptionsDefaults ) ) ;
490
- const messageStreamSymbol = getSymbolFrom ( connection , 'messageStream' ) ;
491
- messageStream = sinon . spy ( connection [ messageStreamSymbol ] ) ;
492
- } ) ;
493
-
494
- afterEach ( ( ) => {
495
- timerSandbox . restore ( ) ;
496
- clock . restore ( ) ;
497
- } ) ;
498
-
499
- it ( 'destroys the message stream and socket' , ( ) => {
500
- messageStream . emit ( 'error' ) ;
501
- clock . tick ( 1 ) ;
502
- expect ( connection . onError ) . to . have . been . calledOnce ;
503
- connection . destroy ( { force : false } ) ;
504
- clock . tick ( 1 ) ;
505
- expect ( messageStream . destroy ) . to . have . been . called ;
506
- expect ( driverSocket . destroy ) . to . not . have . been . called ;
507
- expect ( driverSocket . end ) . to . have . been . calledOnce ;
508
- } ) ;
509
- } ) ;
510
-
511
- describe ( 'onClose()' , ( ) => {
512
- let connection : sinon . SinonSpiedInstance < Connection > ;
513
- let clock : sinon . SinonFakeTimers ;
514
- let timerSandbox : sinon . SinonFakeTimers ;
515
- let driverSocket : sinon . SinonSpiedInstance < FakeSocket > ;
516
- let messageStream : MessageStream ;
517
- beforeEach ( ( ) => {
518
- timerSandbox = createTimerSandbox ( ) ;
519
- clock = sinon . useFakeTimers ( ) ;
520
-
521
- driverSocket = sinon . spy ( new FakeSocket ( ) ) ;
522
- // @ts -expect-error: driverSocket does not fully satisfy the stream type, but that's okay
523
- connection = sinon . spy ( new Connection ( driverSocket , connectionOptionsDefaults ) ) ;
524
- const messageStreamSymbol = getSymbolFrom ( connection , 'messageStream' ) ;
525
- messageStream = sinon . spy ( connection [ messageStreamSymbol ] ) ;
526
- } ) ;
527
-
528
- afterEach ( ( ) => {
529
- timerSandbox . restore ( ) ;
530
- clock . restore ( ) ;
531
- } ) ;
532
-
533
- it ( 'destroys the message stream and socket' , ( ) => {
534
- driverSocket . emit ( 'close' ) ;
535
- clock . tick ( 1 ) ;
536
- expect ( connection . onClose ) . to . have . been . calledOnce ;
537
- connection . destroy ( { force : false } ) ;
538
- clock . tick ( 1 ) ;
539
- expect ( messageStream . destroy ) . to . have . been . called ;
540
- expect ( driverSocket . destroy ) . to . not . have . been . called ;
541
- expect ( driverSocket . end ) . to . have . been . calledOnce ;
542
- } ) ;
543
436
} ) ;
544
437
545
438
describe ( '.hasSessionSupport' , function ( ) {
@@ -593,96 +486,4 @@ describe('new Connection()', function () {
593
486
} ) ;
594
487
} ) ;
595
488
} ) ;
596
-
597
- describe ( 'destroy()' , ( ) => {
598
- let connection : sinon . SinonSpiedInstance < Connection > ;
599
- let clock : sinon . SinonFakeTimers ;
600
- let timerSandbox : sinon . SinonFakeTimers ;
601
- let driverSocket : sinon . SinonSpiedInstance < FakeSocket > ;
602
- let messageStream : MessageStream ;
603
- beforeEach ( ( ) => {
604
- timerSandbox = createTimerSandbox ( ) ;
605
- clock = sinon . useFakeTimers ( ) ;
606
-
607
- driverSocket = sinon . spy ( new FakeSocket ( ) ) ;
608
- // @ts -expect-error: driverSocket does not fully satisfy the stream type, but that's okay
609
- connection = sinon . spy ( new Connection ( driverSocket , connectionOptionsDefaults ) ) ;
610
- const messageStreamSymbol = getSymbolFrom ( connection , 'messageStream' ) ;
611
- messageStream = sinon . spy ( connection [ messageStreamSymbol ] ) ;
612
- } ) ;
613
-
614
- afterEach ( ( ) => {
615
- timerSandbox . restore ( ) ;
616
- clock . restore ( ) ;
617
- } ) ;
618
-
619
- context ( 'when options.force == true' , function ( ) {
620
- it ( 'calls stream.destroy' , ( ) => {
621
- connection . destroy ( { force : true } ) ;
622
- clock . tick ( 1 ) ;
623
- expect ( driverSocket . destroy ) . to . have . been . calledOnce ;
624
- } ) ;
625
-
626
- it ( 'does not call stream.end' , ( ) => {
627
- connection . destroy ( { force : true } ) ;
628
- clock . tick ( 1 ) ;
629
- expect ( driverSocket . end ) . to . not . have . been . called ;
630
- } ) ;
631
-
632
- it ( 'destroys the tcp socket' , ( ) => {
633
- connection . destroy ( { force : true } ) ;
634
- clock . tick ( 1 ) ;
635
- expect ( driverSocket . destroy ) . to . have . been . calledOnce ;
636
- } ) ;
637
-
638
- it ( 'destroys the messageStream' , ( ) => {
639
- connection . destroy ( { force : true } ) ;
640
- clock . tick ( 1 ) ;
641
- expect ( messageStream . destroy ) . to . have . been . calledOnce ;
642
- } ) ;
643
-
644
- it ( 'calls stream.destroy whenever destroy is called ' , ( ) => {
645
- connection . destroy ( { force : true } ) ;
646
- connection . destroy ( { force : true } ) ;
647
- connection . destroy ( { force : true } ) ;
648
- clock . tick ( 1 ) ;
649
- expect ( driverSocket . destroy ) . to . have . been . calledThrice ;
650
- } ) ;
651
- } ) ;
652
-
653
- context ( 'when options.force == false' , function ( ) {
654
- it ( 'calls stream.end' , ( ) => {
655
- connection . destroy ( { force : false } ) ;
656
- clock . tick ( 1 ) ;
657
- expect ( driverSocket . end ) . to . have . been . calledOnce ;
658
- } ) ;
659
-
660
- it ( 'does not call stream.destroy' , ( ) => {
661
- connection . destroy ( { force : false } ) ;
662
- clock . tick ( 1 ) ;
663
- expect ( driverSocket . destroy ) . to . not . have . been . called ;
664
- } ) ;
665
-
666
- it ( 'ends the tcp socket' , ( ) => {
667
- connection . destroy ( { force : false } ) ;
668
- clock . tick ( 1 ) ;
669
- expect ( driverSocket . end ) . to . have . been . calledOnce ;
670
- } ) ;
671
-
672
- it ( 'destroys the messageStream' , ( ) => {
673
- connection . destroy ( { force : false } ) ;
674
- clock . tick ( 1 ) ;
675
- expect ( messageStream . destroy ) . to . have . been . calledOnce ;
676
- } ) ;
677
-
678
- it ( 'calls stream.end exactly once when destroy is called multiple times' , ( ) => {
679
- connection . destroy ( { force : false } ) ;
680
- connection . destroy ( { force : false } ) ;
681
- connection . destroy ( { force : false } ) ;
682
- connection . destroy ( { force : false } ) ;
683
- clock . tick ( 1 ) ;
684
- expect ( driverSocket . end ) . to . have . been . calledOnce ;
685
- } ) ;
686
- } ) ;
687
- } ) ;
688
489
} ) ;
0 commit comments