blob: 90ffcd009efdea60f3f5f119bd01c34ef6c04a9d [file] [log] [blame]
Yangster-mac932ecec2018-02-01 10:23:52 -08001// 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>
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080018#include <log/log_time.h>
Yangster-mac932ecec2018-02-01 10:23:52 -080019#include <stdio.h>
20#include <vector>
21
22using namespace testing;
23using android::sp;
24using std::set;
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080025using std::shared_ptr;
Yangster-mac932ecec2018-02-01 10:23:52 -080026using std::unordered_map;
27using std::vector;
28
29#ifdef __ANDROID__
30
31namespace android {
32namespace os {
33namespace statsd {
34
35const ConfigKey kConfigKey(0, 12345);
36
37TEST(AlarmTrackerTest, TestTriggerTimestamp) {
38 sp<AlarmMonitor> subscriberAlarmMonitor =
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080039 new AlarmMonitor(100,
40 [](const shared_ptr<IStatsCompanionService>&, int64_t){},
41 [](const shared_ptr<IStatsCompanionService>&){});
Yangster-mac932ecec2018-02-01 10:23:52 -080042 Alarm alarm;
43 alarm.set_offset_millis(15 * MS_PER_SEC);
44 alarm.set_period_millis(60 * 60 * MS_PER_SEC); // 1hr
Yangster-macc04feba2018-04-02 14:37:33 -070045 int64_t startMillis = 100000000 * MS_PER_SEC;
46 AlarmTracker tracker(startMillis, startMillis, alarm, kConfigKey, subscriberAlarmMonitor);
Yangster-mac932ecec2018-02-01 10:23:52 -080047
Yangster-macc04feba2018-04-02 14:37:33 -070048 EXPECT_EQ(tracker.mAlarmSec, (int64_t)(startMillis / MS_PER_SEC + 15));
Yangster-mac932ecec2018-02-01 10:23:52 -080049
50 uint64_t currentTimeSec = startMillis / MS_PER_SEC + 10;
51 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> firedAlarmSet =
52 subscriberAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
53 EXPECT_TRUE(firedAlarmSet.empty());
54 tracker.informAlarmsFired(currentTimeSec * NS_PER_SEC, firedAlarmSet);
Yangster-macc04feba2018-04-02 14:37:33 -070055 EXPECT_EQ(tracker.mAlarmSec, (int64_t)(startMillis / MS_PER_SEC + 15));
Yangster-mac932ecec2018-02-01 10:23:52 -080056
57 currentTimeSec = startMillis / MS_PER_SEC + 7000;
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());
Yangster-macc04feba2018-04-02 14:37:33 -070062 EXPECT_EQ(tracker.mAlarmSec, (int64_t)(startMillis / MS_PER_SEC + 15 + 2 * 60 * 60));
Yangster-mac932ecec2018-02-01 10:23:52 -080063}
64
65} // namespace statsd
66} // namespace os
67} // namespace android
68#else
69GTEST_LOG_(INFO) << "This test does nothing.\n";
70#endif