Index: metrics_daemon_test.cc |
diff --git a/metrics_daemon_test.cc b/metrics_daemon_test.cc |
index 530f357b61cd735fc40e72c94badc38dcef7f536..72ddd28554bbbaa1c28e0f53bdf1a66201ea69bd 100644 |
--- a/metrics_daemon_test.cc |
+++ b/metrics_daemon_test.cc |
@@ -32,6 +32,28 @@ static const int kSecondsPerDay = 24 * 60 * 60; |
static const char kTestDir[] = "test"; |
static const char kLastFile[] = "test/last"; |
static const char kCurrentFile[] = "test/current"; |
+static const char kFakeDiskStatsPath[] = "fake-disk-stats"; |
+ |
+#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
|
+#define READ_SECTORS_1 100000 |
+ |
+#define WRITE_SECTORS_0 3000 |
+#define WRITE_SECTORS_1 4000 |
+ |
+#define string(x) string_(x) |
+#define string_(x) #x |
+ |
+static const char* kFakeDiskStats[] = { |
+ " 1793 1788 " string(READ_SECTORS_0) " 105580 " |
+ " 196 175 " string(WRITE_SECTORS_0) " 30290 " |
+ " 0 44060 135850\n", |
+ " 1793 1788 " string(READ_SECTORS_1) " 105580 " |
+ " 196 175 " string(WRITE_SECTORS_1) " 30290 " |
+ " 0 44060 135850\n", |
+}; |
+ |
+static const int kFakeReadSectors[] = { READ_SECTORS_0, READ_SECTORS_1, }; |
+static const int kFakeWriteSectors[] = { WRITE_SECTORS_0, WRITE_SECTORS_1, }; |
// This class allows a TimeTicks object to be initialized with seconds |
// (rather than microseconds) through the protected TimeTicks(int64) |
@@ -54,7 +76,8 @@ class MetricsDaemonTest : public testing::Test { |
EXPECT_EQ(NULL, daemon_.daily_use_.get()); |
EXPECT_EQ(NULL, daemon_.kernel_crash_interval_.get()); |
EXPECT_EQ(NULL, daemon_.user_crash_interval_.get()); |
- daemon_.Init(true, &metrics_lib_); |
+ CreateFakeDiskStatsFile(kFakeDiskStatsPath, kFakeDiskStats[0]); |
+ daemon_.Init(true, &metrics_lib_, kFakeDiskStatsPath); |
// Check configuration of a few histograms. |
FrequencyCounter* frequency_counter = |
@@ -222,12 +245,24 @@ class MetricsDaemonTest : public testing::Test { |
dbus_message_unref(msg); |
} |
- // Get the frequency counter for the given name. |
+ // Gets the frequency counter for the given name. |
FrequencyCounterMock& GetFrequencyMock(const char* histogram_name) { |
return *static_cast<FrequencyCounterMock*>( |
daemon_.frequency_counters_[histogram_name]); |
} |
+ // Creates or overwrites an input file containing fake disk stats. |
+ void CreateFakeDiskStatsFile(const char* diskstats_path, |
+ const char* fake_stats) { |
+ if (unlink(diskstats_path) < 0) { |
+ EXPECT_EQ(errno, ENOENT); |
+ } |
+ FILE* f = fopen(diskstats_path, "w"); |
+ EXPECT_EQ(1, fwrite(fake_stats, strlen(fake_stats), 1, f)); |
+ EXPECT_EQ(0, fclose(f)); |
+ } |
+ |
+ |
// The MetricsDaemon under test. |
MetricsDaemon daemon_; |
@@ -533,6 +568,21 @@ TEST_F(MetricsDaemonTest, GetHistogramPath) { |
MetricsDaemon::kMetricAnyCrashesDailyName).value()); |
} |
+TEST_F(MetricsDaemonTest, ReportDiskStats) { |
+ long int read_sectors_now, write_sectors_now; |
+ |
+ CreateFakeDiskStatsFile(kFakeDiskStatsPath, kFakeDiskStats[1]); |
+ daemon_.DiskStatsReadStats(&read_sectors_now, &write_sectors_now); |
+ EXPECT_EQ(read_sectors_now, kFakeReadSectors[1]); |
+ EXPECT_EQ(write_sectors_now, kFakeWriteSectors[1]); |
+ |
+ MetricsDaemon::DiskStatsState ds_state = daemon_.diskstats_state_; |
+ 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
|
+ .Times(2); |
+ daemon_.DiskStatsCallback(); |
+ EXPECT_TRUE(ds_state != daemon_.diskstats_state_); |
+} |
+ |
int main(int argc, char** argv) { |
testing::InitGoogleTest(&argc, argv); |
return RUN_ALL_TESTS(); |