1 // Copyright (C) 2018 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 "src/anomaly/AlarmTracker.h"
16 
17 #include <gtest/gtest.h>
18 #include <stdio.h>
19 #include <vector>
20 
21 using namespace testing;
22 using android::sp;
23 using std::set;
24 using std::unordered_map;
25 using std::vector;
26 
27 #ifdef __ANDROID__
28 
29 namespace android {
30 namespace os {
31 namespace statsd {
32 
33 const ConfigKey kConfigKey(0, 12345);
34 
TEST(AlarmTrackerTest,TestTriggerTimestamp)35 TEST(AlarmTrackerTest, TestTriggerTimestamp) {
36     sp<AlarmMonitor> subscriberAlarmMonitor =
37         new AlarmMonitor(100, [](const sp<IStatsCompanionService>&, int64_t){},
38                          [](const sp<IStatsCompanionService>&){});
39     Alarm alarm;
40     alarm.set_offset_millis(15 * MS_PER_SEC);
41     alarm.set_period_millis(60 * 60 * MS_PER_SEC);  // 1hr
42     int64_t startMillis = 100000000 * MS_PER_SEC;
43     int64_t nextAlarmTime = startMillis / MS_PER_SEC + 15;
44     AlarmTracker tracker(startMillis, startMillis, alarm, kConfigKey, subscriberAlarmMonitor);
45 
46     EXPECT_EQ(tracker.mAlarmSec, nextAlarmTime);
47 
48     uint64_t currentTimeSec = startMillis / MS_PER_SEC + 10;
49     std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> firedAlarmSet =
50         subscriberAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
51     EXPECT_TRUE(firedAlarmSet.empty());
52     tracker.informAlarmsFired(currentTimeSec * NS_PER_SEC, firedAlarmSet);
53     EXPECT_EQ(tracker.mAlarmSec, nextAlarmTime);
54     EXPECT_EQ(tracker.getAlarmTimestampSec(), nextAlarmTime);
55 
56     currentTimeSec = startMillis / MS_PER_SEC + 7000;
57     nextAlarmTime = startMillis / MS_PER_SEC + 15 + 2 * 60 * 60;
58     firedAlarmSet = subscriberAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
59     EXPECT_EQ(firedAlarmSet.size(), 1u);
60     tracker.informAlarmsFired(currentTimeSec * NS_PER_SEC, firedAlarmSet);
61     EXPECT_TRUE(firedAlarmSet.empty());
62     EXPECT_EQ(tracker.mAlarmSec, nextAlarmTime);
63     EXPECT_EQ(tracker.getAlarmTimestampSec(), nextAlarmTime);
64 
65     // Alarm fires exactly on time.
66     currentTimeSec = startMillis / MS_PER_SEC + 15 + 2 * 60 * 60;
67     nextAlarmTime = startMillis / MS_PER_SEC + 15 + 3 * 60 * 60;
68     firedAlarmSet = subscriberAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
69     ASSERT_EQ(firedAlarmSet.size(), 1u);
70     tracker.informAlarmsFired(currentTimeSec * NS_PER_SEC, firedAlarmSet);
71     EXPECT_TRUE(firedAlarmSet.empty());
72     EXPECT_EQ(tracker.mAlarmSec, nextAlarmTime);
73     EXPECT_EQ(tracker.getAlarmTimestampSec(), nextAlarmTime);
74 
75     // Alarm fires exactly 1 period late.
76     currentTimeSec = startMillis / MS_PER_SEC + 15 + 4 * 60 * 60;
77     nextAlarmTime = startMillis / MS_PER_SEC + 15 + 5 * 60 * 60;
78     firedAlarmSet = subscriberAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
79     ASSERT_EQ(firedAlarmSet.size(), 1u);
80     tracker.informAlarmsFired(currentTimeSec * NS_PER_SEC, firedAlarmSet);
81     EXPECT_TRUE(firedAlarmSet.empty());
82     EXPECT_EQ(tracker.mAlarmSec, nextAlarmTime);
83     EXPECT_EQ(tracker.getAlarmTimestampSec(), nextAlarmTime);
84 }
85 
86 }  // namespace statsd
87 }  // namespace os
88 }  // namespace android
89 #else
90 GTEST_LOG_(INFO) << "This test does nothing.\n";
91 #endif
92