Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <sys/file.h> |
| 6 | |
| 7 | #include <base/eintr_wrapper.h> |
| 8 | #include <base/file_util.h> |
| 9 | #include <base/logging.h> |
| 10 | #include <base/string_util.h> |
| 11 | #include <gmock/gmock.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | |
| 14 | #include "counter.h" |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 15 | #include "counter_mock.h" // For TaggedCounterMock. |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame^] | 16 | #include "metrics_library_mock.h" |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 17 | |
| 18 | using ::testing::_; |
| 19 | using ::testing::MockFunction; |
| 20 | using ::testing::StrictMock; |
| 21 | |
| 22 | namespace chromeos_metrics { |
| 23 | |
| 24 | static const char kTestRecordFile[] = "record-file"; |
| 25 | static const char kDoesNotExistFile[] = "/does/not/exist"; |
| 26 | |
| 27 | class RecordTest : public testing::Test { |
| 28 | protected: |
| 29 | virtual void SetUp() { |
| 30 | EXPECT_EQ(0, record_.tag()); |
| 31 | EXPECT_EQ(0, record_.count()); |
| 32 | } |
| 33 | |
| 34 | // The record under test. |
| 35 | TaggedCounter::Record record_; |
| 36 | }; |
| 37 | |
| 38 | class TaggedCounterTest : public testing::Test { |
| 39 | protected: |
| 40 | virtual void SetUp() { |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame^] | 41 | EXPECT_TRUE(counter_.filename_.empty()); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 42 | EXPECT_TRUE(NULL == counter_.reporter_); |
| 43 | EXPECT_EQ(NULL, counter_.reporter_handle_); |
| 44 | EXPECT_EQ(TaggedCounter::kRecordInvalid, counter_.record_state_); |
| 45 | |
| 46 | counter_.Init(kTestRecordFile, &Reporter, this); |
| 47 | EXPECT_TRUE(AssertNoOrEmptyRecordFile()); |
| 48 | EXPECT_EQ(kTestRecordFile, counter_.filename_); |
| 49 | EXPECT_TRUE(Reporter == counter_.reporter_); |
| 50 | EXPECT_EQ(this, counter_.reporter_handle_); |
| 51 | EXPECT_EQ(TaggedCounter::kRecordInvalid, counter_.record_state_); |
| 52 | |
| 53 | // The test fixture object will be used by the log message handler. |
| 54 | test_ = this; |
| 55 | logging::SetLogMessageHandler(HandleLogMessages); |
| 56 | } |
| 57 | |
| 58 | virtual void TearDown() { |
| 59 | logging::SetLogMessageHandler(NULL); |
| 60 | test_ = NULL; |
| 61 | file_util::Delete(FilePath(kTestRecordFile), false); |
| 62 | } |
| 63 | |
| 64 | // Asserts that the record file contains the specified contents. |
| 65 | testing::AssertionResult AssertRecord(const char* expr_tag, |
| 66 | const char* expr_count, |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 67 | int32 expected_tag, |
| 68 | int32 expected_count) { |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 69 | int fd = HANDLE_EINTR(open(kTestRecordFile, O_RDONLY)); |
| 70 | if (fd < 0) { |
| 71 | testing::Message msg; |
| 72 | msg << "Unable to open " << kTestRecordFile; |
| 73 | return testing::AssertionFailure(msg); |
| 74 | } |
| 75 | |
| 76 | TaggedCounter::Record record; |
| 77 | if (!file_util::ReadFromFD(fd, reinterpret_cast<char*>(&record), |
| 78 | sizeof(record))) { |
| 79 | testing::Message msg; |
| 80 | msg << "Unable to read " << sizeof(record) << " bytes from " |
| 81 | << kTestRecordFile; |
| 82 | HANDLE_EINTR(close(fd)); |
| 83 | return testing::AssertionFailure(msg); |
| 84 | } |
| 85 | |
| 86 | if (record.tag() != expected_tag || record.count() != expected_count) { |
| 87 | testing::Message msg; |
| 88 | msg << "actual record (" << record.tag() << ", " << record.count() |
| 89 | << ") expected (" << expected_tag << ", " << expected_count << ")"; |
| 90 | HANDLE_EINTR(close(fd)); |
| 91 | return testing::AssertionFailure(msg); |
| 92 | } |
| 93 | |
| 94 | HANDLE_EINTR(close(fd)); |
| 95 | return testing::AssertionSuccess(); |
| 96 | } |
| 97 | |
| 98 | // Returns true if the persistent record file does not exist or is |
| 99 | // empty, false otherwise. |
| 100 | bool AssertNoOrEmptyRecordFile() { |
| 101 | FilePath record_file(counter_.filename_); |
| 102 | int64 record_file_size; |
| 103 | return !file_util::PathExists(record_file) || |
| 104 | (file_util::GetFileSize(record_file, &record_file_size) && |
| 105 | record_file_size == 0); |
| 106 | } |
| 107 | |
| 108 | // Adds a reporter call expectation that the specified tag/count |
| 109 | // callback will be generated. |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 110 | void ExpectReporterCall(int32 tag, int32 count) { |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 111 | EXPECT_CALL(reporter_, Call(_, tag, count)) |
| 112 | .Times(1) |
| 113 | .RetiresOnSaturation(); |
| 114 | } |
| 115 | |
| 116 | // The reporter callback forwards the call to the reporter mock so |
| 117 | // that we can set call expectations. |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 118 | static void Reporter(void* handle, int32 tag, int32 count) { |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 119 | TaggedCounterTest* test = static_cast<TaggedCounterTest*>(handle); |
| 120 | ASSERT_FALSE(NULL == test); |
| 121 | test->reporter_.Call(handle, tag, count); |
| 122 | } |
| 123 | |
| 124 | // Collects log messages in the |log_| member string so that they |
| 125 | // can be analyzed for errors and expected behavior. |
| 126 | static bool HandleLogMessages(int severity, const std::string& str) { |
| 127 | test_->log_.append(str); |
| 128 | test_->log_.append("\n"); |
| 129 | |
| 130 | // Returning true would mute the log. |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | // Returns true if the counter log contains |pattern|, false otherwise. |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 135 | bool LogContains(const std::string& pattern) const { |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 136 | return log_.find(pattern) != std::string::npos; |
| 137 | } |
| 138 | |
| 139 | // The TaggedCounter object under test. |
| 140 | TaggedCounter counter_; |
| 141 | |
| 142 | // The accumulated counter log. |
| 143 | std::string log_; |
| 144 | |
| 145 | // Reporter mock to set callback expectations on. |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 146 | StrictMock<MockFunction<void(void* handle, |
| 147 | int32 tag, int32 count)> > reporter_; |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 148 | |
| 149 | // Pointer to the current test fixture. |
| 150 | static TaggedCounterTest* test_; |
| 151 | }; |
| 152 | |
| 153 | // static |
| 154 | TaggedCounterTest* TaggedCounterTest::test_ = NULL; |
| 155 | |
| 156 | TEST_F(RecordTest, Init) { |
| 157 | record_.Init(/* tag */ 5, /* count */ -1); |
| 158 | EXPECT_EQ(5, record_.tag()); |
| 159 | EXPECT_EQ(0, record_.count()); |
| 160 | |
| 161 | record_.Init(/* tag */ -2, /* count */ 10); |
| 162 | EXPECT_EQ(-2, record_.tag()); |
| 163 | EXPECT_EQ(10, record_.count()); |
| 164 | } |
| 165 | |
| 166 | TEST_F(RecordTest, Add) { |
| 167 | record_.Add(/* count */ -1); |
| 168 | EXPECT_EQ(0, record_.count()); |
| 169 | |
| 170 | record_.Add(/* count */ 5); |
| 171 | EXPECT_EQ(5, record_.count()); |
| 172 | |
| 173 | record_.Add(/* count */ 10); |
| 174 | EXPECT_EQ(15, record_.count()); |
| 175 | |
| 176 | record_.Add(/* count */ -2); |
| 177 | EXPECT_EQ(15, record_.count()); |
| 178 | |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 179 | record_.Add(/* count */ kint32max); |
| 180 | EXPECT_EQ(kint32max, record_.count()); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 181 | |
| 182 | record_.Add(/* count */ 1); |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 183 | EXPECT_EQ(kint32max, record_.count()); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | TEST_F(TaggedCounterTest, BadFileLocation) { |
| 187 | // Checks that the counter doesn't die badly if the file can't be |
| 188 | // created. |
| 189 | counter_.Init(kDoesNotExistFile, |
| 190 | /* reporter */ NULL, /* reporter_handle */ NULL); |
| 191 | counter_.Update(/* tag */ 10, /* count */ 20); |
| 192 | EXPECT_TRUE(LogContains("Unable to open the persistent counter file: " |
| 193 | "No such file or directory")); |
| 194 | EXPECT_EQ(TaggedCounter::kRecordInvalid, counter_.record_state_); |
| 195 | file_util::Delete(FilePath(kDoesNotExistFile), false); |
| 196 | } |
| 197 | |
| 198 | TEST_F(TaggedCounterTest, Flush) { |
| 199 | counter_.Flush(); |
| 200 | EXPECT_EQ(TaggedCounter::kRecordNull, counter_.record_state_); |
| 201 | |
| 202 | counter_.Update(/* tag */ 40, /* count */ 60); |
| 203 | ExpectReporterCall(/* tag */ 40, /* count */ 60); |
| 204 | counter_.Flush(); |
| 205 | EXPECT_TRUE(AssertNoOrEmptyRecordFile()); |
| 206 | EXPECT_EQ(TaggedCounter::kRecordNull, counter_.record_state_); |
| 207 | |
| 208 | counter_.Update(/* tag */ 41, /* count */ 70); |
| 209 | counter_.record_.Init(/* tag */ 0, /* count */ 0); |
| 210 | counter_.record_state_ = TaggedCounter::kRecordInvalid; |
| 211 | ExpectReporterCall(/* tag */ 41, /* count */ 70); |
| 212 | counter_.Flush(); |
| 213 | EXPECT_TRUE(AssertNoOrEmptyRecordFile()); |
| 214 | EXPECT_EQ(TaggedCounter::kRecordNull, counter_.record_state_); |
| 215 | } |
| 216 | |
| 217 | TEST_F(TaggedCounterTest, InitFromFile) { |
| 218 | counter_.Update(/* tag */ 30, /* count */ 50); |
| 219 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 30, /* seconds */ 50); |
| 220 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
| 221 | |
| 222 | counter_.Init(kTestRecordFile, &Reporter, this); |
| 223 | counter_.Update(/* tag */ 30, /* count */ 40); |
| 224 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 30, /* seconds */ 90); |
| 225 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
| 226 | |
| 227 | counter_.Init(kTestRecordFile, &Reporter, this); |
| 228 | ExpectReporterCall(/* tag */ 30, /* count */ 90); |
| 229 | counter_.Update(/* tag */ 31, /* count */ 60); |
| 230 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 31, /* seconds */ 60); |
| 231 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
| 232 | |
| 233 | ExpectReporterCall(/* tag */ 31, /* count */ 60); |
| 234 | counter_.Init(kTestRecordFile, &Reporter, this); |
| 235 | counter_.Update(/* tag */ 32, /* count */ 0); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 236 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 32, /* seconds */ 0); |
| 237 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | TEST_F(TaggedCounterTest, Update) { |
| 241 | counter_.Update(/* tag */ 20, /* count */ 30); |
| 242 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 20, /* seconds */ 30); |
| 243 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
| 244 | |
| 245 | counter_.Update(/* tag */ 20, /* count */ 40); |
| 246 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 20, /* seconds */ 70); |
| 247 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
| 248 | |
| 249 | ExpectReporterCall(/* tag */ 20, /* count */ 70); |
| 250 | counter_.Update(/* tag */ 21, /* count */ 15); |
| 251 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 21, /* seconds */ 15); |
| 252 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
| 253 | |
| 254 | ExpectReporterCall(/* tag */ 21, /* count */ 15); |
| 255 | counter_.Update(/* tag */ 22, /* count */ 0); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 256 | EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 22, /* seconds */ 0); |
| 257 | EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame^] | 260 | static const char kTestFilename[] = "test_filename"; |
| 261 | static const char kTestHistogram[] = "test_histogram"; |
| 262 | const int kHistogramMin = 15; |
| 263 | const int kHistogramMax = 1024; |
| 264 | const int kHistogramBuckets = 23; |
| 265 | |
| 266 | class TaggedCounterReporterTest : public testing::Test { |
| 267 | protected: |
| 268 | virtual void SetUp() { |
| 269 | tagged_counter_ = new StrictMock<TaggedCounterMock>(); |
| 270 | reporter_.tagged_counter_.reset(tagged_counter_); |
| 271 | metrics_lib_.reset(new StrictMock<MetricsLibraryMock>); |
| 272 | reporter_.SetMetricsLibraryInterface(metrics_lib_.get()); |
| 273 | ASSERT_TRUE(metrics_lib_.get() == reporter_.metrics_lib_); |
| 274 | } |
| 275 | virtual void TearDown() { |
| 276 | reporter_.SetMetricsLibraryInterface(NULL); |
| 277 | } |
| 278 | |
| 279 | void DoInit(); |
| 280 | StrictMock<TaggedCounterMock>* tagged_counter_; |
| 281 | TaggedCounterReporter reporter_; |
| 282 | scoped_ptr<MetricsLibraryMock> metrics_lib_; |
| 283 | }; |
| 284 | |
| 285 | void TaggedCounterReporterTest::DoInit() { |
| 286 | EXPECT_CALL(*tagged_counter_, |
| 287 | Init(kTestFilename, |
| 288 | TaggedCounterReporter::Report, |
| 289 | &reporter_)) |
| 290 | .Times(1) |
| 291 | .RetiresOnSaturation(); |
| 292 | reporter_.Init(kTestFilename, |
| 293 | kTestHistogram, |
| 294 | kHistogramMin, |
| 295 | kHistogramMax, |
| 296 | kHistogramBuckets); |
| 297 | EXPECT_EQ(kTestHistogram, reporter_.histogram_name_); |
| 298 | EXPECT_EQ(kHistogramBuckets, reporter_.buckets_); |
| 299 | EXPECT_EQ(kHistogramMax, reporter_.max_); |
| 300 | EXPECT_EQ(kHistogramMin, reporter_.min_); |
| 301 | } |
| 302 | |
| 303 | TEST_F(TaggedCounterReporterTest, Init) { |
| 304 | DoInit(); |
| 305 | } |
| 306 | |
| 307 | TEST_F(TaggedCounterReporterTest, Update) { |
| 308 | DoInit(); |
| 309 | EXPECT_CALL(*tagged_counter_, Update(1, 2)) |
| 310 | .Times(1) |
| 311 | .RetiresOnSaturation(); |
| 312 | reporter_.Update(1, 2); |
| 313 | } |
| 314 | |
| 315 | TEST_F(TaggedCounterReporterTest, Flush) { |
| 316 | DoInit(); |
| 317 | EXPECT_CALL(*tagged_counter_, Flush()) |
| 318 | .Times(1) |
| 319 | .RetiresOnSaturation(); |
| 320 | reporter_.Flush(); |
| 321 | } |
| 322 | |
| 323 | TEST_F(TaggedCounterReporterTest, Report) { |
| 324 | DoInit(); |
| 325 | EXPECT_CALL(*metrics_lib_, SendToUMA(kTestHistogram, |
| 326 | 301, |
| 327 | kHistogramMin, |
| 328 | kHistogramMax, |
| 329 | kHistogramBuckets)) |
| 330 | .Times(1) |
| 331 | .RetiresOnSaturation(); |
| 332 | reporter_.Report(&reporter_, 127, 301); |
| 333 | } |
| 334 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 335 | class FrequencyCounterTest : public testing::Test { |
| 336 | protected: |
| 337 | virtual void SetUp() { |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame^] | 338 | tagged_counter_ = NULL; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void CheckInit(int32 cycle_duration); |
| 342 | void CheckCycleNumber(int32 cycle_duration); |
| 343 | |
| 344 | FrequencyCounter frequency_counter_; |
| 345 | StrictMock<TaggedCounterMock>* tagged_counter_; |
| 346 | |
| 347 | TaggedCounter::Reporter reporter_; |
| 348 | }; |
| 349 | |
| 350 | void FrequencyCounterTest::CheckInit(int32 cycle_duration) { |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame^] | 351 | tagged_counter_ = new StrictMock<TaggedCounterMock>; |
| 352 | frequency_counter_.Init(tagged_counter_, cycle_duration); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 353 | EXPECT_EQ(cycle_duration, frequency_counter_.cycle_duration_); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame^] | 354 | EXPECT_EQ(tagged_counter_, frequency_counter_.tagged_counter_.get()); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | TEST_F(FrequencyCounterTest, Init) { |
| 358 | CheckInit(100); |
| 359 | } |
| 360 | |
| 361 | void FrequencyCounterTest::CheckCycleNumber(int32 cycle_duration) { |
| 362 | CheckInit(cycle_duration); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame^] | 363 | EXPECT_EQ(150, frequency_counter_.GetCycleNumber( |
| 364 | cycle_duration * 150)); |
| 365 | EXPECT_EQ(150, frequency_counter_.GetCycleNumber( |
| 366 | cycle_duration * 150 + cycle_duration - 1)); |
| 367 | EXPECT_EQ(151, frequency_counter_.GetCycleNumber( |
| 368 | cycle_duration * 151 + 1)); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 369 | EXPECT_EQ(0, frequency_counter_.GetCycleNumber(0)); |
| 370 | } |
| 371 | |
| 372 | |
| 373 | TEST_F(FrequencyCounterTest, GetCycleNumberForWeek) { |
| 374 | CheckCycleNumber(kSecondsPerWeek); |
| 375 | } |
| 376 | |
| 377 | TEST_F(FrequencyCounterTest, GetCycleNumberForDay) { |
| 378 | CheckCycleNumber(kSecondsPerDay); |
| 379 | } |
| 380 | |
| 381 | TEST_F(FrequencyCounterTest, UpdateInternal) { |
| 382 | CheckInit(kSecondsPerWeek); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame^] | 383 | EXPECT_CALL(*tagged_counter_, Update(150, 2)) |
| 384 | .Times(1) |
| 385 | .RetiresOnSaturation(); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 386 | frequency_counter_.UpdateInternal(2, kSecondsPerWeek * 150); |
| 387 | } |
| 388 | |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 389 | } // namespace chromeos_metrics |
| 390 | |
| 391 | int main(int argc, char** argv) { |
| 392 | testing::InitGoogleTest(&argc, argv); |
| 393 | return RUN_ALL_TESTS(); |
| 394 | } |