Skip to content

Commit 63ad6ba

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

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
@@ -22,34 +22,51 @@ func TestSDAMProse(t *testing.T) {
2222
mt := mtest.New(t)
2323
defer mt.Close()
2424

25-
lowHeartbeatFrequency := 50 * time.Millisecond
26-
heartbeatFrequencyClientOpts := options.Client().
27-
SetHeartbeatInterval(lowHeartbeatFrequency)
28-
heartbeatFrequencyMtOpts := mtest.NewOptions().
29-
ClientOptions(heartbeatFrequencyClientOpts).
25+
// Server limits non-streaming heartbeats and explicit server transition checks to at most one
26+
// per 500ms. Set the test interval to 500ms to minimize the difference between the behavior of
27+
// streaming and non-streaming heartbeat intervals.
28+
heartbeatInterval := 500 * time.Millisecond
29+
heartbeatIntervalClientOpts := options.Client().
30+
SetHeartbeatInterval(heartbeatInterval)
31+
heartbeatIntervalMtOpts := mtest.NewOptions().
32+
ClientOptions(heartbeatIntervalClientOpts).
3033
CreateCollection(false).
3134
ClientType(mtest.Proxy)
32-
mt.RunOpts("heartbeats processed more frequently", heartbeatFrequencyMtOpts, func(mt *mtest.T) {
33-
// Test that lowering heartbeat frequency to 50ms causes the client to process heartbeats more frequently.
35+
mt.RunOpts("heartbeats processed more frequently", heartbeatIntervalMtOpts, func(mt *mtest.T) {
36+
// Test that setting heartbeat interval to 500ms causes the client to process heartbeats
37+
// approximately every 500ms instead of the default 10s. Note that a Client doesn't
38+
// guarantee that it will process heartbeats exactly every 500ms, just that it will wait at
39+
// least 500ms between heartbeats (and should process heartbeats more frequently for shorter
40+
// interval settings).
3441
//
35-
// In X ms, tests on 4.4+ should process at least numberOfNodes * (1 + X/frequency + X/frequency) hello
36-
// responses:
37-
// Each node should process 1 normal response (connection handshake) + X/frequency awaitable responses.
38-
// Each node should also process X/frequency RTT hello responses.
42+
// For number of nodes N, interval I, and duration D, a Client should process at most X
43+
// operations:
3944
//
40-
// Tests on < 4.4 should process at least numberOfNodes * X/frequency messages.
45+
// X = (N * (1 handshake + D/I heartbeats + D/I RTTs))
46+
//
47+
// Assert that a Client processes the expected number of operations for heartbeats sent at
48+
// an interval between I and 2*I to account for different actual heartbeat intervals under
49+
// different runtime conditions.
50+
51+
duration := 2 * time.Second
4152

4253
numNodes := len(options.Client().ApplyURI(mtest.ClusterURI()).Hosts)
43-
timeDuration := 250 * time.Millisecond
44-
numExpectedResponses := numNodes * int(timeDuration/lowHeartbeatFrequency)
45-
if mtest.CompareServerVersions(mtest.ServerVersion(), "4.4") >= 0 {
46-
numExpectedResponses = numNodes * (2*int(timeDuration/lowHeartbeatFrequency) + 1)
47-
}
54+
maxExpected := numNodes * (1 + 2*int(duration/heartbeatInterval))
55+
minExpected := numNodes * (1 + 2*int(duration/(heartbeatInterval*2)))
4856

49-
time.Sleep(timeDuration + 50*time.Millisecond)
57+
time.Sleep(duration)
5058
messages := mt.GetProxiedMessages()
51-
assert.True(mt, len(messages) >= numExpectedResponses, "expected at least %d responses, got %d",
52-
numExpectedResponses, len(messages))
59+
assert.True(
60+
mt,
61+
len(messages) >= minExpected && len(messages) <= maxExpected,
62+
"expected number of messages to be in range [%d, %d], got %d"+
63+
" (num nodes = %d, duration = %v, interval = %v)",
64+
minExpected,
65+
maxExpected,
66+
len(messages),
67+
numNodes,
68+
duration,
69+
heartbeatInterval)
5370
})
5471

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

0 commit comments

Comments
 (0)