summaryrefslogtreecommitdiff
path: root/src/mongo/db/keys_collection_manager_sharding_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/keys_collection_manager_sharding_test.cpp')
-rw-r--r--src/mongo/db/keys_collection_manager_sharding_test.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/mongo/db/keys_collection_manager_sharding_test.cpp b/src/mongo/db/keys_collection_manager_sharding_test.cpp
index 14b37758089..8bde5344ea6 100644
--- a/src/mongo/db/keys_collection_manager_sharding_test.cpp
+++ b/src/mongo/db/keys_collection_manager_sharding_test.cpp
@@ -395,4 +395,31 @@ TEST_F(KeysManagerShardedTest, ShouldNotReturnKeysInFeatureCompatibilityVersion3
ASSERT_EQ(ErrorCodes::KeyNotFound, keyStatus.getStatus());
}
+LogicalTime addSeconds(const LogicalTime& logicalTime, const Seconds& seconds) {
+ auto asTimestamp = logicalTime.asTimestamp();
+ return LogicalTime(Timestamp(asTimestamp.getSecs() + seconds.count(), asTimestamp.getInc()));
+}
+
+TEST(KeysCollectionManagerUtilTest, HowMuchSleepNeededForCalculationDoesNotOverflow) {
+ auto secondsSinceEpoch = durationCount<Seconds>(Date_t::now().toDurationSinceEpoch());
+ auto defaultKeysIntervalSeconds = Seconds(KeysRotationIntervalSec);
+
+ // Mock inputs that would have caused an overflow without the changes from SERVER-48709.
+ // "currentTime" is the current logical time in the LogicalClock, which will typically be close
+ // to a timestamp constructed from the number of seconds since the unix epoch. "latestExpiredAt"
+ // is the highest expiration logical time of any key, which will at most be currentTime +
+ // (default key rotation interval * 2) because two valid keys are kept at a time. "interval" is
+ // the duration a key is valid for, which defaults to 90 days = 7,776,000 seconds.
+ auto currentTime = LogicalTime(Timestamp(secondsSinceEpoch, 0));
+ auto latestExpiredAt = addSeconds(currentTime, defaultKeysIntervalSeconds * 2);
+ auto interval = Milliseconds(defaultKeysIntervalSeconds);
+
+ // Despite the default rotation interval seconds * 1000 not fitting in a 32 bit unsigned
+ // integer (7,776,000,000 vs. 4,294,967,295), the calculation should not overflow, and the next
+ // wakeup should correctly be the default interval.
+ auto nextWakeupMillis =
+ keys_collection_manager_util::howMuchSleepNeedFor(currentTime, latestExpiredAt, interval);
+ ASSERT_EQ(nextWakeupMillis, interval);
+}
+
} // namespace mongo