@@ -6,22 +6,24 @@ import { type Connection, type MongoClient, type RTTPinger } from '../../mongodb
6
6
import { sleep } from '../../tools/utils' ;
7
7
8
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
9
+ * RTTPingers are only created after getting a hello from the server that defines topologyVersion
10
+ * Each monitor is reaching out to a different node and rttPinger's are created async as a result.
12
11
*
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?
12
+ * This function checks for rttPingers and sleeps if none are found.
14
13
*/
15
14
async function getRTTPingers ( client : MongoClient ) {
15
+ type RTTPingerConnection = Omit < RTTPinger , 'connection' > & { connection : Connection } ;
16
+ const pingers = ( rtt => rtt ?. connection != null ) as ( r ?: RTTPinger ) => r is RTTPingerConnection ;
17
+
18
+ if ( ! client . topology ) expect . fail ( 'Must provide a connected client' ) ;
19
+
16
20
// eslint-disable-next-line no-constant-condition
17
21
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
+ const servers = client . topology . s . servers . values ( ) ;
23
+ const rttPingers = Array . from ( servers , s => s . monitor ?. rttPinger ) . filter ( pingers ) ;
22
24
23
25
if ( rttPingers . length !== 0 ) {
24
- return rttPingers as ( Omit < RTTPinger , 'connection' > & { connection : Connection } ) [ ] ;
26
+ return rttPingers ;
25
27
}
26
28
27
29
await sleep ( 5 ) ;
@@ -32,25 +34,26 @@ describe('class RTTPinger', () => {
32
34
afterEach ( ( ) => sinon . restore ( ) ) ;
33
35
34
36
beforeEach ( async function ( ) {
37
+ if ( ! this . currentTest ) return ;
35
38
if ( this . configuration . isLoadBalanced ) {
36
- if ( this . currentTest )
37
- this . currentTest . skipReason = 'No monitoring in LB mode, test not relevant' ;
39
+ this . currentTest . skipReason = 'No monitoring in LB mode, test not relevant' ;
38
40
return this . skip ( ) ;
39
41
}
40
42
if ( semver . gte ( '4.4.0' , this . configuration . version ) ) {
41
- if ( this . currentTest )
42
- this . currentTest . skipReason =
43
- 'Test requires streaming monitoring, needs to be on MongoDB 4.4+' ;
43
+ this . currentTest . skipReason =
44
+ 'Test requires streaming monitoring, needs to be on MongoDB 4.4+' ;
44
45
return this . skip ( ) ;
45
46
}
46
47
} ) ;
47
48
48
49
context ( 'when serverApi is enabled' , ( ) => {
49
50
let serverApiClient : MongoClient ;
51
+
50
52
beforeEach ( async function ( ) {
53
+ if ( ! this . currentTest ) return ;
54
+
51
55
if ( semver . gte ( '5.0.0' , this . configuration . version ) ) {
52
- if ( this . currentTest )
53
- this . currentTest . skipReason = 'Test requires serverApi, needs to be on MongoDB 5.0+' ;
56
+ this . currentTest . skipReason = 'Test requires serverApi, needs to be on MongoDB 5.0+' ;
54
57
return this . skip ( ) ;
55
58
}
56
59
0 commit comments