summaryrefslogtreecommitdiff
path: root/src/mongo/client
diff options
context:
space:
mode:
authorRandolph Tan <randolph@10gen.com>2021-03-04 23:08:50 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-05-13 21:20:05 +0000
commitec6dffd110749232f15e0498e48e4f849cf7e6a0 (patch)
tree7f071f0f90813aea31af7413858f9ee2caa03dd1 /src/mongo/client
parentb28b53d23843c0e42e0751673c76088a3e7e60fa (diff)
downloadmongo-ec6dffd110749232f15e0498e48e4f849cf7e6a0.tar.gz
SERVER-54292 remove dependency on scanning replica set monitor in tenant_migration_recipient_service_tests
Diffstat (limited to 'src/mongo/client')
-rw-r--r--src/mongo/client/SConscript12
-rw-r--r--src/mongo/client/replica_set_monitor_manager.cpp6
-rw-r--r--src/mongo/client/replica_set_monitor_manager.h1
-rw-r--r--src/mongo/client/streamable_replica_set_monitor_for_testing.cpp79
-rw-r--r--src/mongo/client/streamable_replica_set_monitor_for_testing.h65
5 files changed, 163 insertions, 0 deletions
diff --git a/src/mongo/client/SConscript b/src/mongo/client/SConscript
index e120354b79c..589c74df1af 100644
--- a/src/mongo/client/SConscript
+++ b/src/mongo/client/SConscript
@@ -315,6 +315,18 @@ env.Library(
)
env.Library(
+ target='replica_set_monitor_test_helper',
+ source=[
+ 'streamable_replica_set_monitor_for_testing.cpp',
+ ],
+ LIBDEPS=[
+ '$BUILD_DIR/mongo/client/clientdriver_network',
+ '$BUILD_DIR/mongo/client/sdam/sdam_test',
+ '$BUILD_DIR/mongo/util/net/network',
+ ],
+)
+
+env.Library(
target='fetcher',
source=[
'fetcher.cpp',
diff --git a/src/mongo/client/replica_set_monitor_manager.cpp b/src/mongo/client/replica_set_monitor_manager.cpp
index 0d1d434734e..a21fc832d35 100644
--- a/src/mongo/client/replica_set_monitor_manager.cpp
+++ b/src/mongo/client/replica_set_monitor_manager.cpp
@@ -342,4 +342,10 @@ bool ReplicaSetMonitorManager::isShutdown() const {
void ReplicaSetMonitorConnectionManager::dropConnections(const HostAndPort& hostAndPort) {
_network->dropConnections(hostAndPort);
}
+
+void ReplicaSetMonitorManager::installMonitor_forTests(std::shared_ptr<ReplicaSetMonitor> monitor) {
+ stdx::lock_guard<Latch> lk(_mutex);
+ _monitors[monitor->getName()] = monitor;
+}
+
} // namespace mongo
diff --git a/src/mongo/client/replica_set_monitor_manager.h b/src/mongo/client/replica_set_monitor_manager.h
index bde3e41f815..9ce98024c76 100644
--- a/src/mongo/client/replica_set_monitor_manager.h
+++ b/src/mongo/client/replica_set_monitor_manager.h
@@ -156,6 +156,7 @@ public:
bool isShutdown() const;
+ void installMonitor_forTests(std::shared_ptr<ReplicaSetMonitor> monitor);
private:
/**
diff --git a/src/mongo/client/streamable_replica_set_monitor_for_testing.cpp b/src/mongo/client/streamable_replica_set_monitor_for_testing.cpp
new file mode 100644
index 00000000000..e0f582beed6
--- /dev/null
+++ b/src/mongo/client/streamable_replica_set_monitor_for_testing.cpp
@@ -0,0 +1,79 @@
+/**
+ * Copyright (C) 2021-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/client/streamable_replica_set_monitor_for_testing.h"
+
+#include "mongo/executor/network_interface_factory.h"
+#include "mongo/executor/network_interface_thread_pool.h"
+#include "mongo/executor/thread_pool_task_executor.h"
+#include "mongo/rpc/metadata/egress_metadata_hook_list.h"
+
+namespace mongo {
+
+StreamableReplicaSetMonitorForTesting::~StreamableReplicaSetMonitorForTesting() {
+ if (_taskExecutor) {
+ _taskExecutor->shutdown();
+ _taskExecutor->join();
+ }
+
+ if (_replSetMonitor) {
+ ReplicaSetMonitorManager::get()->removeMonitor(_replSetMonitor->getName());
+ }
+}
+
+void StreamableReplicaSetMonitorForTesting::setup(const MongoURI& uri) {
+ auto hookList = std::make_unique<rpc::EgressMetadataHookList>();
+ auto networkConnectionHook = std::make_unique<ReplicaSetMonitorManagerNetworkConnectionHook>();
+
+ std::shared_ptr<executor::NetworkInterface> networkInterface =
+ executor::makeNetworkInterface("ReplicaSetMonitor-TestTaskExecutor",
+ std::move(networkConnectionHook),
+ std::move(hookList));
+ _connectionManager = std::make_unique<ReplicaSetMonitorConnectionManager>(networkInterface);
+
+ auto pool = std::make_unique<executor::NetworkInterfaceThreadPool>(networkInterface.get());
+
+ _taskExecutor =
+ std::make_shared<executor::ThreadPoolTaskExecutor>(std::move(pool), networkInterface);
+ _taskExecutor->startup();
+
+ _replSetMonitor = std::make_shared<StreamableReplicaSetMonitor>(
+ uri, _taskExecutor, _connectionManager, [] {});
+ auto topologyManager = std::make_unique<sdam::MockTopologyManager>();
+ _topologyManagerPtr = topologyManager.get();
+
+ _replSetMonitor->initForTesting(std::move(topologyManager));
+ ReplicaSetMonitorManager::get()->installMonitor_forTests(_replSetMonitor);
+}
+
+sdam::MockTopologyManager* StreamableReplicaSetMonitorForTesting::getTopologyManager() {
+ return _topologyManagerPtr;
+}
+
+} // namespace mongo
diff --git a/src/mongo/client/streamable_replica_set_monitor_for_testing.h b/src/mongo/client/streamable_replica_set_monitor_for_testing.h
new file mode 100644
index 00000000000..bc481ad735e
--- /dev/null
+++ b/src/mongo/client/streamable_replica_set_monitor_for_testing.h
@@ -0,0 +1,65 @@
+/**
+ * Copyright (C) 2021-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/client/replica_set_monitor_manager.h"
+#include "mongo/client/sdam/mock_topology_manager.h"
+#include "mongo/client/streamable_replica_set_monitor.h"
+
+namespace mongo {
+
+/**
+ * Sets up a streamable replica set monitor for testing that does not automatically refresh on its
+ * own, and with configurable topology.
+ */
+class StreamableReplicaSetMonitorForTesting {
+public:
+ ~StreamableReplicaSetMonitorForTesting();
+
+ void setup(const MongoURI& uri);
+
+ sdam::MockTopologyManager* getTopologyManager();
+
+private:
+ // Executor for monitoring replica sets.
+ std::shared_ptr<executor::TaskExecutor> _taskExecutor;
+
+ // Allows closing connections established by the network interface associated with the
+ // _taskExecutor instance
+ std::shared_ptr<ReplicaSetMonitorConnectionManager> _connectionManager;
+
+ // Allows changing replica set topology for mocking replica set targetter response.
+ sdam::MockTopologyManager* _topologyManagerPtr;
+
+ // ReplicaSetMonitorManager only stores weak_ptrs, so we need to keep the monitor alive.
+ std::shared_ptr<StreamableReplicaSetMonitor> _replSetMonitor;
+};
+
+} // namespace mongo