Skip to content

Commit ca73d0d

Browse files
matthewdaleBenjamin Rewis
authored andcommitted
GODRIVER-2265 Make more correct assertions in SDAM heartbeat test. (#837)
1 parent 7894d89 commit ca73d0d

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

mongo/integration/sdam_prose_test.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,51 @@ func TestSDAMProse(t *testing.T) {
2121
mt := mtest.New(t)
2222
defer mt.Close()
2323

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).
2932
CreateCollection(false).
3033
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).
3340
//
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:
3843
//
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
4051

4152
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)))
4755

48-
time.Sleep(timeDuration + 50*time.Millisecond)
56+
time.Sleep(duration)
4957
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)
5269
})
5370

5471
mt.RunOpts("rtt tests", noClientOpts, func(mt *mtest.T) {

0 commit comments

Comments
 (0)