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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
/**
* Copyright (C) 2015 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/platform/basic.h"
#include <iostream>
#include <memory>
#include <utility>
#include "mongo/base/status.h"
#include "mongo/executor/network_connection_hook.h"
#include "mongo/executor/network_interface.h"
#include "mongo/executor/network_interface_mock.h"
#include "mongo/executor/test_network_connection_hook.h"
#include "mongo/executor/thread_pool_mock.h"
#include "mongo/stdx/memory.h"
#include "mongo/unittest/unittest.h"
namespace mongo {
namespace executor {
namespace {
class NetworkInterfaceMockTest : public mongo::unittest::Test {
public:
NetworkInterfaceMockTest()
: _net{}, _executor(&_net, 1, ThreadPoolMock::Options()), _tearDownCalled(false) {}
NetworkInterfaceMock& net() {
return _net;
}
ThreadPoolMock& executor() {
return _executor;
}
HostAndPort testHost() {
return {"localHost", 27017};
}
// intentionally not done in setUp as some methods need to be called prior to starting
// the network.
void startNetwork() {
net().startup();
executor().startup();
}
virtual void setUp() override {
_tearDownCalled = false;
}
virtual void tearDown() override {
// We're calling tearDown() manually in some tests so
// we can check post-conditions.
if (_tearDownCalled) {
return;
}
_tearDownCalled = true;
net().exitNetwork();
executor().shutdown();
// Wake up sleeping executor threads so they clean up.
net().signalWorkAvailable();
executor().join();
net().shutdown();
}
private:
NetworkInterfaceMock _net;
ThreadPoolMock _executor;
bool _tearDownCalled;
};
TEST_F(NetworkInterfaceMockTest, ConnectionHook) {
bool validateCalled = false;
bool hostCorrectForValidate = false;
bool replyCorrectForValidate;
bool makeRequestCalled = false;
bool hostCorrectForRequest = false;
bool handleReplyCalled = false;
bool gotExpectedReply = false;
RemoteCommandRequest expectedRequest{testHost(),
"test",
BSON("1" << 2),
BSON("some"
<< "stuff"),
nullptr};
RemoteCommandResponse expectedResponse{BSON("foo"
<< "bar"
<< "baz"
<< "garply"),
BSON("bar"
<< "baz"),
Milliseconds(30)};
// need to copy as it will be moved
auto isMasterReplyData = BSON("iamyour"
<< "father");
RemoteCommandResponse isMasterReply{
isMasterReplyData.copy(), BSON("blah" << 2), Milliseconds(20)};
net().setHandshakeReplyForHost(testHost(), std::move(isMasterReply));
// Since the contract of these methods is that they do not throw, we run the ASSERTs in
// the test scope.
net().setConnectionHook(makeTestHook(
[&](const HostAndPort& remoteHost, const RemoteCommandResponse& isMasterReply) {
validateCalled = true;
hostCorrectForValidate = (remoteHost == testHost());
replyCorrectForValidate = SimpleBSONObjComparator::kInstance.evaluate(
isMasterReply.data == isMasterReplyData);
return Status::OK();
},
[&](const HostAndPort& remoteHost) {
makeRequestCalled = true;
hostCorrectForRequest = (remoteHost == testHost());
return boost::make_optional<RemoteCommandRequest>(expectedRequest);
},
[&](const HostAndPort& remoteHost, RemoteCommandResponse&& response) {
handleReplyCalled = true;
hostCorrectForRequest = (remoteHost == testHost());
gotExpectedReply = SimpleBSONObjComparator::kInstance.evaluate(
expectedResponse.data == response.data); // Don't bother checking all fields.
return Status::OK();
}));
startNetwork();
TaskExecutor::CallbackHandle cb{};
bool commandFinished = false;
bool gotCorrectCommandReply = false;
RemoteCommandRequest actualCommandExpected{
testHost(), "testDB", BSON("test" << 1), rpc::makeEmptyMetadata(), nullptr};
RemoteCommandResponse actualResponseExpected{BSON("1212121212"
<< "12121212121212"),
BSONObj(),
Milliseconds(0)};
ASSERT_OK(net().startCommand(cb, actualCommandExpected, [&](RemoteCommandResponse resp) {
commandFinished = true;
if (resp.isOK()) {
gotCorrectCommandReply = (actualResponseExpected.toString() == resp.toString());
}
}));
// At this point validate and makeRequest should have been called.
ASSERT(validateCalled);
ASSERT(hostCorrectForValidate);
ASSERT(replyCorrectForValidate);
ASSERT(makeRequestCalled);
ASSERT(hostCorrectForRequest);
// handleReply should not have been called as we haven't responded to the reply yet.
ASSERT(!handleReplyCalled);
// we haven't gotten to the actual command yet
ASSERT(!commandFinished);
{
net().enterNetwork();
ASSERT(net().hasReadyRequests());
auto req = net().getNextReadyRequest();
ASSERT_BSONOBJ_EQ(req->getRequest().cmdObj, expectedRequest.cmdObj);
net().scheduleResponse(req, net().now(), expectedResponse);
net().runReadyNetworkOperations();
net().exitNetwork();
}
// We should have responded to the post connect command.
ASSERT(handleReplyCalled);
ASSERT(gotExpectedReply);
// We should not have responsed to the actual command.
ASSERT(!commandFinished);
{
net().enterNetwork();
ASSERT(net().hasReadyRequests());
auto actualCommand = net().getNextReadyRequest();
ASSERT_BSONOBJ_EQ(actualCommand->getRequest().cmdObj, actualCommandExpected.cmdObj);
net().scheduleResponse(actualCommand, net().now(), actualResponseExpected);
net().runReadyNetworkOperations();
net().exitNetwork();
}
ASSERT(commandFinished);
ASSERT(gotCorrectCommandReply);
}
TEST_F(NetworkInterfaceMockTest, ConnectionHookFailedValidation) {
net().setConnectionHook(makeTestHook(
[&](const HostAndPort& remoteHost, const RemoteCommandResponse& isMasterReply) -> Status {
// We just need some obscure non-OK code.
return {ErrorCodes::ConflictingOperationInProgress, "blah"};
},
[&](const HostAndPort& remoteHost) -> StatusWith<boost::optional<RemoteCommandRequest>> {
MONGO_UNREACHABLE;
},
[&](const HostAndPort& remoteHost, RemoteCommandResponse&& response) -> Status {
MONGO_UNREACHABLE;
}));
startNetwork();
TaskExecutor::CallbackHandle cb{};
bool commandFinished = false;
bool statusPropagated = false;
RemoteCommandRequest request;
ASSERT_OK(net().startCommand(cb, request, [&](RemoteCommandResponse resp) {
commandFinished = true;
statusPropagated = resp.status.code() == ErrorCodes::ConflictingOperationInProgress;
}));
{
net().enterNetwork();
// We should have short-circuited the network and immediately called the callback.
// If we change isMaster replies to go through the normal network mechanism,
// this test will need to change.
ASSERT(!net().hasReadyRequests());
net().exitNetwork();
}
ASSERT(commandFinished);
ASSERT(statusPropagated);
}
TEST_F(NetworkInterfaceMockTest, ConnectionHookNoRequest) {
bool makeRequestCalled = false;
net().setConnectionHook(makeTestHook(
[&](const HostAndPort& remoteHost, const RemoteCommandResponse& isMasterReply) -> Status {
return Status::OK();
},
[&](const HostAndPort& remoteHost) -> StatusWith<boost::optional<RemoteCommandRequest>> {
makeRequestCalled = true;
return {boost::none};
},
[&](const HostAndPort& remoteHost, RemoteCommandResponse&& response) -> Status {
MONGO_UNREACHABLE;
}));
startNetwork();
TaskExecutor::CallbackHandle cb{};
bool commandFinished = false;
RemoteCommandRequest request;
ASSERT_OK(net().startCommand(
cb, request, [&](RemoteCommandResponse resp) { commandFinished = true; }));
{
net().enterNetwork();
ASSERT(net().hasReadyRequests());
auto req = net().getNextReadyRequest();
net().scheduleResponse(req, net().now(), RemoteCommandResponse{});
net().runReadyNetworkOperations();
net().exitNetwork();
}
ASSERT(commandFinished);
}
TEST_F(NetworkInterfaceMockTest, ConnectionHookMakeRequestFails) {
bool makeRequestCalled = false;
net().setConnectionHook(makeTestHook(
[&](const HostAndPort& remoteHost, const RemoteCommandResponse& isMasterReply) -> Status {
return Status::OK();
},
[&](const HostAndPort& remoteHost) -> StatusWith<boost::optional<RemoteCommandRequest>> {
makeRequestCalled = true;
return {ErrorCodes::InvalidSyncSource, "blah"};
},
[&](const HostAndPort& remoteHost, RemoteCommandResponse&& response) -> Status {
MONGO_UNREACHABLE;
}));
startNetwork();
TaskExecutor::CallbackHandle cb{};
bool commandFinished = false;
bool errorPropagated = false;
RemoteCommandRequest request;
ASSERT_OK(net().startCommand(cb, request, [&](RemoteCommandResponse resp) {
commandFinished = true;
errorPropagated = resp.status.code() == ErrorCodes::InvalidSyncSource;
}));
{
net().enterNetwork();
ASSERT(!net().hasReadyRequests());
net().exitNetwork();
}
ASSERT(commandFinished);
ASSERT(errorPropagated);
}
TEST_F(NetworkInterfaceMockTest, ConnectionHookHandleReplyFails) {
bool handleReplyCalled = false;
net().setConnectionHook(makeTestHook(
[&](const HostAndPort& remoteHost, const RemoteCommandResponse& isMasterReply) -> Status {
return Status::OK();
},
[&](const HostAndPort& remoteHost) -> StatusWith<boost::optional<RemoteCommandRequest>> {
return boost::make_optional<RemoteCommandRequest>({});
},
[&](const HostAndPort& remoteHost, RemoteCommandResponse&& response) -> Status {
handleReplyCalled = true;
return {ErrorCodes::CappedPositionLost, "woot"};
}));
startNetwork();
TaskExecutor::CallbackHandle cb{};
bool commandFinished = false;
bool errorPropagated = false;
RemoteCommandRequest request;
ASSERT_OK(net().startCommand(cb, request, [&](RemoteCommandResponse resp) {
commandFinished = true;
errorPropagated = resp.status.code() == ErrorCodes::CappedPositionLost;
}));
ASSERT(!handleReplyCalled);
{
net().enterNetwork();
ASSERT(net().hasReadyRequests());
auto req = net().getNextReadyRequest();
net().scheduleResponse(req, net().now(), RemoteCommandResponse{});
net().runReadyNetworkOperations();
net().exitNetwork();
}
ASSERT(handleReplyCalled);
ASSERT(commandFinished);
ASSERT(errorPropagated);
}
TEST_F(NetworkInterfaceMockTest, InShutdown) {
startNetwork();
ASSERT_FALSE(net().inShutdown());
tearDown();
ASSERT(net().inShutdown());
}
TEST_F(NetworkInterfaceMockTest, StartCommandReturnsNotOKIfShutdownHasStarted) {
startNetwork();
tearDown();
TaskExecutor::CallbackHandle cb{};
RemoteCommandRequest request;
ASSERT_NOT_OK(net().startCommand(cb, request, [](RemoteCommandResponse resp) {}));
}
TEST_F(NetworkInterfaceMockTest, SetAlarmReturnsNotOKIfShutdownHasStarted) {
startNetwork();
tearDown();
ASSERT_NOT_OK(net().setAlarm(net().now() + Milliseconds(100), [] {}));
}
TEST_F(NetworkInterfaceMockTest, CommandTimeout) {
startNetwork();
TaskExecutor::CallbackHandle cb;
RemoteCommandRequest request;
request.timeout = Milliseconds(2000);
ErrorCodes::Error statusPropagated = ErrorCodes::OK;
auto finishFn = [&](RemoteCommandResponse resp) { statusPropagated = resp.status.code(); };
//
// Command times out.
//
ASSERT_OK(net().startCommand(cb, request, finishFn));
net().enterNetwork();
ASSERT(net().hasReadyRequests());
net().blackHole(net().getNextReadyRequest());
net().runUntil(net().now() + Milliseconds(2010));
net().exitNetwork();
ASSERT_NOT_EQUALS(ErrorCodes::OK, statusPropagated);
//
// Command finishes before timeout.
//
Date_t start = net().now();
ASSERT_OK(net().startCommand(cb, request, finishFn));
net().enterNetwork();
// Consume the request. We'll schedule a successful response later.
ASSERT(net().hasReadyRequests());
auto noi = net().getNextReadyRequest();
// Assert the command hasn't timed out after 1000ms.
net().runUntil(start + Milliseconds(1000));
ASSERT_EQUALS(start + Milliseconds(1000), net().now());
ASSERT_NOT_EQUALS(ErrorCodes::OK, statusPropagated);
// Reply with a successful response.
net().scheduleResponse(noi, net().now(), {});
net().runReadyNetworkOperations();
net().exitNetwork();
ASSERT_EQUALS(ErrorCodes::OK, statusPropagated);
ASSERT_EQUALS(start + Milliseconds(1000), net().now());
}
} // namespace
} // namespace executor
} // namespace mongo
|