PM: Eliminate use of Time::Now() in unit tests.
The goal here is to make test inputs entirely deterministic. Since unit
tests are used for qualifying new CLs through the CQ, we want them to be
predictable and easily reproducible.
BUG=None
TEST=Unit tests.
Change-Id: I504db1149ac4c7d7206551bbc4aa562ba8a2687d
Reviewed-on: https://chromium-review.googlesource.com/190488
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
diff --git a/policy_manager/real_shill_provider_unittest.cc b/policy_manager/real_shill_provider_unittest.cc
index 855c154..da1477a 100644
--- a/policy_manager/real_shill_provider_unittest.cc
+++ b/policy_manager/real_shill_provider_unittest.cc
@@ -62,7 +62,7 @@
virtual void SetUp() {
provider_.reset(new RealShillProvider(&mock_dbus_, &fake_clock_));
PMTEST_ASSERT_NOT_NULL(provider_.get());
- fake_clock_.SetWallclockTime(init_time_);
+ fake_clock_.SetWallclockTime(InitTime());
// A DBus connection should only be obtained once.
EXPECT_CALL(mock_dbus_, BusGet(_, _)).WillOnce(
Return(kFakeConnection));
@@ -107,6 +107,24 @@
testing::Mock::VerifyAndClear(&mock_dbus_);
}
+ // These methods generate fixed timestamps for use in faking the current time.
+ Time InitTime() {
+ Time::Exploded now_exp;
+ now_exp.year = 2014;
+ now_exp.month = 3;
+ now_exp.day_of_week = 2;
+ now_exp.day_of_month = 18;
+ now_exp.hour = 8;
+ now_exp.minute = 5;
+ now_exp.second = 33;
+ now_exp.millisecond = 675;
+ return Time::FromLocalExploded(now_exp);
+ }
+
+ Time ConnChangedTime() {
+ return InitTime() + TimeDelta::FromSeconds(10);
+ }
+
// Sets up a mock "GetProperties" call on |proxy| that returns a hash table
// containing |num_entries| entries formed by subsequent key/value pairs. Keys
// and values are plain C strings (const char*). Mock will be expected to call
@@ -151,7 +169,7 @@
auto callback = reinterpret_cast<ShillConnector::PropertyChangedHandler>(
signal_handler_);
auto default_service_gval = GValueNewString(service_path);
- Time conn_change_time = Time::Now();
+ Time conn_change_time = ConnChangedTime();
fake_clock_.SetWallclockTime(conn_change_time);
callback(kFakeManagerProxy, shill::kDefaultServiceProperty,
default_service_gval, signal_data_);
@@ -200,7 +218,6 @@
}
const TimeDelta default_timeout_ = TimeDelta::FromSeconds(1);
- const Time init_time_ = Time::Now();
StrictMock<MockDBusWrapper> mock_dbus_;
FakeClock fake_clock_;
scoped_ptr<RealShillProvider> provider_;
@@ -225,7 +242,7 @@
scoped_ptr<const Time> conn_last_changed(
provider_->var_conn_last_changed()->GetValue(default_timeout_, NULL));
PMTEST_ASSERT_NOT_NULL(conn_last_changed.get());
- EXPECT_EQ(init_time_, *conn_last_changed);
+ EXPECT_EQ(InitTime(), *conn_last_changed);
}
// Test that Ethernet connection is identified correctly.
@@ -282,7 +299,7 @@
auto callback = reinterpret_cast<ShillConnector::PropertyChangedHandler>(
signal_handler_);
auto default_service_gval = GValueNewString(kFakeVpnServicePath);
- Time conn_change_time = Time::Now();
+ Time conn_change_time = ConnChangedTime();
fake_clock_.SetWallclockTime(conn_change_time);
callback(kFakeManagerProxy, shill::kDefaultServiceProperty,
default_service_gval, signal_data_);
@@ -326,7 +343,7 @@
auto callback = reinterpret_cast<ShillConnector::PropertyChangedHandler>(
signal_handler_);
auto default_service_gval = GValueNewString(kFakeEthernetServicePath);
- Time conn_change_time = Time::Now();
+ Time conn_change_time = ConnChangedTime();
fake_clock_.SetWallclockTime(conn_change_time);
callback(kFakeManagerProxy, shill::kDefaultServiceProperty,
default_service_gval, signal_data_);