summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsamantharitter <samantha.ritter@10gen.com>2016-05-16 13:39:05 -0400
committersamantharitter <samantha.ritter@10gen.com>2016-05-31 12:05:23 -0400
commit513e15ce0ac1bf5f0d1f8a58f85cf89e6032eecf (patch)
treecc055614bfcd659b894aea8726c03322878554ca /src
parent427ef58a99f8d8b1cee76c89fc0ad7b47b661edb (diff)
downloadmongo-513e15ce0ac1bf5f0d1f8a58f85cf89e6032eecf.tar.gz
SERVER-24080 Implement and test mock ServiceEntryPoint
Diffstat (limited to 'src')
-rw-r--r--src/mongo/transport/SConscript26
-rw-r--r--src/mongo/transport/service_entry_point_mock.cpp119
-rw-r--r--src/mongo/transport/service_entry_point_mock.h79
-rw-r--r--src/mongo/transport/service_entry_point_mock_test.cpp65
4 files changed, 289 insertions, 0 deletions
diff --git a/src/mongo/transport/SConscript b/src/mongo/transport/SConscript
index 04d780779fc..3db057c9dba 100644
--- a/src/mongo/transport/SConscript
+++ b/src/mongo/transport/SConscript
@@ -11,3 +11,29 @@ env.CppUnitTest(
'$BUILD_DIR/mongo/util/foundation',
],
)
+
+env.Library(
+ target='service_entry_point_test_suite',
+ source=[
+ 'service_entry_point_test_suite.cpp',
+ ## move these somewhere better:
+ 'session.cpp',
+ 'ticket.cpp',
+ 'transport_layer.cpp',
+ ],
+ LIBDEPS=[
+ '$BUILD_DIR/mongo/util/foundation',
+ '$BUILD_DIR/mongo/util/net/network',
+ ],
+)
+
+env.CppUnitTest(
+ target='service_entry_point_mock_test',
+ source=[
+ 'service_entry_point_mock.cpp', ## move?
+ 'service_entry_point_mock_test.cpp',
+ ],
+ LIBDEPS=[
+ 'service_entry_point_test_suite',
+ ],
+)
diff --git a/src/mongo/transport/service_entry_point_mock.cpp b/src/mongo/transport/service_entry_point_mock.cpp
new file mode 100644
index 00000000000..778bfcc3758
--- /dev/null
+++ b/src/mongo/transport/service_entry_point_mock.cpp
@@ -0,0 +1,119 @@
+/**
+ * Copyright (C) 2016 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 "mongo/transport/service_entry_point_mock.h"
+
+#include <vector>
+
+#include "mongo/bson/bsonmisc.h"
+#include "mongo/bson/bsonobjbuilder.h"
+#include "mongo/stdx/functional.h"
+#include "mongo/stdx/thread.h"
+#include "mongo/transport/transport_layer.h"
+
+namespace mongo {
+
+using namespace transport;
+
+namespace {
+void setOkResponse(Message* m) {
+ // Need to set up our { ok : 1 } response.
+ BufBuilder b{};
+
+ // Leave room for the message header
+ b.skip(mongo::MsgData::MsgDataHeaderSize);
+
+ // Add our response
+ auto okObj = BSON("ok" << 1.0);
+ okObj.appendSelfToBufBuilder(b);
+
+ // Add some metadata
+ auto metadata = BSONObj();
+ metadata.appendSelfToBufBuilder(b);
+
+ // Set Message header fields
+ MsgData::View msg = b.buf();
+ msg.setLen(b.len());
+ msg.setOperation(dbCommandReply);
+
+ // Set the message, transfer buffer ownership to Message
+ m->reset();
+ m->setData(msg.view2ptr(), true);
+ b.decouple();
+}
+
+} // namespace
+
+ServiceEntryPointMock::ServiceEntryPointMock(transport::TransportLayer* tl)
+ : _tl(tl), _outMessage(), _inShutdown(false) {
+ setOkResponse(&_outMessage);
+}
+
+ServiceEntryPointMock::~ServiceEntryPointMock() {
+ {
+ stdx::lock_guard<stdx::mutex> lk(_shutdownLock);
+ _inShutdown = true;
+ }
+
+ for (auto& t : _threads) {
+ t.join();
+ }
+}
+
+void ServiceEntryPointMock::startSession(transport::Session&& session) {
+ _threads.emplace_back(&ServiceEntryPointMock::run, this, std::move(session));
+}
+
+void ServiceEntryPointMock::run(transport::Session&& session) {
+ Message inMessage;
+ while (true) {
+ {
+ stdx::lock_guard<stdx::mutex> lk(_shutdownLock);
+ if (_inShutdown)
+ break;
+ }
+
+ // sourceMessage()
+ auto sourceTicket = _tl->sourceMessage(session, &inMessage);
+ auto sourceRes = _tl->wait(std::move(sourceTicket));
+ if (!sourceRes.isOK()) {
+ break;
+ }
+
+ // sinkMessage()
+ auto sinkTicket = _tl->sinkMessage(session, _outMessage);
+ auto sinkRes = _tl->wait(std::move(sinkTicket));
+ if (!sinkRes.isOK()) {
+ break;
+ }
+ }
+}
+
+} // namespace mongo
diff --git a/src/mongo/transport/service_entry_point_mock.h b/src/mongo/transport/service_entry_point_mock.h
new file mode 100644
index 00000000000..582de6178d1
--- /dev/null
+++ b/src/mongo/transport/service_entry_point_mock.h
@@ -0,0 +1,79 @@
+/**
+ * Copyright (C) 2016 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.
+ */
+
+#pragma once
+
+#include "mongo/base/disallow_copying.h"
+#include "mongo/stdx/mutex.h"
+#include "mongo/stdx/thread.h"
+#include "mongo/transport/service_entry_point.h"
+#include "mongo/util/net/message.h"
+
+namespace mongo {
+
+namespace transport {
+
+class Session;
+class TransportLayer;
+
+} // namespace transport
+
+class ServiceEntryPointMock : public ServiceEntryPoint {
+ MONGO_DISALLOW_COPYING(ServiceEntryPointMock);
+
+public:
+ ServiceEntryPointMock(transport::TransportLayer* tl);
+
+ virtual ~ServiceEntryPointMock();
+
+ /**
+ * This method will spawn a thread that will do the following:
+ *
+ * - call tl->sourceMessage()
+ * - call tl->wait()
+ * - call tl->sinkMessage() with { ok : 1 }
+ * - call tl->wait()
+ *
+ * ...repeat until wait() returns an error.
+ */
+ void startSession(transport::Session&& session) override;
+
+private:
+ void run(transport::Session&& session);
+
+ transport::TransportLayer* _tl;
+
+ Message _outMessage;
+
+ stdx::mutex _shutdownLock;
+ bool _inShutdown;
+
+ std::vector<stdx::thread> _threads;
+};
+
+} // namespace mongo
diff --git a/src/mongo/transport/service_entry_point_mock_test.cpp b/src/mongo/transport/service_entry_point_mock_test.cpp
new file mode 100644
index 00000000000..c996c446b37
--- /dev/null
+++ b/src/mongo/transport/service_entry_point_mock_test.cpp
@@ -0,0 +1,65 @@
+/**
+ * Copyright (C) 2016 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 "mongo/stdx/memory.h"
+#include "mongo/transport/service_entry_point.h"
+#include "mongo/transport/service_entry_point_mock.h"
+#include "mongo/transport/service_entry_point_test_suite.h"
+#include "mongo/transport/session.h"
+#include "mongo/transport/ticket.h"
+#include "mongo/unittest/unittest.h"
+
+namespace mongo {
+
+namespace {
+
+std::unique_ptr<ServiceEntryPoint> mockSEPFactory(transport::TransportLayer* tl) {
+ return stdx::make_unique<ServiceEntryPointMock>(tl);
+}
+
+} // namespace
+
+TEST_F(ServiceEntryPointTestSuite, ServiceEntryPointMockTest) {
+ setServiceEntryPoint(&mockSEPFactory);
+
+ // Lifecycle tests
+ noLifeCycleTest();
+ halfLifeCycleTest();
+ fullLifeCycleTest();
+
+ // Concurrent session tests
+ interruptingSessionTest();
+
+ // Stress tests
+ burstStressTest();
+ longSessionStressTest();
+}
+
+} // namespace mongo