Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <utime.h> | 5 #include <utime.h> |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include <base/file_util.h> | 10 #include <base/file_util.h> |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 using std::vector; | 25 using std::vector; |
| 26 using ::testing::_; | 26 using ::testing::_; |
| 27 using ::testing::Return; | 27 using ::testing::Return; |
| 28 using ::testing::StrictMock; | 28 using ::testing::StrictMock; |
| 29 | 29 |
| 30 static const int kSecondsPerDay = 24 * 60 * 60; | 30 static const int kSecondsPerDay = 24 * 60 * 60; |
| 31 | 31 |
| 32 static const char kTestDir[] = "test"; | 32 static const char kTestDir[] = "test"; |
| 33 static const char kLastFile[] = "test/last"; | 33 static const char kLastFile[] = "test/last"; |
| 34 static const char kCurrentFile[] = "test/current"; | 34 static const char kCurrentFile[] = "test/current"; |
| 35 static const char kFakeDiskStatsPath[] = "fake-disk-stats"; | |
| 36 | |
| 37 #define READ_SECTORS_0 80000 | |
|
petkov
2011/02/12 05:34:45
no #define per style :/ you may need to use String
Luigi Semenzato
2011/02/14 18:43:43
Yes. Too bad that #defines are the only way to st
| |
| 38 #define READ_SECTORS_1 100000 | |
| 39 | |
| 40 #define WRITE_SECTORS_0 3000 | |
| 41 #define WRITE_SECTORS_1 4000 | |
| 42 | |
| 43 #define string(x) string_(x) | |
| 44 #define string_(x) #x | |
| 45 | |
| 46 static const char* kFakeDiskStats[] = { | |
| 47 " 1793 1788 " string(READ_SECTORS_0) " 105580 " | |
| 48 " 196 175 " string(WRITE_SECTORS_0) " 30290 " | |
| 49 " 0 44060 135850\n", | |
| 50 " 1793 1788 " string(READ_SECTORS_1) " 105580 " | |
| 51 " 196 175 " string(WRITE_SECTORS_1) " 30290 " | |
| 52 " 0 44060 135850\n", | |
| 53 }; | |
| 54 | |
| 55 static const int kFakeReadSectors[] = { READ_SECTORS_0, READ_SECTORS_1, }; | |
| 56 static const int kFakeWriteSectors[] = { WRITE_SECTORS_0, WRITE_SECTORS_1, }; | |
| 35 | 57 |
| 36 // This class allows a TimeTicks object to be initialized with seconds | 58 // This class allows a TimeTicks object to be initialized with seconds |
| 37 // (rather than microseconds) through the protected TimeTicks(int64) | 59 // (rather than microseconds) through the protected TimeTicks(int64) |
| 38 // constructor. | 60 // constructor. |
| 39 class TestTicks : public TimeTicks { | 61 class TestTicks : public TimeTicks { |
| 40 public: | 62 public: |
| 41 TestTicks(int64 seconds) | 63 TestTicks(int64 seconds) |
| 42 : TimeTicks(seconds * Time::kMicrosecondsPerSecond) {} | 64 : TimeTicks(seconds * Time::kMicrosecondsPerSecond) {} |
| 43 }; | 65 }; |
| 44 | 66 |
| 45 // Overloaded for test failure printing purposes. | 67 // Overloaded for test failure printing purposes. |
| 46 static std::ostream& operator<<(std::ostream& o, const Time& time) { | 68 static std::ostream& operator<<(std::ostream& o, const Time& time) { |
| 47 o << time.ToInternalValue() << "us"; | 69 o << time.ToInternalValue() << "us"; |
| 48 return o; | 70 return o; |
| 49 }; | 71 }; |
| 50 | 72 |
| 51 class MetricsDaemonTest : public testing::Test { | 73 class MetricsDaemonTest : public testing::Test { |
| 52 protected: | 74 protected: |
| 53 virtual void SetUp() { | 75 virtual void SetUp() { |
| 54 EXPECT_EQ(NULL, daemon_.daily_use_.get()); | 76 EXPECT_EQ(NULL, daemon_.daily_use_.get()); |
| 55 EXPECT_EQ(NULL, daemon_.kernel_crash_interval_.get()); | 77 EXPECT_EQ(NULL, daemon_.kernel_crash_interval_.get()); |
| 56 EXPECT_EQ(NULL, daemon_.user_crash_interval_.get()); | 78 EXPECT_EQ(NULL, daemon_.user_crash_interval_.get()); |
| 57 daemon_.Init(true, &metrics_lib_); | 79 CreateFakeDiskStatsFile(kFakeDiskStatsPath, kFakeDiskStats[0]); |
| 80 daemon_.Init(true, &metrics_lib_, kFakeDiskStatsPath); | |
| 58 | 81 |
| 59 // Check configuration of a few histograms. | 82 // Check configuration of a few histograms. |
| 60 FrequencyCounter* frequency_counter = | 83 FrequencyCounter* frequency_counter = |
| 61 daemon_.frequency_counters_[MetricsDaemon::kMetricAnyCrashesDailyName]; | 84 daemon_.frequency_counters_[MetricsDaemon::kMetricAnyCrashesDailyName]; |
| 62 const TaggedCounterReporter* reporter = GetReporter(frequency_counter); | 85 const TaggedCounterReporter* reporter = GetReporter(frequency_counter); |
| 63 EXPECT_EQ(MetricsDaemon::kMetricAnyCrashesDailyName, | 86 EXPECT_EQ(MetricsDaemon::kMetricAnyCrashesDailyName, |
| 64 reporter->histogram_name()); | 87 reporter->histogram_name()); |
| 65 EXPECT_EQ(chromeos_metrics::kSecondsPerDay, | 88 EXPECT_EQ(chromeos_metrics::kSecondsPerDay, |
| 66 frequency_counter->cycle_duration()); | 89 frequency_counter->cycle_duration()); |
| 67 EXPECT_EQ(MetricsDaemon::kMetricCrashFrequencyMin, reporter->min()); | 90 EXPECT_EQ(MetricsDaemon::kMetricCrashFrequencyMin, reporter->min()); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 | 136 |
| 114 EXPECT_FALSE(daemon_.user_active_); | 137 EXPECT_FALSE(daemon_.user_active_); |
| 115 EXPECT_TRUE(daemon_.user_active_last_.is_null()); | 138 EXPECT_TRUE(daemon_.user_active_last_.is_null()); |
| 116 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); | 139 EXPECT_EQ(MetricsDaemon::kUnknownPowerState, daemon_.power_state_); |
| 117 EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_); | 140 EXPECT_EQ(MetricsDaemon::kUnknownSessionState, daemon_.session_state_); |
| 118 | 141 |
| 119 file_util::Delete(FilePath(kTestDir), true); | 142 file_util::Delete(FilePath(kTestDir), true); |
| 120 file_util::CreateDirectory(FilePath(kTestDir)); | 143 file_util::CreateDirectory(FilePath(kTestDir)); |
| 121 } | 144 } |
| 122 | 145 |
| 123 virtual void TearDown() {} | 146 virtual void TearDown() {} |
|
petkov
2011/02/12 05:34:45
maybe delete the fake stats file?
Luigi Semenzato
2011/02/14 18:43:43
Except that it's useful for debugging. Is it gene
petkov
2011/02/14 19:28:32
Don't know but from what I've seen, unit tests try
| |
| 124 | 147 |
| 125 const TaggedCounterReporter* | 148 const TaggedCounterReporter* |
| 126 GetReporter(FrequencyCounter* frequency_counter) const { | 149 GetReporter(FrequencyCounter* frequency_counter) const { |
| 127 return static_cast<const TaggedCounterReporter*>( | 150 return static_cast<const TaggedCounterReporter*>( |
| 128 &frequency_counter->tagged_counter()); | 151 &frequency_counter->tagged_counter()); |
| 129 } | 152 } |
| 130 | 153 |
| 131 void ExpectFrequencyFlushCalls() { | 154 void ExpectFrequencyFlushCalls() { |
| 132 MetricsDaemon::FrequencyCounters::iterator i; | 155 MetricsDaemon::FrequencyCounters::iterator i; |
| 133 for (i = daemon_.frequency_counters_.begin(); | 156 for (i = daemon_.frequency_counters_.begin(); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 } | 238 } |
| 216 return msg; | 239 return msg; |
| 217 } | 240 } |
| 218 | 241 |
| 219 // Deallocates the DBus message |msg| previously allocated through | 242 // Deallocates the DBus message |msg| previously allocated through |
| 220 // dbus_message_new*. | 243 // dbus_message_new*. |
| 221 void DeleteDBusMessage(DBusMessage* msg) { | 244 void DeleteDBusMessage(DBusMessage* msg) { |
| 222 dbus_message_unref(msg); | 245 dbus_message_unref(msg); |
| 223 } | 246 } |
| 224 | 247 |
| 225 // Get the frequency counter for the given name. | 248 // Gets the frequency counter for the given name. |
| 226 FrequencyCounterMock& GetFrequencyMock(const char* histogram_name) { | 249 FrequencyCounterMock& GetFrequencyMock(const char* histogram_name) { |
| 227 return *static_cast<FrequencyCounterMock*>( | 250 return *static_cast<FrequencyCounterMock*>( |
| 228 daemon_.frequency_counters_[histogram_name]); | 251 daemon_.frequency_counters_[histogram_name]); |
| 229 } | 252 } |
| 230 | 253 |
| 254 // Creates or overwrites an input file containing fake disk stats. | |
| 255 void CreateFakeDiskStatsFile(const char* diskstats_path, | |
| 256 const char* fake_stats) { | |
| 257 if (unlink(diskstats_path) < 0) { | |
| 258 EXPECT_EQ(errno, ENOENT); | |
| 259 } | |
| 260 FILE* f = fopen(diskstats_path, "w"); | |
| 261 EXPECT_EQ(1, fwrite(fake_stats, strlen(fake_stats), 1, f)); | |
| 262 EXPECT_EQ(0, fclose(f)); | |
| 263 } | |
| 264 | |
| 265 | |
| 231 // The MetricsDaemon under test. | 266 // The MetricsDaemon under test. |
| 232 MetricsDaemon daemon_; | 267 MetricsDaemon daemon_; |
| 233 | 268 |
| 234 // Metrics library mock. It's a strict mock so that all unexpected | 269 // Metrics library mock. It's a strict mock so that all unexpected |
| 235 // metric generation calls are marked as failures. | 270 // metric generation calls are marked as failures. |
| 236 StrictMock<MetricsLibraryMock> metrics_lib_; | 271 StrictMock<MetricsLibraryMock> metrics_lib_; |
| 237 | 272 |
| 238 // Counter mocks. They are strict mocks so that all unexpected | 273 // Counter mocks. They are strict mocks so that all unexpected |
| 239 // update calls are marked as failures. They are pointers so that | 274 // update calls are marked as failures. They are pointers so that |
| 240 // they can replace the scoped_ptr's allocated by the daemon. | 275 // they can replace the scoped_ptr's allocated by the daemon. |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 526 EXPECT_TRUE(daemon_.user_active_); | 561 EXPECT_TRUE(daemon_.user_active_); |
| 527 EXPECT_EQ(TestTime(10 * kSecondsPerDay + 1000), daemon_.user_active_last_); | 562 EXPECT_EQ(TestTime(10 * kSecondsPerDay + 1000), daemon_.user_active_last_); |
| 528 } | 563 } |
| 529 | 564 |
| 530 TEST_F(MetricsDaemonTest, GetHistogramPath) { | 565 TEST_F(MetricsDaemonTest, GetHistogramPath) { |
| 531 EXPECT_EQ("/var/log/metrics/Logging.AnyCrashesDaily", | 566 EXPECT_EQ("/var/log/metrics/Logging.AnyCrashesDaily", |
| 532 daemon_.GetHistogramPath( | 567 daemon_.GetHistogramPath( |
| 533 MetricsDaemon::kMetricAnyCrashesDailyName).value()); | 568 MetricsDaemon::kMetricAnyCrashesDailyName).value()); |
| 534 } | 569 } |
| 535 | 570 |
| 571 TEST_F(MetricsDaemonTest, ReportDiskStats) { | |
| 572 long int read_sectors_now, write_sectors_now; | |
| 573 | |
| 574 CreateFakeDiskStatsFile(kFakeDiskStatsPath, kFakeDiskStats[1]); | |
| 575 daemon_.DiskStatsReadStats(&read_sectors_now, &write_sectors_now); | |
| 576 EXPECT_EQ(read_sectors_now, kFakeReadSectors[1]); | |
| 577 EXPECT_EQ(write_sectors_now, kFakeWriteSectors[1]); | |
| 578 | |
| 579 MetricsDaemon::DiskStatsState ds_state = daemon_.diskstats_state_; | |
| 580 EXPECT_CALL(metrics_lib_, SendToUMA(_, _, _, _, _)) | |
|
petkov
2011/02/12 05:34:45
You could check for the actual values that get sen
Luigi Semenzato
2011/02/14 18:43:43
Can I? I don't know how that EXPECT_CALL framewor
petkov
2011/02/14 19:28:32
Yes, you can. Also, right now you're not testing a
| |
| 581 .Times(2); | |
| 582 daemon_.DiskStatsCallback(); | |
| 583 EXPECT_TRUE(ds_state != daemon_.diskstats_state_); | |
| 584 } | |
| 585 | |
| 536 int main(int argc, char** argv) { | 586 int main(int argc, char** argv) { |
| 537 testing::InitGoogleTest(&argc, argv); | 587 testing::InitGoogleTest(&argc, argv); |
| 538 return RUN_ALL_TESTS(); | 588 return RUN_ALL_TESTS(); |
| 539 } | 589 } |
| OLD | NEW |