@@ -6,22 +6,24 @@ import { type Connection, type MongoClient, type RTTPinger } from '../../mongodb
66import { sleep } from '../../tools/utils' ;
77
88/**
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.
1211 *
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.
1413 */
1514async 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+
1620 // eslint-disable-next-line no-constant-condition
1721 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 ) ;
2224
2325 if ( rttPingers . length !== 0 ) {
24- return rttPingers as ( Omit < RTTPinger , 'connection' > & { connection : Connection } ) [ ] ;
26+ return rttPingers ;
2527 }
2628
2729 await sleep ( 5 ) ;
@@ -32,25 +34,26 @@ describe('class RTTPinger', () => {
3234 afterEach ( ( ) => sinon . restore ( ) ) ;
3335
3436 beforeEach ( async function ( ) {
37+ if ( ! this . currentTest ) return ;
3538 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' ;
3840 return this . skip ( ) ;
3941 }
4042 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+' ;
4445 return this . skip ( ) ;
4546 }
4647 } ) ;
4748
4849 context ( 'when serverApi is enabled' , ( ) => {
4950 let serverApiClient : MongoClient ;
51+
5052 beforeEach ( async function ( ) {
53+ if ( ! this . currentTest ) return ;
54+
5155 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+' ;
5457 return this . skip ( ) ;
5558 }
5659
0 commit comments