-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathserver_discovery_and_monitoring.prose.test.ts
More file actions
185 lines (164 loc) · 6.05 KB
/
server_discovery_and_monitoring.prose.test.ts
File metadata and controls
185 lines (164 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { expect } from 'chai';
import { once } from 'events';
import {
CONNECTION_POOL_CLEARED,
CONNECTION_POOL_READY,
type MongoClient,
SERVER_HEARTBEAT_FAILED,
SERVER_HEARTBEAT_SUCCEEDED
} from '../../mongodb';
describe('Server Discovery and Monitoring Prose Tests', function () {
context('Monitors sleep at least minHeartbeatFrequencyMS between checks', function () {
/*
This test will be used to ensure monitors sleep for an appropriate amount of time between failed server checks
so as to not flood the server with new connection creations.
This test requires MongoDB 4.9.0+.
1. Enable the following failpoint:
{
configureFailPoint: "failCommand",
mode: { times: 5 },
data: {
failCommands: ["hello"], // or legacy hello command
errorCode: 1234,
appName: "SDAMMinHeartbeatFrequencyTest"
}
}
2. Create a client with directConnection=true, appName="SDAMMinHeartbeatFrequencyTest", and serverSelectionTimeoutMS=5000.
3. Start a timer.
4. Execute a ping command.
5. Stop the timer. Assert that the ping took between 2 seconds and 3.5 seconds to complete.
*/
let client: MongoClient;
beforeEach(async function () {
const utilClient = this.configuration.newClient({ directConnection: true });
// 1.
await utilClient.db('admin').command({
configureFailPoint: 'failCommand',
mode: { times: 5 },
data: {
failCommands: ['hello', 'ismaster'],
errorCode: 1234,
appName: 'SDAMMinHeartbeatFrequencyTest'
}
});
await utilClient.close();
// 2.
client = this.configuration.newClient({
directConnection: true,
appName: 'SDAMMinHeartbeatFrequencyTest',
serverSelectionTimeoutMS: 5000
});
});
afterEach(async function () {
await client.db('admin').command({
configureFailPoint: 'failCommand',
mode: 'off',
data: {
failCommands: ['hello', 'ismaster'],
errorCode: 1234,
appName: 'SDAMMinHeartbeatFrequencyTest'
}
});
});
afterEach(async function () {
await client.close();
});
it('ensure monitors sleep for an appropriate amount of time between pings', {
metadata: { requires: { mongodb: '>=4.9.0', topology: '!load-balanced' } },
test: async function () {
// 3.
const startTime = Date.now();
// 4.
await client.db().command({ ping: 1 });
// 5.
const timeTaken = Date.now() - startTime;
const secondsTaken = timeTaken / 1000;
expect(secondsTaken).to.be.within(2, 3.5);
}
});
});
context('Connection Pool Management', function () {
/*
This test will be used to ensure monitors properly create and unpause connection pools when they discover servers.
This test requires failCommand appName support which is only available in MongoDB 4.2.9+.
1. Create a client with directConnection=true, appName="SDAMPoolManagementTest", and heartbeatFrequencyMS=500 (or lower if possible).
2. Verify via SDAM and CMAP event monitoring that a ConnectionPoolReadyEvent occurs after the first ServerHeartbeatSucceededEvent event does.
3. Enable the following failpoint:
{
configureFailPoint: "failCommand",
mode: { times: 2 },
data: {
failCommands: ["hello"], // or legacy hello command
errorCode: 1234,
appName: "SDAMPoolManagementTest"
}
}
4. Verify that a ServerHeartbeatFailedEvent and a ConnectionPoolClearedEvent (CMAP) are emitted.
5. Then verify that a ServerHeartbeatSucceededEvent and a ConnectionPoolReadyEvent (CMAP) are emitted.
6. Disable the failpoint.
*/
let client: MongoClient;
const events: string[] = [];
beforeEach(async function () {
client = this.configuration.newClient({
directConnection: true,
appName: 'SDAMPoolManagementTest',
heartbeatFrequencyMS: 500
});
for (const event of [
CONNECTION_POOL_READY,
SERVER_HEARTBEAT_SUCCEEDED,
SERVER_HEARTBEAT_FAILED,
CONNECTION_POOL_CLEARED
]) {
client.on(event, () => {
events.push(event);
});
}
});
afterEach(async function () {
await client.db('admin').command({
configureFailPoint: 'failCommand',
mode: 'off',
data: {
failCommands: ['hello'],
errorCode: 1234,
appName: 'SDAMPoolManagementTest'
}
});
});
afterEach(async function () {
await client.close();
});
it.skip(
'ensure monitors properly create and unpause connection pools when they discover servers',
{
metadata: { requires: { mongodb: '>=4.2.9', topology: '!load-balanced' } },
test: async function () {
await client.connect();
expect(events.shift()).to.equal(SERVER_HEARTBEAT_SUCCEEDED);
expect(events.shift()).to.equal(CONNECTION_POOL_READY);
expect(events).to.be.empty;
const heartBeatFailedEvent = once(client, SERVER_HEARTBEAT_FAILED);
await client.db('admin').command({
configureFailPoint: 'failCommand',
mode: { times: 2 },
data: {
failCommands: ['hello'],
errorCode: 1234,
appName: 'SDAMPoolManagementTest'
}
});
await heartBeatFailedEvent;
expect(events.shift()).to.equal(SERVER_HEARTBEAT_FAILED);
expect(events.shift()).to.equal(CONNECTION_POOL_CLEARED);
expect(events).to.be.empty;
await once(client, SERVER_HEARTBEAT_SUCCEEDED);
expect(events.shift()).to.equal(SERVER_HEARTBEAT_SUCCEEDED);
expect(events.shift()).to.equal(CONNECTION_POOL_READY);
expect(events).to.be.empty;
}
}
).skipReason = 'TODO(NODE-5206): fix flaky test';
});
});