1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gtest/gtest.h>
16 
17 #include "src/StatsLogProcessor.h"
18 #include "src/stats_log_util.h"
19 #include "tests/statsd_test_util.h"
20 
21 #include <vector>
22 
23 namespace android {
24 namespace os {
25 namespace statsd {
26 
27 #ifdef __ANDROID__
28 
29 namespace {
30 
CreateStatsdConfig()31 StatsdConfig CreateStatsdConfig() {
32     StatsdConfig config;
33 
34     auto alarm = config.add_alarm();
35     alarm->set_id(123456);
36     alarm->set_offset_millis(TimeUnitToBucketSizeInMillis(TEN_MINUTES));
37     alarm->set_period_millis(TimeUnitToBucketSizeInMillis(ONE_HOUR));
38 
39     alarm = config.add_alarm();
40     alarm->set_id(654321);
41     alarm->set_offset_millis(TimeUnitToBucketSizeInMillis(FIVE_MINUTES));
42     alarm->set_period_millis(TimeUnitToBucketSizeInMillis(THIRTY_MINUTES));
43     return config;
44 }
45 
46 }  // namespace
47 
TEST(AlarmE2eTest,TestMultipleAlarms)48 TEST(AlarmE2eTest, TestMultipleAlarms) {
49     auto config = CreateStatsdConfig();
50     int64_t bucketStartTimeNs = 10000000000;
51 
52     ConfigKey cfgKey;
53     auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
54     ASSERT_EQ(processor->mMetricsManagers.size(), 1u);
55     EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
56     ASSERT_EQ(2u, processor->mMetricsManagers.begin()->second->mAllPeriodicAlarmTrackers.size());
57 
58     auto alarmTracker1 = processor->mMetricsManagers.begin()->second->mAllPeriodicAlarmTrackers[0];
59     auto alarmTracker2 = processor->mMetricsManagers.begin()->second->mAllPeriodicAlarmTrackers[1];
60 
61     int64_t alarmTimestampSec0 = bucketStartTimeNs / NS_PER_SEC + 10 * 60;
62     int64_t alarmTimestampSec1 = bucketStartTimeNs / NS_PER_SEC + 5 * 60;
63     EXPECT_EQ(alarmTimestampSec0, alarmTracker1->getAlarmTimestampSec());
64     EXPECT_EQ(alarmTimestampSec1, alarmTracker2->getAlarmTimestampSec());
65 
66     // Alarm fired.
67     const int64_t alarmFiredTimestampSec0 = alarmTimestampSec1 + 5;
68     auto alarmSet = processor->getPeriodicAlarmMonitor()->popSoonerThan(
69             static_cast<uint32_t>(alarmFiredTimestampSec0));
70     ASSERT_EQ(1u, alarmSet.size());
71     processor->onPeriodicAlarmFired(alarmFiredTimestampSec0 * NS_PER_SEC, alarmSet);
72     EXPECT_EQ(alarmTimestampSec0, alarmTracker1->getAlarmTimestampSec());
73     EXPECT_EQ(alarmTimestampSec1 + 30 * 60, alarmTracker2->getAlarmTimestampSec());
74 
75     // Alarms fired very late.
76     const int64_t alarmFiredTimestampSec1 = alarmTimestampSec0 + 2 * 60 * 60 + 125;
77     alarmSet = processor->getPeriodicAlarmMonitor()->popSoonerThan(
78             static_cast<uint32_t>(alarmFiredTimestampSec1));
79     ASSERT_EQ(2u, alarmSet.size());
80     processor->onPeriodicAlarmFired(alarmFiredTimestampSec1 * NS_PER_SEC, alarmSet);
81     EXPECT_EQ(alarmTimestampSec0 + 60 * 60 * 3, alarmTracker1->getAlarmTimestampSec());
82     EXPECT_EQ(alarmTimestampSec1 + 30 * 60 * 5, alarmTracker2->getAlarmTimestampSec());
83 }
84 
85 #else
86 GTEST_LOG_(INFO) << "This test does nothing.\n";
87 #endif
88 
89 }  // namespace statsd
90 }  // namespace os
91 }  // namespace android
92