@@ -2,9 +2,32 @@ import { expect } from 'chai';
2
2
import * as semver from 'semver' ;
3
3
import * as sinon from 'sinon' ;
4
4
5
- import { type MongoClient } from '../../mongodb' ;
5
+ import { type Connection , type MongoClient , type RTTPinger } from '../../mongodb' ;
6
6
import { sleep } from '../../tools/utils' ;
7
7
8
+ /**
9
+ * RTTPinger creation depends on getting a response to the monitor's initial hello
10
+ * and that hello containing a topologyVersion.
11
+ * Subsequently the rttPinger creates its connection asynchronously
12
+ *
13
+ * I just went with a sleepy loop, until we have what we need, One could also use SDAM events in a clever way perhaps?
14
+ */
15
+ async function getRTTPingers ( client : MongoClient ) {
16
+ // eslint-disable-next-line no-constant-condition
17
+ while ( true ) {
18
+ const rttPingers = Array . from ( client . topology ?. s . servers . values ( ) ?? [ ] , s => {
19
+ if ( s . monitor ?. rttPinger ?. connection != null ) return s . monitor ?. rttPinger ;
20
+ else null ;
21
+ } ) . filter ( rtt => rtt != null ) ;
22
+
23
+ if ( rttPingers . length !== 0 ) {
24
+ return rttPingers as ( Omit < RTTPinger , 'connection' > & { connection : Connection } ) [ ] ;
25
+ }
26
+
27
+ await sleep ( 5 ) ;
28
+ }
29
+ }
30
+
8
31
describe ( 'class RTTPinger' , ( ) => {
9
32
afterEach ( ( ) => sinon . restore ( ) ) ;
10
33
@@ -43,16 +66,9 @@ describe('class RTTPinger', () => {
43
66
44
67
it ( 'measures rtt with a hello command' , async function ( ) {
45
68
await serverApiClient . connect ( ) ;
46
- await sleep ( 1001 ) ; // rttPinger creation
47
-
48
- const rttPingers = Array . from ( serverApiClient . topology ?. s . servers . values ( ) ?? [ ] , s => {
49
- if ( s . monitor ?. rttPinger ) return s . monitor ?. rttPinger ;
50
- else expect . fail ( 'expected rttPinger to be defined' ) ;
51
- } ) ;
52
-
53
- await sleep ( 11 ) ; // rttPinger connection creation
69
+ const rttPingers = await getRTTPingers ( serverApiClient ) ;
54
70
55
- const spies = rttPingers . map ( rtt => rtt . connection && sinon . spy ( rtt . connection , 'command' ) ) ;
71
+ const spies = rttPingers . map ( rtt => sinon . spy ( rtt . connection , 'command' ) ) ;
56
72
57
73
await sleep ( 11 ) ; // allow for another ping after spies have been made
58
74
@@ -75,19 +91,12 @@ describe('class RTTPinger', () => {
75
91
76
92
it ( 'destroys the connection' , async function ( ) {
77
93
await client . connect ( ) ;
78
- await sleep ( 1001 ) ; // rttPinger creation
94
+ const rttPingers = await getRTTPingers ( client ) ;
79
95
80
- const rttPingers = Array . from ( client . topology ?. s . servers . values ( ) ?? [ ] , s => {
81
- if ( s . monitor ?. rttPinger ) return s . monitor ?. rttPinger ;
82
- else expect . fail ( 'expected rttPinger to be defined' ) ;
83
- } ) ;
84
-
85
- await sleep ( 11 ) ; // rttPinger connection creation
86
-
87
- for ( const rtt of rttPingers )
88
- rtt . connection && sinon . stub ( rtt . connection , 'command' ) . yieldsRight ( new Error ( 'any' ) ) ;
89
-
90
- const spies = rttPingers . map ( rtt => rtt . connection && sinon . spy ( rtt . connection , 'destroy' ) ) ;
96
+ for ( const rtt of rttPingers ) {
97
+ sinon . stub ( rtt . connection , 'command' ) . yieldsRight ( new Error ( 'any' ) ) ;
98
+ }
99
+ const spies = rttPingers . map ( rtt => sinon . spy ( rtt . connection , 'destroy' ) ) ;
91
100
92
101
await sleep ( 11 ) ; // allow for another ping after spies have been made
93
102
0 commit comments