summaryrefslogtreecommitdiff
path: root/src/mongo
diff options
context:
space:
mode:
authorVesselina Ratcheva <vesselina.ratcheva@10gen.com>2020-11-17 21:16:50 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-12-01 09:52:46 +0000
commit87a3ef28352d45516e09b831fce6b4aa4ac46b21 (patch)
tree6f9658dc26818aefa6f2c6707a562d5b35044007 /src/mongo
parentecd54f7d5a7d56baf9509e83f0393565369b1936 (diff)
downloadmongo-87a3ef28352d45516e09b831fce6b4aa4ac46b21.tar.gz
SERVER-50155 Create unittest file for MockNetwork
SERVER-52957 Move mock network fixture into executor folder
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/db/repl/SConscript4
-rw-r--r--src/mongo/db/repl/initial_syncer_test.cpp2
-rw-r--r--src/mongo/db/repl/replication_coordinator_impl_elect_v1_test.cpp2
-rw-r--r--src/mongo/executor/SConscript8
-rw-r--r--src/mongo/executor/mock_network_fixture.cpp (renamed from src/mongo/db/repl/mock_fixture.cpp)2
-rw-r--r--src/mongo/executor/mock_network_fixture.h (renamed from src/mongo/db/repl/mock_fixture.h)0
-rw-r--r--src/mongo/executor/mock_network_fixture_test.cpp102
-rw-r--r--src/mongo/executor/network_interface_mock_test.cpp57
-rw-r--r--src/mongo/executor/network_interface_mock_test_fixture.cpp64
-rw-r--r--src/mongo/executor/network_interface_mock_test_fixture.h70
10 files changed, 247 insertions, 64 deletions
diff --git a/src/mongo/db/repl/SConscript b/src/mongo/db/repl/SConscript
index 1095830b10b..5844865da81 100644
--- a/src/mongo/db/repl/SConscript
+++ b/src/mongo/db/repl/SConscript
@@ -790,7 +790,6 @@ env.Library(
env.Library(
target='replmocks',
source=[
- 'mock_fixture.cpp',
'replication_consistency_markers_mock.cpp',
'replication_coordinator_external_state_mock.cpp',
'replication_coordinator_mock.cpp',
@@ -807,9 +806,6 @@ env.Library(
'replica_set_messages',
'storage_interface',
],
- LIBDEPS_PRIVATE=[
- '$BUILD_DIR/mongo/db/matcher/expressions',
- ],
)
env.Library('read_concern_args',
diff --git a/src/mongo/db/repl/initial_syncer_test.cpp b/src/mongo/db/repl/initial_syncer_test.cpp
index 7dc92186440..b1c14a8e096 100644
--- a/src/mongo/db/repl/initial_syncer_test.cpp
+++ b/src/mongo/db/repl/initial_syncer_test.cpp
@@ -46,7 +46,6 @@
#include "mongo/db/repl/data_replicator_external_state_mock.h"
#include "mongo/db/repl/initial_syncer.h"
#include "mongo/db/repl/member_state.h"
-#include "mongo/db/repl/mock_fixture.h"
#include "mongo/db/repl/oplog_entry.h"
#include "mongo/db/repl/oplog_fetcher.h"
#include "mongo/db/repl/oplog_fetcher_mock.h"
@@ -65,6 +64,7 @@
#include "mongo/db/repl/update_position_args.h"
#include "mongo/db/service_context_test_fixture.h"
#include "mongo/db/storage/storage_engine_mock.h"
+#include "mongo/executor/mock_network_fixture.h"
#include "mongo/executor/network_interface_mock.h"
#include "mongo/executor/thread_pool_task_executor_test_fixture.h"
#include "mongo/platform/mutex.h"
diff --git a/src/mongo/db/repl/replication_coordinator_impl_elect_v1_test.cpp b/src/mongo/db/repl/replication_coordinator_impl_elect_v1_test.cpp
index 89d5c946fa5..84b45c40fa5 100644
--- a/src/mongo/db/repl/replication_coordinator_impl_elect_v1_test.cpp
+++ b/src/mongo/db/repl/replication_coordinator_impl_elect_v1_test.cpp
@@ -35,7 +35,6 @@
#include "mongo/db/jsobj.h"
#include "mongo/db/operation_context_noop.h"
#include "mongo/db/repl/hello_response.h"
-#include "mongo/db/repl/mock_fixture.h"
#include "mongo/db/repl/repl_set_config.h"
#include "mongo/db/repl/repl_set_heartbeat_args_v1.h"
#include "mongo/db/repl/repl_set_heartbeat_response.h"
@@ -45,6 +44,7 @@
#include "mongo/db/repl/replication_metrics.h"
#include "mongo/db/repl/topology_coordinator.h"
#include "mongo/db/repl/vote_requester.h"
+#include "mongo/executor/mock_network_fixture.h"
#include "mongo/executor/network_interface_mock.h"
#include "mongo/logv2/log.h"
#include "mongo/unittest/death_test.h"
diff --git a/src/mongo/executor/SConscript b/src/mongo/executor/SConscript
index 600547a89b7..2cb463484bd 100644
--- a/src/mongo/executor/SConscript
+++ b/src/mongo/executor/SConscript
@@ -85,6 +85,7 @@ env.Library(
env.Library(
target='network_interface_mock',
source=[
+ 'mock_network_fixture.cpp',
'network_interface_mock.cpp',
'thread_pool_mock.cpp',
],
@@ -95,7 +96,10 @@ env.Library(
'$BUILD_DIR/mongo/util/net/network',
'network_interface',
'task_executor_interface',
- ]
+ ],
+ LIBDEPS_PRIVATE=[
+ '$BUILD_DIR/mongo/db/matcher/expressions',
+ ],
)
env.Library(
@@ -281,7 +285,9 @@ env.CppUnitTest(
source=[
'connection_pool_test.cpp',
'connection_pool_test_fixture.cpp',
+ 'mock_network_fixture_test.cpp',
'network_interface_mock_test.cpp',
+ 'network_interface_mock_test_fixture.cpp',
'scoped_task_executor_test.cpp',
'task_executor_cursor_test.cpp',
'thread_pool_task_executor_test.cpp',
diff --git a/src/mongo/db/repl/mock_fixture.cpp b/src/mongo/executor/mock_network_fixture.cpp
index a10e32aa475..da028d8a6ba 100644
--- a/src/mongo/db/repl/mock_fixture.cpp
+++ b/src/mongo/executor/mock_network_fixture.cpp
@@ -31,7 +31,7 @@
#include "mongo/platform/basic.h"
-#include "mongo/db/repl/mock_fixture.h"
+#include "mongo/executor/mock_network_fixture.h"
#include "mongo/db/matcher/matcher.h"
#include "mongo/executor/network_interface_mock.h"
diff --git a/src/mongo/db/repl/mock_fixture.h b/src/mongo/executor/mock_network_fixture.h
index a2628042097..a2628042097 100644
--- a/src/mongo/db/repl/mock_fixture.h
+++ b/src/mongo/executor/mock_network_fixture.h
diff --git a/src/mongo/executor/mock_network_fixture_test.cpp b/src/mongo/executor/mock_network_fixture_test.cpp
new file mode 100644
index 00000000000..f97b4e7f6eb
--- /dev/null
+++ b/src/mongo/executor/mock_network_fixture_test.cpp
@@ -0,0 +1,102 @@
+/**
+ * Copyright (C) 2020-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * 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
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * 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 Server Side 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.
+ */
+
+#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kTest
+
+#include "mongo/platform/basic.h"
+
+#include "mongo/base/status.h"
+#include "mongo/executor/mock_network_fixture.h"
+#include "mongo/executor/network_interface.h"
+#include "mongo/executor/network_interface_mock.h"
+#include "mongo/executor/network_interface_mock_test_fixture.h"
+#include "mongo/executor/thread_pool_mock.h"
+#include "mongo/logv2/log.h"
+#include "mongo/unittest/unittest.h"
+
+namespace mongo {
+namespace {
+
+using namespace executor;
+using namespace test::mock;
+
+class MockNetworkTest : public NetworkInterfaceMockTest {
+public:
+ MockNetworkTest() : NetworkInterfaceMockTest(), _mock(&NetworkInterfaceMockTest::net()){};
+
+ MockNetwork& mock() {
+ return _mock;
+ }
+
+ void setUp() override {
+ NetworkInterfaceMockTest::setUp();
+ NetworkInterfaceMockTest::startNetwork();
+ }
+
+ void tearDown() override {
+ NetworkInterfaceMockTest::tearDown();
+ // Will check for unsatisfied expectations.
+ mock().verifyAndClearExpectations();
+ }
+
+ void evaluateResponse(const RemoteCommandOnAnyResponse& resp, BSONObj expectedResponse) {
+ LOGV2(5015503, "Test got command response", "resp"_attr = resp);
+ ASSERT(resp.isOK());
+ ASSERT(SimpleBSONObjComparator::kInstance.evaluate(expectedResponse == resp.data));
+ }
+
+ std::string kExampleCmdName = "someCommandName";
+ RemoteCommandRequestOnAny kExampleRequest{
+ {testHost()}, "testDB", BSON(kExampleCmdName << 1), rpc::makeEmptyMetadata(), nullptr};
+ BSONObj kExampleResponse = BSON("some"
+ << "response");
+
+private:
+ MockNetwork _mock;
+};
+
+TEST_F(MockNetworkTest, MockFixtureBasicTest) {
+ mock().expect(kExampleCmdName, kExampleResponse);
+
+ RemoteCommandRequestOnAny request{kExampleRequest};
+ bool commandFinished = false;
+
+ TaskExecutor::CallbackHandle cb;
+ auto finishFn = [&](const RemoteCommandOnAnyResponse& resp) {
+ evaluateResponse(resp, kExampleResponse);
+ commandFinished = true;
+ };
+ ASSERT_OK(net().startCommand(cb, request, finishFn));
+
+ mock().runUntilExpectationsSatisfied();
+ ASSERT(commandFinished);
+}
+
+} // namespace
+} // namespace mongo \ No newline at end of file
diff --git a/src/mongo/executor/network_interface_mock_test.cpp b/src/mongo/executor/network_interface_mock_test.cpp
index d0575679cdd..c3f419a391f 100644
--- a/src/mongo/executor/network_interface_mock_test.cpp
+++ b/src/mongo/executor/network_interface_mock_test.cpp
@@ -36,68 +36,13 @@
#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/network_interface_mock_test_fixture.h"
#include "mongo/executor/test_network_connection_hook.h"
-#include "mongo/executor/thread_pool_mock.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();
- }
-
- RemoteCommandRequestOnAny kUnimportantRequest{
- {testHost()}, "testDB", BSON("test" << 1), rpc::makeEmptyMetadata(), nullptr};
-
-private:
- NetworkInterfaceMock _net;
- ThreadPoolMock _executor;
- bool _tearDownCalled;
-};
-
TEST_F(NetworkInterfaceMockTest, ConnectionHook) {
bool validateCalled = false;
bool hostCorrectForValidate = false;
diff --git a/src/mongo/executor/network_interface_mock_test_fixture.cpp b/src/mongo/executor/network_interface_mock_test_fixture.cpp
new file mode 100644
index 00000000000..0efae253dc8
--- /dev/null
+++ b/src/mongo/executor/network_interface_mock_test_fixture.cpp
@@ -0,0 +1,64 @@
+/**
+ * Copyright (C) 2020-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * 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
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * 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 Server Side 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 "mongo/executor/network_interface_mock_test_fixture.h"
+
+namespace mongo {
+namespace executor {
+
+// Intentionally not done in setUp in case there are methods that need to be called prior to
+// starting the network.
+void NetworkInterfaceMockTest::startNetwork() {
+ net().startup();
+ executor().startup();
+}
+
+void NetworkInterfaceMockTest::setUp() {
+ _tearDownCalled = false;
+}
+
+void NetworkInterfaceMockTest::tearDown() {
+ // 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();
+}
+} // namespace executor
+} // namespace mongo \ No newline at end of file
diff --git a/src/mongo/executor/network_interface_mock_test_fixture.h b/src/mongo/executor/network_interface_mock_test_fixture.h
new file mode 100644
index 00000000000..a19d2be80e4
--- /dev/null
+++ b/src/mongo/executor/network_interface_mock_test_fixture.h
@@ -0,0 +1,70 @@
+/**
+ * Copyright (C) 2020-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * 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
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * 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 Server Side 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.
+ */
+
+#pragma once
+
+#include "mongo/executor/network_interface_mock.h"
+#include "mongo/executor/thread_pool_mock.h"
+#include "mongo/unittest/unittest.h"
+
+namespace mongo {
+namespace executor {
+
+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};
+ }
+
+ void startNetwork();
+ virtual void setUp() override;
+ virtual void tearDown() override;
+
+ RemoteCommandRequestOnAny kUnimportantRequest{
+ {testHost()}, "testDB", BSON("test" << 1), rpc::makeEmptyMetadata(), nullptr};
+
+private:
+ NetworkInterfaceMock _net;
+ ThreadPoolMock _executor;
+ bool _tearDownCalled;
+};
+
+} // namespace executor
+} // namespace mongo \ No newline at end of file