@@ -10,6 +10,84 @@ import {
1010} from '../../../src/constants' ;
1111
1212describe ( 'Server Discovery and Monitoring Prose Tests' , function ( ) {
13+ context ( 'Monitors sleep at least minHeartbeatFrequencyMS between checks' , function ( ) {
14+ /*
15+ This test will be used to ensure monitors sleep for an appropriate amount of time between failed server checks
16+ so as to not flood the server with new connection creations.
17+
18+ This test requires MongoDB 4.9.0+.
19+
20+ 1. Enable the following failpoint:
21+ {
22+ configureFailPoint: "failCommand",
23+ mode: { times: 5 },
24+ data: {
25+ failCommands: ["hello"], // or legacy hello command
26+ errorCode: 1234,
27+ appName: "SDAMMinHeartbeatFrequencyTest"
28+ }
29+ }
30+ 2. Create a client with directConnection=true, appName="SDAMMinHeartbeatFrequencyTest", and serverSelectionTimeoutMS=5000.
31+ 3. Start a timer.
32+ 4. Execute a ping command.
33+ 5. Stop the timer. Assert that the ping took between 2 seconds and 3.5 seconds to complete.
34+ */
35+
36+ let client : MongoClient ;
37+ beforeEach ( async function ( ) {
38+ const utilClient = this . configuration . newClient ( { directConnection : true } ) ;
39+
40+ // 1.
41+ await utilClient . db ( 'admin' ) . command ( {
42+ configureFailPoint : 'failCommand' ,
43+ mode : { times : 5 } ,
44+ data : {
45+ failCommands : [ 'hello' , 'ismaster' ] ,
46+ errorCode : 1234 ,
47+ appName : 'SDAMMinHeartbeatFrequencyTest'
48+ }
49+ } ) ;
50+ await utilClient . close ( ) ;
51+
52+ // 2.
53+ client = this . configuration . newClient ( {
54+ directConnection : true ,
55+ appName : 'SDAMMinHeartbeatFrequencyTest' ,
56+ serverSelectionTimeoutMS : 5000
57+ } ) ;
58+ } ) ;
59+
60+ afterEach ( async function ( ) {
61+ await client . db ( 'admin' ) . command ( {
62+ configureFailPoint : 'failCommand' ,
63+ mode : 'off' ,
64+ data : {
65+ failCommands : [ 'hello' , 'ismaster' ] ,
66+ errorCode : 1234 ,
67+ appName : 'SDAMMinHeartbeatFrequencyTest'
68+ }
69+ } ) ;
70+ } ) ;
71+
72+ afterEach ( async function ( ) {
73+ await client . close ( ) ;
74+ } ) ;
75+
76+ it ( 'ensure monitors sleep for an appropriate amount of time between pings' , {
77+ metadata : { requires : { mongodb : '>=4.9.0' , topology : '!load-balanced' } } ,
78+ test : async function ( ) {
79+ // 3.
80+ const startTime = Date . now ( ) ;
81+ // 4.
82+ await client . db ( ) . command ( { ping : 1 } ) ;
83+ // 5.
84+ const timeTaken = Date . now ( ) - startTime ;
85+ const secondsTaken = timeTaken / 1000 ;
86+ expect ( secondsTaken ) . to . be . within ( 2 , 3.5 ) ;
87+ }
88+ } ) ;
89+ } ) ;
90+
1391 context ( 'Connection Pool Management' , function ( ) {
1492 /*
1593 This test will be used to ensure monitors properly create and unpause connection pools when they discover servers.
0 commit comments