summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2022-08-25 13:11:17 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-08-31 10:59:31 +0000
commit559d9f496e6e3ee5262e64cf3d00f5f741239d4b (patch)
tree8ee12e03ad80b1e2858b59ec4f2cc0112c3d6f34
parentda44564ea5010bcae4e65e789799012784306c4a (diff)
downloadmongo-559d9f496e6e3ee5262e64cf3d00f5f741239d4b.tar.gz
SERVER-68477 add unit test for TTLCollectionCache
(cherry picked from commit f32f550aff1def5709ea68f42dc9d4bf9321d5e6)
-rw-r--r--src/mongo/db/SConscript2
-rw-r--r--src/mongo/db/ttl_collection_cache_test.cpp90
2 files changed, 92 insertions, 0 deletions
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript
index f677e021b30..a9404ebedfa 100644
--- a/src/mongo/db/SConscript
+++ b/src/mongo/db/SConscript
@@ -2517,6 +2517,7 @@ if wiredtiger:
'transaction/transaction_history_iterator_test.cpp',
'transaction/transaction_participant_retryable_writes_test.cpp',
'transaction/transaction_participant_test.cpp',
+ 'ttl_collection_cache_test.cpp',
'ttl_test.cpp',
'update_index_data_test.cpp',
'vector_clock_mongod_test.cpp',
@@ -2617,6 +2618,7 @@ if wiredtiger:
'snapshot_window_options',
'startup_warnings_mongod',
'time_proof_service',
+ 'ttl_collection_cache',
'ttl_d',
'update_index_data',
'vector_clock',
diff --git a/src/mongo/db/ttl_collection_cache_test.cpp b/src/mongo/db/ttl_collection_cache_test.cpp
new file mode 100644
index 00000000000..4f598b3736c
--- /dev/null
+++ b/src/mongo/db/ttl_collection_cache_test.cpp
@@ -0,0 +1,90 @@
+/**
+ * Copyright (C) 2022-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/db/ttl_collection_cache.h"
+#include "mongo/unittest/unittest.h"
+
+namespace mongo {
+namespace {
+
+TEST(TTLCollectionCacheTest, Basic) {
+ TTLCollectionCache cache;
+ ASSERT_EQ(cache.getTTLInfos().size(), 0);
+
+ auto uuidCollA = UUID::gen();
+ auto uuidCollB = UUID::gen();
+ auto infoIndexA1 = TTLCollectionCache::Info{"collA_ttl_1"};
+ auto infoClusteredA = TTLCollectionCache::Info{TTLCollectionCache::ClusteredId{}};
+ auto infoIndexB1 = TTLCollectionCache::Info("collB_ttl_1");
+
+ // Confirm registerTTLInfo() behavior using getTTLInfo().
+ cache.registerTTLInfo(uuidCollA, infoIndexA1);
+ cache.registerTTLInfo(uuidCollA, infoClusteredA);
+ cache.registerTTLInfo(uuidCollB, infoIndexB1);
+
+ auto infos = cache.getTTLInfos();
+ ASSERT_EQ(infos.size(), 2U);
+ ASSERT_EQ(infos.count(uuidCollA), 1U);
+ ASSERT_EQ(infos[uuidCollA].size(), 2U);
+ auto indexNameA = stdx::get_if<TTLCollectionCache::IndexName>(&infos[uuidCollA][0]);
+ ASSERT(indexNameA);
+ ASSERT_EQ(*indexNameA, "collA_ttl_1");
+ ASSERT(stdx::get_if<TTLCollectionCache::ClusteredId>(&infos[uuidCollA][1]));
+
+ ASSERT_EQ(infos.count(uuidCollB), 1U);
+ ASSERT_EQ(infos[uuidCollB].size(), 1U);
+
+ auto indexNameB = stdx::get_if<TTLCollectionCache::IndexName>(&infos[uuidCollB][0]);
+ ASSERT(indexNameB);
+ ASSERT_EQ(*indexNameB, "collB_ttl_1");
+
+ // Check deregisterTTLInfo(). TTLCollectionCache should clean up
+ // UUIDs that no longer have any TTL infos registered.
+ cache.deregisterTTLInfo(uuidCollB, infoIndexB1);
+ infos = cache.getTTLInfos();
+ ASSERT_EQ(infos.size(), 1U);
+ ASSERT_EQ(infos.count(uuidCollA), 1U);
+ ASSERT_EQ(infos[uuidCollA].size(), 2U);
+
+ // Remove info for TTL index on collection A.
+ cache.deregisterTTLInfo(uuidCollA, infoIndexA1);
+ infos = cache.getTTLInfos();
+ ASSERT_EQ(infos.size(), 1U);
+ ASSERT_EQ(infos.count(uuidCollA), 1U);
+ ASSERT_EQ(infos[uuidCollA].size(), 1U);
+ ASSERT(stdx::get_if<TTLCollectionCache::ClusteredId>(&infos[uuidCollA][0]));
+
+ // Remove clustered info for collection A.
+ cache.deregisterTTLInfo(uuidCollA, infoClusteredA);
+ infos = cache.getTTLInfos();
+ ASSERT_EQ(infos.size(), 0U);
+}
+
+} // namespace
+} // namespace mongo