@@ -21,34 +21,51 @@ func TestSDAMProse(t *testing.T) {
21
21
mt := mtest .New (t )
22
22
defer mt .Close ()
23
23
24
- lowHeartbeatFrequency := 50 * time .Millisecond
25
- heartbeatFrequencyClientOpts := options .Client ().
26
- SetHeartbeatInterval (lowHeartbeatFrequency )
27
- heartbeatFrequencyMtOpts := mtest .NewOptions ().
28
- ClientOptions (heartbeatFrequencyClientOpts ).
24
+ // Server limits non-streaming heartbeats and explicit server transition checks to at most one
25
+ // per 500ms. Set the test interval to 500ms to minimize the difference between the behavior of
26
+ // streaming and non-streaming heartbeat intervals.
27
+ heartbeatInterval := 500 * time .Millisecond
28
+ heartbeatIntervalClientOpts := options .Client ().
29
+ SetHeartbeatInterval (heartbeatInterval )
30
+ heartbeatIntervalMtOpts := mtest .NewOptions ().
31
+ ClientOptions (heartbeatIntervalClientOpts ).
29
32
CreateCollection (false ).
30
33
ClientType (mtest .Proxy )
31
- mt .RunOpts ("heartbeats processed more frequently" , heartbeatFrequencyMtOpts , func (mt * mtest.T ) {
32
- // Test that lowering heartbeat frequency to 50ms causes the client to process heartbeats more frequently.
34
+ mt .RunOpts ("heartbeats processed more frequently" , heartbeatIntervalMtOpts , func (mt * mtest.T ) {
35
+ // Test that setting heartbeat interval to 500ms causes the client to process heartbeats
36
+ // approximately every 500ms instead of the default 10s. Note that a Client doesn't
37
+ // guarantee that it will process heartbeats exactly every 500ms, just that it will wait at
38
+ // least 500ms between heartbeats (and should process heartbeats more frequently for shorter
39
+ // interval settings).
33
40
//
34
- // In X ms, tests on 4.4+ should process at least numberOfNodes * (1 + X/frequency + X/frequency) isMaster
35
- // responses:
36
- // Each node should process 1 normal response (connection handshake) + X/frequency awaitable responses.
37
- // Each node should also process X/frequency RTT isMaster responses.
41
+ // For number of nodes N, interval I, and duration D, a Client should process at most X
42
+ // operations:
38
43
//
39
- // Tests on < 4.4 should process at least numberOfNodes * X/frequency messages.
44
+ // X = (N * (1 handshake + D/I heartbeats + D/I RTTs))
45
+ //
46
+ // Assert that a Client processes the expected number of operations for heartbeats sent at
47
+ // an interval between I and 2*I to account for different actual heartbeat intervals under
48
+ // different runtime conditions.
49
+
50
+ duration := 2 * time .Second
40
51
41
52
numNodes := len (options .Client ().ApplyURI (mtest .ClusterURI ()).Hosts )
42
- timeDuration := 250 * time .Millisecond
43
- numExpectedResponses := numNodes * int (timeDuration / lowHeartbeatFrequency )
44
- if mtest .CompareServerVersions (mtest .ServerVersion (), "4.4" ) >= 0 {
45
- numExpectedResponses = numNodes * (2 * int (timeDuration / lowHeartbeatFrequency ) + 1 )
46
- }
53
+ maxExpected := numNodes * (1 + 2 * int (duration / heartbeatInterval ))
54
+ minExpected := numNodes * (1 + 2 * int (duration / (heartbeatInterval * 2 )))
47
55
48
- time .Sleep (timeDuration + 50 * time . Millisecond )
56
+ time .Sleep (duration )
49
57
messages := mt .GetProxiedMessages ()
50
- assert .True (mt , len (messages ) >= numExpectedResponses , "expected at least %d responses, got %d" ,
51
- numExpectedResponses , len (messages ))
58
+ assert .True (
59
+ mt ,
60
+ len (messages ) >= minExpected && len (messages ) <= maxExpected ,
61
+ "expected number of messages to be in range [%d, %d], got %d" +
62
+ " (num nodes = %d, duration = %v, interval = %v)" ,
63
+ minExpected ,
64
+ maxExpected ,
65
+ len (messages ),
66
+ numNodes ,
67
+ duration ,
68
+ heartbeatInterval )
52
69
})
53
70
54
71
mt .RunOpts ("rtt tests" , noClientOpts , func (mt * mtest.T ) {
0 commit comments