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