Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1059)

Unified Diff: metrics_daemon_test.cc

Issue 2731008: Implement a persistent storage aggregation counter class. (Closed) Base URL: ssh://git@chromiumos-git/metrics.git
Patch Set: Address kmixter's comments. Created 10 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « metrics_daemon.cc ('k') | metrics_library_mock.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: metrics_daemon_test.cc
diff --git a/metrics_daemon_test.cc b/metrics_daemon_test.cc
index 49acd3164ad992d8a39ddd3970c4972b94692a1f..7119c8506f51332091b5b19357f1e261444bb52b 100644
--- a/metrics_daemon_test.cc
+++ b/metrics_daemon_test.cc
@@ -2,26 +2,19 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <gtest/gtest.h>
+
+#include "counter_mock.h"
#include "metrics_daemon.h"
#include "metrics_library_mock.h"
-#include <sys/file.h>
-
-#include <base/eintr_wrapper.h>
-#include <base/file_util.h>
-#include <base/logging.h>
-#include <base/string_util.h>
-#include <gtest/gtest.h>
-
using base::Time;
using base::TimeTicks;
-using ::testing::Mock;
+using chromeos_metrics::TaggedCounterMock;
+using ::testing::_;
using ::testing::Return;
using ::testing::StrictMock;
-static const char kTestDailyUseRecordFile[] = "daily-usage-test";
-static const char kDoesNotExistFile[] = "/does/not/exist";
-
static const int kSecondsPerDay = 24 * 60 * 60;
// This class allows a TimeTicks object to be initialized with seconds
@@ -48,46 +41,29 @@ static std::ostream& operator<<(std::ostream& o, const Time& time) {
class MetricsDaemonTest : public testing::Test {
protected:
virtual void SetUp() {
- EXPECT_EQ(NULL, daemon_.daily_use_record_file_);
+ EXPECT_EQ(NULL, daemon_.daily_use_.get());
daemon_.Init(true, &metrics_lib_);
- // Tests constructor initialization. Switches to a test daily use
- // record file.
- EXPECT_TRUE(NULL != daemon_.daily_use_record_file_);
- daemon_.daily_use_record_file_ = kTestDailyUseRecordFile;
- EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
- EXPECT_EQ(0, daemon_.daily_use_day_last_);
+ // Tests constructor initialization. Switches to mock counters.
+ EXPECT_TRUE(NULL != daemon_.daily_use_.get());
+ daily_use_ = new StrictMock<TaggedCounterMock>();
+ daemon_.daily_use_.reset(daily_use_); // Transfers ownership.
EXPECT_FALSE(daemon_.user_active_);
EXPECT_TRUE(daemon_.user_active_last_.is_null());
EXPECT_EQ(MetricsDaemon::kUnknownNetworkState, daemon_.network_state_);
EXPECT_TRUE(daemon_.network_state_last_.is_null());
EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_);
EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_);
-
- // The test fixture object will be used by the log message handler.
- daemon_test_ = this;
- logging::SetLogMessageHandler(HandleLogMessages);
- }
-
- virtual void TearDown() {
- logging::SetLogMessageHandler(NULL);
- daemon_test_ = NULL;
- file_util::Delete(FilePath(kTestDailyUseRecordFile), false);
}
- // Collects log messages in the |daemon_log_| member string so that
- // they can be analyzed for errors and expected behavior.
- static bool HandleLogMessages(int severity, const std::string& str) {
- daemon_test_->daemon_log_.append(str);
- daemon_test_->daemon_log_.append("\n");
-
- // Returning true would mute the log.
- return false;
- }
+ virtual void TearDown() {}
- // Returns true if the daemon log contains |pattern|, false otherwise.
- bool LogContains(const std::string& pattern) {
- return daemon_log_.find(pattern) != std::string::npos;
+ // Adds a daily use aggregation counter expectation that the
+ // specified tag/count update will be generated.
+ void ExpectDailyUseUpdate(int tag, int count) {
+ EXPECT_CALL(*daily_use_, Update(tag, count))
+ .Times(1)
+ .RetiresOnSaturation();
}
// Adds a metrics library mock expectation that the specified metric
@@ -118,55 +94,11 @@ class MetricsDaemonTest : public testing::Test {
MetricsDaemon::kMetricTimeToNetworkDropBuckets);
}
+ // Converts from seconds to a Time object.
Time TestTime(int64 seconds) {
return Time::FromInternalValue(seconds * Time::kMicrosecondsPerSecond);
}
- // Asserts that the daily use record file contains the specified
- // contents.
- testing::AssertionResult AssertDailyUseRecord(const char* expr_day,
- const char* expr_seconds,
- int expected_day,
- int expected_seconds) {
- int fd = HANDLE_EINTR(open(daemon_.daily_use_record_file_, O_RDONLY));
- if (fd < 0) {
- testing::Message msg;
- msg << "Unable to open " << daemon_.daily_use_record_file_;
- return testing::AssertionFailure(msg);
- }
-
- MetricsDaemon::UseRecord record;
- if (!file_util::ReadFromFD(fd, reinterpret_cast<char*>(&record),
- sizeof(record))) {
- testing::Message msg;
- msg << "Unable to read " << sizeof(record) << " bytes from "
- << daemon_.daily_use_record_file_;
- HANDLE_EINTR(close(fd));
- return testing::AssertionFailure(msg);
- }
-
- if (record.day_ != expected_day || record.seconds_ != expected_seconds) {
- testing::Message msg;
- msg << "actual use record (" << record.day_ << ", " << record.seconds_
- << ") expected (" << expected_day << ", " << expected_seconds << ")";
- HANDLE_EINTR(close(fd));
- return testing::AssertionFailure(msg);
- }
-
- HANDLE_EINTR(close(fd));
- return testing::AssertionSuccess();
- }
-
- // Returns true if the daily use record file does not exist or is
- // empty, false otherwise.
- bool AssertNoOrEmptyUseRecordFile() {
- FilePath record_file(daemon_.daily_use_record_file_);
- int64 record_file_size;
- return !file_util::PathExists(record_file) ||
- (file_util::GetFileSize(record_file, &record_file_size) &&
- record_file_size == 0);
- }
-
// Creates a new DBus signal message with a single string
// argument. The message can be deallocated through
// DeleteDBusMessage.
@@ -195,9 +127,6 @@ class MetricsDaemonTest : public testing::Test {
dbus_message_unref(msg);
}
- // Pointer to the current test fixture.
- static MetricsDaemonTest* daemon_test_;
-
// The MetricsDaemon under test.
MetricsDaemon daemon_;
@@ -205,67 +134,19 @@ class MetricsDaemonTest : public testing::Test {
// metric generation calls are marked as failures.
StrictMock<MetricsLibraryMock> metrics_lib_;
- // The accumulated metrics daemon log.
- std::string daemon_log_;
+ // Daily use time aggregation counter mock. It's a strict mock so
+ // that all unexpected update calls are marked as failures. It's a
+ // pointer so that it can replace the scoped_ptr allocated by the
+ // daemon.
+ StrictMock<TaggedCounterMock>* daily_use_;
};
-// static
-MetricsDaemonTest* MetricsDaemonTest::daemon_test_ = NULL;
-
-TEST_F(MetricsDaemonTest, LogDailyUseRecordBadFileLocation) {
- // Checks that the daemon doesn't die badly if the file can't be
- // created.
- daemon_.daily_use_record_file_ = kDoesNotExistFile;
- daemon_.LogDailyUseRecord(10, 20);
- EXPECT_TRUE(LogContains("Unable to open the daily use file: "
- "No such file or directory"));
- EXPECT_EQ(0, daemon_.daily_use_day_last_);
- file_util::Delete(FilePath(kDoesNotExistFile), false);
-}
-
-TEST_F(MetricsDaemonTest, LogDailyUseRecordOnLogin) {
- daemon_.LogDailyUseRecord(/* day */ 5, /* seconds */ 120);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 5, /* seconds */ 120);
- EXPECT_EQ(5, daemon_.daily_use_day_last_);
-
- daemon_.LogDailyUseRecord(/* day */ 5, /* seconds */ 0);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 5, /* seconds */ 120);
- EXPECT_EQ(5, daemon_.daily_use_day_last_);
-
- daemon_.LogDailyUseRecord(/* day */ 5, /* seconds */ 240);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 5, /* seconds */ 360);
- EXPECT_EQ(5, daemon_.daily_use_day_last_);
-
- ExpectDailyUseTimeMetric(/* sample */ 6);
- daemon_.LogDailyUseRecord(/* day */ 6, /* seconds */ 0);
- EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
- EXPECT_EQ(6, daemon_.daily_use_day_last_);
-}
-
-TEST_F(MetricsDaemonTest, LogDailyUseRecordRoundDown) {
- daemon_.LogDailyUseRecord(/* day */ 7, /* seconds */ 89);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 7, /* seconds */ 89);
- EXPECT_EQ(7, daemon_.daily_use_day_last_);
+TEST_F(MetricsDaemonTest, DailyUseReporter) {
+ ExpectDailyUseTimeMetric(/* sample */ 2);
+ MetricsDaemon::DailyUseReporter(&daemon_, /* tag */ 20, /* count */ 90);
ExpectDailyUseTimeMetric(/* sample */ 1);
- daemon_.LogDailyUseRecord(/* day */ 6, /* seconds */ 15);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 6, /* seconds */ 15);
- EXPECT_EQ(6, daemon_.daily_use_day_last_);
-}
-
-TEST_F(MetricsDaemonTest, LogDailyUseRecordRoundUp) {
- daemon_.LogDailyUseRecord(/* day */ 6, /* seconds */ 0);
- EXPECT_EQ(6, daemon_.daily_use_day_last_);
-
- // Tests rounding use time to the closest minute.
- daemon_.LogDailyUseRecord(/* day */ 6, /* seconds */ 90);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 6, /* seconds */ 90);
- EXPECT_EQ(6, daemon_.daily_use_day_last_);
-
- ExpectDailyUseTimeMetric(/* sample */ 2);
- daemon_.LogDailyUseRecord(/* day */ 7, /* seconds */ 89);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 7, /* seconds */ 89);
- EXPECT_EQ(7, daemon_.daily_use_day_last_);
+ MetricsDaemon::DailyUseReporter(&daemon_, /* tag */ 23, /* count */ 89);
}
TEST_F(MetricsDaemonTest, LookupNetworkState) {
@@ -322,6 +203,9 @@ TEST_F(MetricsDaemonTest, MessageFilter) {
EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res);
DeleteDBusMessage(msg);
+ EXPECT_CALL(*daily_use_, Update(_, 0))
+ .Times(1)
+ .RetiresOnSaturation();
msg = NewDBusSignalString("/",
"org.chromium.PowerManager",
"ScreenIsUnlocked",
@@ -332,6 +216,9 @@ TEST_F(MetricsDaemonTest, MessageFilter) {
EXPECT_EQ(DBUS_HANDLER_RESULT_HANDLED, res);
DeleteDBusMessage(msg);
+ EXPECT_CALL(*daily_use_, Update(_, 0))
+ .Times(1)
+ .RetiresOnSaturation();
msg = NewDBusSignalString("/org/chromium/SessionManager",
"org.chromium.SessionManagerInterface",
"SessionStateChanged",
@@ -396,30 +283,28 @@ TEST_F(MetricsDaemonTest, NetStateChangedSuspend) {
}
TEST_F(MetricsDaemonTest, PowerStateChanged) {
+ ExpectDailyUseUpdate(7, 0);
daemon_.SetUserActiveState(/* active */ true,
TestTime(7 * kSecondsPerDay + 15));
EXPECT_TRUE(daemon_.user_active_);
EXPECT_EQ(TestTime(7 * kSecondsPerDay + 15), daemon_.user_active_last_);
- EXPECT_EQ(7, daemon_.daily_use_day_last_);
- EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
+ ExpectDailyUseUpdate(7, 30);
daemon_.PowerStateChanged("mem", TestTime(7 * kSecondsPerDay + 45));
EXPECT_EQ(MetricsDaemon::kPowerStateMem, daemon_.power_state_);
EXPECT_FALSE(daemon_.user_active_);
EXPECT_EQ(TestTime(7 * kSecondsPerDay + 45), daemon_.user_active_last_);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 7, /* seconds */ 30);
daemon_.PowerStateChanged("on", TestTime(7 * kSecondsPerDay + 85));
EXPECT_EQ(MetricsDaemon::kPowerStateOn, daemon_.power_state_);
EXPECT_FALSE(daemon_.user_active_);
EXPECT_EQ(TestTime(7 * kSecondsPerDay + 45), daemon_.user_active_last_);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 7, /* seconds */ 30);
+ ExpectDailyUseUpdate(7, 0);
daemon_.PowerStateChanged("otherstate", TestTime(7 * kSecondsPerDay + 185));
EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_);
EXPECT_FALSE(daemon_.user_active_);
EXPECT_EQ(TestTime(7 * kSecondsPerDay + 185), daemon_.user_active_last_);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 7, /* seconds */ 30);
}
TEST_F(MetricsDaemonTest, SendMetric) {
@@ -429,117 +314,76 @@ TEST_F(MetricsDaemonTest, SendMetric) {
}
TEST_F(MetricsDaemonTest, SessionStateChanged) {
+ ExpectDailyUseUpdate(15, 0);
daemon_.SessionStateChanged("started", TestTime(15 * kSecondsPerDay + 20));
EXPECT_EQ(MetricsDaemon::kSessionStateStarted, daemon_.session_state_);
EXPECT_TRUE(daemon_.user_active_);
EXPECT_EQ(TestTime(15 * kSecondsPerDay + 20), daemon_.user_active_last_);
- EXPECT_EQ(15, daemon_.daily_use_day_last_);
- EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
+ ExpectDailyUseUpdate(15, 130);
daemon_.SessionStateChanged("stopped", TestTime(15 * kSecondsPerDay + 150));
EXPECT_EQ(MetricsDaemon::kSessionStateStopped, daemon_.session_state_);
EXPECT_FALSE(daemon_.user_active_);
EXPECT_EQ(TestTime(15 * kSecondsPerDay + 150), daemon_.user_active_last_);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 15, /* seconds */ 130);
+ ExpectDailyUseUpdate(15, 0);
daemon_.SessionStateChanged("otherstate",
TestTime(15 * kSecondsPerDay + 300));
EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_);
EXPECT_FALSE(daemon_.user_active_);
EXPECT_EQ(TestTime(15 * kSecondsPerDay + 300), daemon_.user_active_last_);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 15, /* seconds */ 130);
}
-TEST_F(MetricsDaemonTest, SetUserActiveStateSendOnLogin) {
+TEST_F(MetricsDaemonTest, SetUserActiveState) {
+ ExpectDailyUseUpdate(5, 0);
daemon_.SetUserActiveState(/* active */ false,
TestTime(5 * kSecondsPerDay + 10));
EXPECT_FALSE(daemon_.user_active_);
EXPECT_EQ(TestTime(5 * kSecondsPerDay + 10), daemon_.user_active_last_);
- EXPECT_EQ(5, daemon_.daily_use_day_last_);
- EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
+ ExpectDailyUseUpdate(6, 0);
daemon_.SetUserActiveState(/* active */ true,
TestTime(6 * kSecondsPerDay + 20));
EXPECT_TRUE(daemon_.user_active_);
EXPECT_EQ(TestTime(6 * kSecondsPerDay + 20), daemon_.user_active_last_);
- EXPECT_EQ(6, daemon_.daily_use_day_last_);
- EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
+ ExpectDailyUseUpdate(6, 100);
daemon_.SetUserActiveState(/* active */ true,
TestTime(6 * kSecondsPerDay + 120));
EXPECT_TRUE(daemon_.user_active_);
EXPECT_EQ(TestTime(6 * kSecondsPerDay + 120), daemon_.user_active_last_);
- EXPECT_EQ(6, daemon_.daily_use_day_last_);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 6, /* seconds */ 100);
+ ExpectDailyUseUpdate(6, 110);
daemon_.SetUserActiveState(/* active */ false,
- TestTime(6 * kSecondsPerDay + 220));
+ TestTime(6 * kSecondsPerDay + 230));
EXPECT_FALSE(daemon_.user_active_);
- EXPECT_EQ(TestTime(6 * kSecondsPerDay + 220), daemon_.user_active_last_);
- EXPECT_EQ(6, daemon_.daily_use_day_last_);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 6, /* seconds */ 200);
-
- ExpectDailyUseTimeMetric(/* sample */ 3);
- daemon_.SetUserActiveState(/* active */ true,
- TestTime(8 * kSecondsPerDay - 300));
- EXPECT_TRUE(daemon_.user_active_);
- EXPECT_EQ(TestTime(8 * kSecondsPerDay - 300), daemon_.user_active_last_);
- EXPECT_EQ(7, daemon_.daily_use_day_last_);
- EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
-}
-
-TEST_F(MetricsDaemonTest, SetUserActiveStateSendOnMonitor) {
- daemon_.SetUserActiveState(/* active */ true,
- TestTime(8 * kSecondsPerDay - 300));
- EXPECT_TRUE(daemon_.user_active_);
- EXPECT_EQ(TestTime(8 * kSecondsPerDay - 300), daemon_.user_active_last_);
- EXPECT_EQ(7, daemon_.daily_use_day_last_);
- EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
+ EXPECT_EQ(TestTime(6 * kSecondsPerDay + 230), daemon_.user_active_last_);
+ ExpectDailyUseUpdate(6, 0);
daemon_.SetUserActiveState(/* active */ false,
- TestTime(8 * kSecondsPerDay + 300));
+ TestTime(6 * kSecondsPerDay + 260));
EXPECT_FALSE(daemon_.user_active_);
- EXPECT_EQ(TestTime(8 * kSecondsPerDay + 300), daemon_.user_active_last_);
- EXPECT_EQ(8, daemon_.daily_use_day_last_);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 8, /* seconds */ 600);
-
- daemon_.SetUserActiveState(/* active */ true,
- TestTime(9 * kSecondsPerDay - 200));
- EXPECT_TRUE(daemon_.user_active_);
- EXPECT_EQ(TestTime(9 * kSecondsPerDay - 200), daemon_.user_active_last_);
- EXPECT_EQ(8, daemon_.daily_use_day_last_);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 8, /* seconds */ 600);
-
- ExpectDailyUseTimeMetric(/* sample */ 10);
- daemon_.SetUserActiveState(/* active */ true,
- TestTime(9 * kSecondsPerDay + 200));
- EXPECT_TRUE(daemon_.user_active_);
- EXPECT_EQ(TestTime(9 * kSecondsPerDay + 200), daemon_.user_active_last_);
- EXPECT_EQ(9, daemon_.daily_use_day_last_);
- EXPECT_PRED_FORMAT2(AssertDailyUseRecord, /* day */ 9, /* seconds */ 400);
+ EXPECT_EQ(TestTime(6 * kSecondsPerDay + 260), daemon_.user_active_last_);
}
TEST_F(MetricsDaemonTest, SetUserActiveStateTimeJump) {
+ ExpectDailyUseUpdate(10, 0);
daemon_.SetUserActiveState(/* active */ true,
TestTime(10 * kSecondsPerDay + 500));
EXPECT_TRUE(daemon_.user_active_);
EXPECT_EQ(TestTime(10 * kSecondsPerDay + 500), daemon_.user_active_last_);
- EXPECT_EQ(10, daemon_.daily_use_day_last_);
- EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
+ ExpectDailyUseUpdate(10, 0);
daemon_.SetUserActiveState(/* active */ true,
TestTime(10 * kSecondsPerDay + 300));
EXPECT_TRUE(daemon_.user_active_);
EXPECT_EQ(TestTime(10 * kSecondsPerDay + 300), daemon_.user_active_last_);
- EXPECT_EQ(10, daemon_.daily_use_day_last_);
- EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
+ ExpectDailyUseUpdate(10, 0);
daemon_.SetUserActiveState(/* active */ true,
TestTime(10 * kSecondsPerDay + 1000));
EXPECT_TRUE(daemon_.user_active_);
EXPECT_EQ(TestTime(10 * kSecondsPerDay + 1000), daemon_.user_active_last_);
- EXPECT_EQ(10, daemon_.daily_use_day_last_);
- EXPECT_TRUE(AssertNoOrEmptyUseRecordFile());
}
int main(int argc, char** argv) {
« no previous file with comments | « metrics_daemon.cc ('k') | metrics_library_mock.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698