summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Schwerin <schwerin@mongodb.com>2016-06-16 13:51:03 -0400
committerAndy Schwerin <schwerin@mongodb.com>2016-07-13 17:37:01 -0400
commit3f8990345ec18fe2f0316859231c2424e4355b95 (patch)
tree64f276053cfdd7cd7f96ad40081fa9c6a11ebdb6
parent9e7a9892b0c47c6bd9a7393ff4e378feac65db48 (diff)
downloadmongo-3f8990345ec18fe2f0316859231c2424e4355b95.tar.gz
SERVER-21004 Add setAlarm capability and virtualization flag to ClockSourceMock.
-rw-r--r--src/mongo/util/SConscript10
-rw-r--r--src/mongo/util/clock_source.h23
-rw-r--r--src/mongo/util/clock_source_mock.cpp35
-rw-r--r--src/mongo/util/clock_source_mock.h16
-rw-r--r--src/mongo/util/clock_source_mock_test.cpp169
5 files changed, 250 insertions, 3 deletions
diff --git a/src/mongo/util/SConscript b/src/mongo/util/SConscript
index b7767ee1563..31f2d017eda 100644
--- a/src/mongo/util/SConscript
+++ b/src/mongo/util/SConscript
@@ -162,6 +162,16 @@ env.Library(
)
env.CppUnitTest(
+ target='clock_source_mock_test',
+ source=[
+ 'clock_source_mock_test.cpp',
+ ],
+ LIBDEPS=[
+ 'clock_source_mock',
+ ],
+)
+
+env.CppUnitTest(
target='text_test',
source=[
'text_test.cpp'
diff --git a/src/mongo/util/clock_source.h b/src/mongo/util/clock_source.h
index 2c6ad1b6da3..c8f8d637f81 100644
--- a/src/mongo/util/clock_source.h
+++ b/src/mongo/util/clock_source.h
@@ -28,6 +28,7 @@
#pragma once
+#include "mongo/stdx/functional.h"
#include "mongo/util/time_support.h"
namespace mongo {
@@ -50,6 +51,28 @@ public:
* Returns the current wall clock time, as defined by this source.
*/
virtual Date_t now() = 0;
+
+ /**
+ * Schedules "action" to run sometime after this clock source reaches "when".
+ *
+ * Returns InternalError if this clock source does not implement setAlarm. May also
+ * return ShutdownInProgress during shutdown. Other errors are also allowed.
+ */
+ virtual Status setAlarm(Date_t when, stdx::function<void()> action) {
+ return {ErrorCodes::InternalError, "This clock source does not implement setAlarm."};
+ }
+
+ /**
+ * Returns true if this clock source (loosely) tracks the OS clock used for things
+ * like condition_variable::wait_until. Virtualized clocks used for testing return
+ * false here, and should provide an implementation for setAlarm, above.
+ */
+ bool tracksSystemClock() const {
+ return _tracksSystemClock;
+ }
+
+protected:
+ bool _tracksSystemClock = true;
};
} // namespace mongo
diff --git a/src/mongo/util/clock_source_mock.cpp b/src/mongo/util/clock_source_mock.cpp
index f2257bf2604..66879ca5438 100644
--- a/src/mongo/util/clock_source_mock.cpp
+++ b/src/mongo/util/clock_source_mock.cpp
@@ -29,7 +29,8 @@
#include "mongo/platform/basic.h"
#include "mongo/util/clock_source_mock.h"
-#include "mongo/util/time_support.h"
+
+#include <algorithm>
namespace mongo {
@@ -38,14 +39,46 @@ Milliseconds ClockSourceMock::getPrecision() {
}
Date_t ClockSourceMock::now() {
+ stdx::lock_guard<stdx::mutex> lk(_mutex);
return _now;
}
void ClockSourceMock::advance(Milliseconds ms) {
+ stdx::unique_lock<stdx::mutex> lk(_mutex);
_now += ms;
+ _processAlarms(std::move(lk));
}
void ClockSourceMock::reset(Date_t newNow) {
+ stdx::unique_lock<stdx::mutex> lk(_mutex);
_now = newNow;
+ _processAlarms(std::move(lk));
+}
+
+Status ClockSourceMock::setAlarm(Date_t when, stdx::function<void()> action) {
+ stdx::unique_lock<stdx::mutex> lk(_mutex);
+ if (when <= _now) {
+ lk.unlock();
+ action();
+ return Status::OK();
+ }
+ _alarms.emplace_back(std::make_pair(when, std::move(action)));
+ return Status::OK();
}
+
+void ClockSourceMock::_processAlarms(stdx::unique_lock<stdx::mutex> lk) {
+ using std::swap;
+ invariant(lk.owns_lock());
+ std::vector<Alarm> readyAlarms;
+ std::vector<Alarm>::iterator iter;
+ auto alarmIsNotExpired = [&](const Alarm& alarm) { return alarm.first > _now; };
+ auto expiredAlarmsBegin = std::partition(_alarms.begin(), _alarms.end(), alarmIsNotExpired);
+ std::move(expiredAlarmsBegin, _alarms.end(), std::back_inserter(readyAlarms));
+ _alarms.erase(expiredAlarmsBegin, _alarms.end());
+ lk.unlock();
+ for (const auto& alarm : readyAlarms) {
+ alarm.second();
+ }
+}
+
} // namespace mongo
diff --git a/src/mongo/util/clock_source_mock.h b/src/mongo/util/clock_source_mock.h
index 83af9cabb28..1c3f7e98f71 100644
--- a/src/mongo/util/clock_source_mock.h
+++ b/src/mongo/util/clock_source_mock.h
@@ -28,6 +28,10 @@
#pragma once
+#include <utility>
+#include <vector>
+
+#include "mongo/stdx/mutex.h"
#include "mongo/util/clock_source.h"
#include "mongo/util/time_support.h"
@@ -41,10 +45,13 @@ public:
/**
* Constructs a ClockSourceMock with the current time set to the Unix epoch.
*/
- ClockSourceMock() = default;
+ ClockSourceMock() {
+ _tracksSystemClock = false;
+ }
Milliseconds getPrecision() override;
- Date_t now() final;
+ Date_t now() override;
+ Status setAlarm(Date_t when, stdx::function<void()> action) override;
/**
* Advances the current time by the given value.
@@ -57,7 +64,12 @@ public:
void reset(Date_t newNow);
private:
+ using Alarm = std::pair<Date_t, stdx::function<void()>>;
+ void _processAlarms(stdx::unique_lock<stdx::mutex> lk);
+
+ stdx::mutex _mutex;
Date_t _now;
+ std::vector<Alarm> _alarms;
};
} // namespace mongo
diff --git a/src/mongo/util/clock_source_mock_test.cpp b/src/mongo/util/clock_source_mock_test.cpp
new file mode 100644
index 00000000000..3eb352b2532
--- /dev/null
+++ b/src/mongo/util/clock_source_mock_test.cpp
@@ -0,0 +1,169 @@
+/**
+ * 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/unittest/unittest.h"
+#include "mongo/util/clock_source_mock.h"
+
+namespace mongo {
+namespace {
+
+TEST(ClockSourceMockTest, ClockSourceShouldReportThatItIsNotSystemClock) {
+ ClockSourceMock cs;
+ ASSERT(!cs.tracksSystemClock());
+}
+
+TEST(ClockSourceMockTest, ExpiredAlarmExecutesWhenSet) {
+ ClockSourceMock cs;
+ int alarmFiredCount = 0;
+ const Date_t alarmDate = cs.now();
+ const auto alarmAction = [&] { ++alarmFiredCount; };
+ ASSERT_OK(cs.setAlarm(alarmDate, alarmAction));
+ ASSERT_EQ(1, alarmFiredCount) << cs.now();
+ alarmFiredCount = 0;
+ cs.advance(Seconds{1});
+ ASSERT_EQ(0, alarmFiredCount) << cs.now();
+ ASSERT_OK(cs.setAlarm(alarmDate, alarmAction));
+ ASSERT_EQ(1, alarmFiredCount) << cs.now();
+}
+
+TEST(ClockSourceMockTest, AlarmExecutesAfterExpirationUsingAdvance) {
+ ClockSourceMock cs;
+ int alarmFiredCount = 0;
+ const Date_t alarmDate = cs.now() + Seconds{10};
+ const auto alarmAction = [&] { ++alarmFiredCount; };
+ ASSERT_OK(cs.setAlarm(alarmDate, alarmAction));
+ ASSERT_EQ(0, alarmFiredCount) << cs.now();
+ cs.advance(Seconds{8});
+ ASSERT_EQ(0, alarmFiredCount) << cs.now();
+ cs.advance(Seconds{1});
+ ASSERT_EQ(0, alarmFiredCount) << cs.now();
+ cs.advance(Seconds{20});
+ ASSERT_EQ(1, alarmFiredCount) << cs.now();
+ cs.advance(Seconds{1});
+ ASSERT_EQ(1, alarmFiredCount) << cs.now();
+}
+
+TEST(ClockSourceMockTest, AlarmExecutesAfterExpirationUsingReset) {
+ ClockSourceMock cs;
+ int alarmFiredCount = 0;
+ const Date_t startDate = cs.now();
+ const Date_t alarmDate = startDate + Seconds{10};
+ const auto alarmAction = [&] { ++alarmFiredCount; };
+ ASSERT_OK(cs.setAlarm(alarmDate, alarmAction));
+ ASSERT_EQ(0, alarmFiredCount) << cs.now();
+ cs.reset(startDate + Seconds{8});
+ ASSERT_EQ(0, alarmFiredCount) << cs.now();
+ cs.reset(startDate + Seconds{9});
+ ASSERT_EQ(0, alarmFiredCount) << cs.now();
+ cs.reset(startDate + Seconds{20});
+ ASSERT_EQ(1, alarmFiredCount) << cs.now();
+ cs.reset(startDate + Seconds{21});
+ ASSERT_EQ(1, alarmFiredCount) << cs.now();
+}
+
+TEST(ClockSourceMockTest, MultipleAlarmsWithSameDeadlineTriggeredAtSameTime) {
+ ClockSourceMock cs;
+ int alarmFiredCount = 0;
+ const Date_t alarmDate = cs.now() + Seconds{10};
+ const auto alarmAction = [&] { ++alarmFiredCount; };
+ ASSERT_OK(cs.setAlarm(alarmDate, alarmAction));
+ ASSERT_OK(cs.setAlarm(alarmDate, alarmAction));
+ ASSERT_EQ(0, alarmFiredCount) << cs.now();
+ cs.advance(Seconds{20});
+ ASSERT_EQ(2, alarmFiredCount) << cs.now();
+}
+
+TEST(ClockSourceMockTest, MultipleAlarmsWithDifferentDeadlineTriggeredAtSameTime) {
+ ClockSourceMock cs;
+ int alarmFiredCount = 0;
+ const auto alarmAction = [&] { ++alarmFiredCount; };
+ ASSERT_OK(cs.setAlarm(cs.now() + Seconds{1}, alarmAction));
+ ASSERT_OK(cs.setAlarm(cs.now() + Seconds{10}, alarmAction));
+ ASSERT_EQ(0, alarmFiredCount) << cs.now();
+ cs.advance(Seconds{20});
+ ASSERT_EQ(2, alarmFiredCount) << cs.now();
+}
+
+TEST(ClockSourceMockTest, MultipleAlarmsWithDifferentDeadlineTriggeredAtDifferentTimes) {
+ ClockSourceMock cs;
+ int alarmFiredCount = 0;
+ const auto alarmAction = [&] { ++alarmFiredCount; };
+ ASSERT_OK(cs.setAlarm(cs.now() + Seconds{1}, alarmAction));
+ ASSERT_OK(cs.setAlarm(cs.now() + Seconds{10}, alarmAction));
+ ASSERT_EQ(0, alarmFiredCount) << cs.now();
+ cs.advance(Seconds{5});
+ ASSERT_EQ(1, alarmFiredCount) << cs.now();
+ cs.advance(Seconds{5});
+ ASSERT_EQ(2, alarmFiredCount) << cs.now();
+}
+
+TEST(ClockSourceMockTest, AlarmScheudlesExpiredAlarmWhenSignaled) {
+ ClockSourceMock cs;
+ const auto beginning = cs.now();
+ int alarmFiredCount = 0;
+ ASSERT_OK(cs.setAlarm(beginning + Seconds{1},
+ [&] {
+ ++alarmFiredCount;
+ ASSERT_OK(cs.setAlarm(beginning, [&] { ++alarmFiredCount; }));
+ }));
+ ASSERT_EQ(0, alarmFiredCount);
+ cs.advance(Seconds{1});
+ ASSERT_EQ(2, alarmFiredCount);
+}
+
+TEST(ClockSourceMockTest, ExpiredAlarmScheudlesExpiredAlarm) {
+ ClockSourceMock cs;
+ const auto beginning = cs.now();
+ int alarmFiredCount = 0;
+ ASSERT_OK(cs.setAlarm(beginning, [&] {
+ ++alarmFiredCount;
+ ASSERT_OK(cs.setAlarm(beginning, [&] { ++alarmFiredCount; }));
+ }));
+ ASSERT_EQ(2, alarmFiredCount);
+}
+
+TEST(ClockSourceMockTest, AlarmScheudlesAlarmWhenSignaled) {
+ ClockSourceMock cs;
+ const auto beginning = cs.now();
+ int alarmFiredCount = 0;
+ ASSERT_OK(cs.setAlarm(beginning + Seconds{1},
+ [&] {
+ ++alarmFiredCount;
+ ASSERT_OK(
+ cs.setAlarm(beginning + Seconds{2}, [&] { ++alarmFiredCount; }));
+ }));
+ ASSERT_EQ(0, alarmFiredCount);
+ cs.advance(Seconds{1});
+ ASSERT_EQ(1, alarmFiredCount);
+ cs.advance(Seconds{1});
+ ASSERT_EQ(2, alarmFiredCount);
+}
+}
+} // namespace mongo