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/14 19:28:33
no #define -- maybe change to static consts...
Luigi Semenzato
2011/02/14 23:24:17
Sorry. Too little caffeine.
| |
38 #define READ_SECTORS_1 100000 | |
39 | |
40 #define WRITE_SECTORS_0 3000 | |
41 #define WRITE_SECTORS_1 4000 | |
42 | |
43 static const char kFakeDiskStatsFormat[] = | |
44 " 1793 1788 %d 105580 " | |
45 " 196 175 %d 30290 " | |
46 " 0 44060 135850\n"; | |
47 | |
48 static char kFakeDiskStats[2][200]; | |
49 | |
50 static const int kFakeReadSectors[] = { READ_SECTORS_0, READ_SECTORS_1, }; | |
51 static const int kFakeWriteSectors[] = { WRITE_SECTORS_0, WRITE_SECTORS_1, }; | |
35 | 52 |
36 // This class allows a TimeTicks object to be initialized with seconds | 53 // This class allows a TimeTicks object to be initialized with seconds |
37 // (rather than microseconds) through the protected TimeTicks(int64) | 54 // (rather than microseconds) through the protected TimeTicks(int64) |
38 // constructor. | 55 // constructor. |
39 class TestTicks : public TimeTicks { | 56 class TestTicks : public TimeTicks { |
40 public: | 57 public: |
41 TestTicks(int64 seconds) | 58 TestTicks(int64 seconds) |
42 : TimeTicks(seconds * Time::kMicrosecondsPerSecond) {} | 59 : TimeTicks(seconds * Time::kMicrosecondsPerSecond) {} |
43 }; | 60 }; |
44 | 61 |
45 // Overloaded for test failure printing purposes. | 62 // Overloaded for test failure printing purposes. |
46 static std::ostream& operator<<(std::ostream& o, const Time& time) { | 63 static std::ostream& operator<<(std::ostream& o, const Time& time) { |
47 o << time.ToInternalValue() << "us"; | 64 o << time.ToInternalValue() << "us"; |
48 return o; | 65 return o; |
49 }; | 66 }; |
50 | 67 |
51 class MetricsDaemonTest : public testing::Test { | 68 class MetricsDaemonTest : public testing::Test { |
52 protected: | 69 protected: |
53 virtual void SetUp() { | 70 virtual void SetUp() { |
54 EXPECT_EQ(NULL, daemon_.daily_use_.get()); | 71 EXPECT_EQ(NULL, daemon_.daily_use_.get()); |
55 EXPECT_EQ(NULL, daemon_.kernel_crash_interval_.get()); | 72 EXPECT_EQ(NULL, daemon_.kernel_crash_interval_.get()); |
56 EXPECT_EQ(NULL, daemon_.user_crash_interval_.get()); | 73 EXPECT_EQ(NULL, daemon_.user_crash_interval_.get()); |
57 daemon_.Init(true, &metrics_lib_); | 74 snprintf(kFakeDiskStats[0], sizeof(kFakeDiskStats[0]), |
petkov
2011/02/14 19:28:33
Use StringPrintf.
| |
75 kFakeDiskStatsFormat, READ_SECTORS_0, WRITE_SECTORS_0); | |
76 snprintf(kFakeDiskStats[1], sizeof(kFakeDiskStats[1]), | |
77 kFakeDiskStatsFormat, READ_SECTORS_1, WRITE_SECTORS_1); | |
78 CreateFakeDiskStatsFile(kFakeDiskStatsPath, kFakeDiskStats[0]); | |
79 daemon_.Init(true, &metrics_lib_, kFakeDiskStatsPath); | |
58 | 80 |
59 // Check configuration of a few histograms. | 81 // Check configuration of a few histograms. |
60 FrequencyCounter* frequency_counter = | 82 FrequencyCounter* frequency_counter = |
61 daemon_.frequency_counters_[MetricsDaemon::kMetricAnyCrashesDailyName]; | 83 daemon_.frequency_counters_[MetricsDaemon::kMetricAnyCrashesDailyName]; |
62 const TaggedCounterReporter* reporter = GetReporter(frequency_counter); | 84 const TaggedCounterReporter* reporter = GetReporter(frequency_counter); |
63 EXPECT_EQ(MetricsDaemon::kMetricAnyCrashesDailyName, | 85 EXPECT_EQ(MetricsDaemon::kMetricAnyCrashesDailyName, |
64 reporter->histogram_name()); | 86 reporter->histogram_name()); |
65 EXPECT_EQ(chromeos_metrics::kSecondsPerDay, | 87 EXPECT_EQ(chromeos_metrics::kSecondsPerDay, |
66 frequency_counter->cycle_duration()); | 88 frequency_counter->cycle_duration()); |
67 EXPECT_EQ(MetricsDaemon::kMetricCrashFrequencyMin, reporter->min()); | 89 EXPECT_EQ(MetricsDaemon::kMetricCrashFrequencyMin, reporter->min()); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 } | 237 } |
216 return msg; | 238 return msg; |
217 } | 239 } |
218 | 240 |
219 // Deallocates the DBus message |msg| previously allocated through | 241 // Deallocates the DBus message |msg| previously allocated through |
220 // dbus_message_new*. | 242 // dbus_message_new*. |
221 void DeleteDBusMessage(DBusMessage* msg) { | 243 void DeleteDBusMessage(DBusMessage* msg) { |
222 dbus_message_unref(msg); | 244 dbus_message_unref(msg); |
223 } | 245 } |
224 | 246 |
225 // Get the frequency counter for the given name. | 247 // Gets the frequency counter for the given name. |
226 FrequencyCounterMock& GetFrequencyMock(const char* histogram_name) { | 248 FrequencyCounterMock& GetFrequencyMock(const char* histogram_name) { |
227 return *static_cast<FrequencyCounterMock*>( | 249 return *static_cast<FrequencyCounterMock*>( |
228 daemon_.frequency_counters_[histogram_name]); | 250 daemon_.frequency_counters_[histogram_name]); |
229 } | 251 } |
230 | 252 |
253 // Creates or overwrites an input file containing fake disk stats. | |
254 void CreateFakeDiskStatsFile(const char* diskstats_path, | |
255 const char* fake_stats) { | |
256 if (unlink(diskstats_path) < 0) { | |
257 EXPECT_EQ(errno, ENOENT); | |
258 } | |
259 FILE* f = fopen(diskstats_path, "w"); | |
260 EXPECT_EQ(1, fwrite(fake_stats, strlen(fake_stats), 1, f)); | |
261 EXPECT_EQ(0, fclose(f)); | |
262 } | |
263 | |
264 | |
231 // The MetricsDaemon under test. | 265 // The MetricsDaemon under test. |
232 MetricsDaemon daemon_; | 266 MetricsDaemon daemon_; |
233 | 267 |
234 // Metrics library mock. It's a strict mock so that all unexpected | 268 // Metrics library mock. It's a strict mock so that all unexpected |
235 // metric generation calls are marked as failures. | 269 // metric generation calls are marked as failures. |
236 StrictMock<MetricsLibraryMock> metrics_lib_; | 270 StrictMock<MetricsLibraryMock> metrics_lib_; |
237 | 271 |
238 // Counter mocks. They are strict mocks so that all unexpected | 272 // Counter mocks. They are strict mocks so that all unexpected |
239 // update calls are marked as failures. They are pointers so that | 273 // update calls are marked as failures. They are pointers so that |
240 // they can replace the scoped_ptr's allocated by the daemon. | 274 // 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_); | 560 EXPECT_TRUE(daemon_.user_active_); |
527 EXPECT_EQ(TestTime(10 * kSecondsPerDay + 1000), daemon_.user_active_last_); | 561 EXPECT_EQ(TestTime(10 * kSecondsPerDay + 1000), daemon_.user_active_last_); |
528 } | 562 } |
529 | 563 |
530 TEST_F(MetricsDaemonTest, GetHistogramPath) { | 564 TEST_F(MetricsDaemonTest, GetHistogramPath) { |
531 EXPECT_EQ("/var/log/metrics/Logging.AnyCrashesDaily", | 565 EXPECT_EQ("/var/log/metrics/Logging.AnyCrashesDaily", |
532 daemon_.GetHistogramPath( | 566 daemon_.GetHistogramPath( |
533 MetricsDaemon::kMetricAnyCrashesDailyName).value()); | 567 MetricsDaemon::kMetricAnyCrashesDailyName).value()); |
534 } | 568 } |
535 | 569 |
570 TEST_F(MetricsDaemonTest, ReportDiskStats) { | |
571 long int read_sectors_now, write_sectors_now; | |
572 | |
573 CreateFakeDiskStatsFile(kFakeDiskStatsPath, kFakeDiskStats[1]); | |
574 daemon_.DiskStatsReadStats(&read_sectors_now, &write_sectors_now); | |
575 EXPECT_EQ(read_sectors_now, kFakeReadSectors[1]); | |
576 EXPECT_EQ(write_sectors_now, kFakeWriteSectors[1]); | |
577 | |
578 MetricsDaemon::DiskStatsState ds_state = daemon_.diskstats_state_; | |
579 EXPECT_CALL(metrics_lib_, SendToUMA(_, _, _, _, _)) | |
580 .Times(2); | |
581 daemon_.DiskStatsCallback(); | |
582 EXPECT_TRUE(ds_state != daemon_.diskstats_state_); | |
583 } | |
584 | |
536 int main(int argc, char** argv) { | 585 int main(int argc, char** argv) { |
537 testing::InitGoogleTest(&argc, argv); | 586 testing::InitGoogleTest(&argc, argv); |
538 return RUN_ALL_TESTS(); | 587 return RUN_ALL_TESTS(); |
539 } | 588 } |
OLD | NEW |