summaryrefslogtreecommitdiff
path: root/src/mongo/db/sessions_collection_mock.cpp
diff options
context:
space:
mode:
authorSamantha Ritter <samantha.ritter@10gen.com>2017-06-02 13:57:56 -0400
committersamantharitter <samantha.ritter@10gen.com>2017-06-05 13:29:41 -0400
commit89e48e22ba667caad7223eaa2b6ee92868323268 (patch)
tree9f4da31a0c16f78b180df656fccfc60a1e5c7237 /src/mongo/db/sessions_collection_mock.cpp
parentc2293992d5672c7c7f1c5d94628924ea91a78316 (diff)
downloadmongo-89e48e22ba667caad7223eaa2b6ee92868323268.tar.gz
SERVER-28300 Implement mock libraries to test logical session cache
Diffstat (limited to 'src/mongo/db/sessions_collection_mock.cpp')
-rw-r--r--src/mongo/db/sessions_collection_mock.cpp158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/mongo/db/sessions_collection_mock.cpp b/src/mongo/db/sessions_collection_mock.cpp
new file mode 100644
index 00000000000..a56f6dec21a
--- /dev/null
+++ b/src/mongo/db/sessions_collection_mock.cpp
@@ -0,0 +1,158 @@
+/**
+ * Copyright (C) 2017 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/db/sessions_collection_mock.h"
+#include "mongo/platform/basic.h"
+#include "mongo/stdx/functional.h"
+
+namespace mongo {
+
+MockSessionsCollectionImpl::MockSessionsCollectionImpl()
+ : _sessions(),
+ _fetch(stdx::bind(&MockSessionsCollectionImpl::_fetchRecord, this, stdx::placeholders::_1)),
+ _insert(stdx::bind(&MockSessionsCollectionImpl::_insertRecord, this, stdx::placeholders::_1)),
+ _refresh(
+ stdx::bind(&MockSessionsCollectionImpl::_refreshSessions, this, stdx::placeholders::_1)),
+ _remove(
+ stdx::bind(&MockSessionsCollectionImpl::_removeRecords, this, stdx::placeholders::_1)) {}
+
+void MockSessionsCollectionImpl::setFetchHook(FetchHook hook) {
+ _fetch = std::move(hook);
+}
+
+void MockSessionsCollectionImpl::setInsertHook(InsertHook hook) {
+ _insert = std::move(hook);
+}
+
+void MockSessionsCollectionImpl::setRefreshHook(RefreshHook hook) {
+ _refresh = std::move(hook);
+}
+
+void MockSessionsCollectionImpl::setRemoveHook(RemoveHook hook) {
+ _remove = std::move(hook);
+}
+
+void MockSessionsCollectionImpl::clearHooks() {
+ _fetch = stdx::bind(&MockSessionsCollectionImpl::_fetchRecord, this, stdx::placeholders::_1);
+ _insert = stdx::bind(&MockSessionsCollectionImpl::_insertRecord, this, stdx::placeholders::_1);
+ _refresh =
+ stdx::bind(&MockSessionsCollectionImpl::_refreshSessions, this, stdx::placeholders::_1);
+ _remove = stdx::bind(&MockSessionsCollectionImpl::_removeRecords, this, stdx::placeholders::_1);
+}
+
+StatusWith<LogicalSessionRecord> MockSessionsCollectionImpl::fetchRecord(LogicalSessionId lsid) {
+ return _fetch(std::move(lsid));
+}
+
+Status MockSessionsCollectionImpl::insertRecord(LogicalSessionRecord record) {
+ return _insert(std::move(record));
+}
+
+MockSessionsCollectionImpl::SessionList MockSessionsCollectionImpl::refreshSessions(
+ SessionList sessions) {
+ return _refresh(std::move(sessions));
+}
+
+void MockSessionsCollectionImpl::removeRecords(SessionList sessions) {
+ _remove(std::move(sessions));
+}
+
+void MockSessionsCollectionImpl::add(LogicalSessionRecord record) {
+ stdx::unique_lock<stdx::mutex> lk(_mutex);
+ _sessions.insert({record.getLsid(), std::move(record)});
+}
+
+void MockSessionsCollectionImpl::remove(LogicalSessionId lsid) {
+ stdx::unique_lock<stdx::mutex> lk(_mutex);
+ _sessions.erase(lsid);
+}
+
+bool MockSessionsCollectionImpl::has(LogicalSessionId lsid) {
+ stdx::unique_lock<stdx::mutex> lk(_mutex);
+ return _sessions.find(lsid) != _sessions.end();
+}
+
+void MockSessionsCollectionImpl::clearSessions() {
+ stdx::unique_lock<stdx::mutex> lk(_mutex);
+ _sessions.clear();
+}
+
+const MockSessionsCollectionImpl::SessionMap& MockSessionsCollectionImpl::sessions() const {
+ return _sessions;
+}
+
+StatusWith<LogicalSessionRecord> MockSessionsCollectionImpl::_fetchRecord(LogicalSessionId lsid) {
+ stdx::unique_lock<stdx::mutex> lk(_mutex);
+
+ // If we do not have this record, return an error
+ auto it = _sessions.find(lsid);
+ if (it == _sessions.end()) {
+ return {ErrorCodes::NoSuchSession, "No matching record in the sessions collection"};
+ }
+
+ return it->second;
+}
+
+Status MockSessionsCollectionImpl::_insertRecord(LogicalSessionRecord record) {
+ stdx::unique_lock<stdx::mutex> lk(_mutex);
+ auto res = _sessions.insert({record.getLsid(), std::move(record)});
+
+ // We should never try to insert the same record twice. In theory this could
+ // happen because of a UUID conflict.
+ if (!res.second) {
+ return {ErrorCodes::DuplicateSession, "Session already exists in the sessions collection"};
+ }
+
+ return Status::OK();
+}
+
+MockSessionsCollectionImpl::SessionList MockSessionsCollectionImpl::_refreshSessions(
+ SessionList sessions) {
+ SessionList notFound{};
+
+ {
+ stdx::unique_lock<stdx::mutex> lk(_mutex);
+ for (auto& lsid : sessions) {
+ auto it = _sessions.find(lsid);
+ if (it == _sessions.end()) {
+ notFound.push_back(lsid);
+ }
+ }
+ }
+
+ return notFound;
+}
+
+void MockSessionsCollectionImpl::_removeRecords(SessionList sessions) {
+ stdx::unique_lock<stdx::mutex> lk(_mutex);
+ for (auto& lsid : sessions) {
+ _sessions.erase(lsid);
+ }
+}
+
+} // namespace mongo