Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 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> |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 18 | #include <log/log_time.h> |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <vector> |
| 21 | |
| 22 | using namespace testing; |
| 23 | using android::sp; |
| 24 | using std::set; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 25 | using std::shared_ptr; |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 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 | |
| 37 | TEST(AlarmTrackerTest, TestTriggerTimestamp) { |
| 38 | sp<AlarmMonitor> subscriberAlarmMonitor = |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 39 | new AlarmMonitor(100, |
| 40 | [](const shared_ptr<IStatsCompanionService>&, int64_t){}, |
| 41 | [](const shared_ptr<IStatsCompanionService>&){}); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 42 | Alarm alarm; |
| 43 | alarm.set_offset_millis(15 * MS_PER_SEC); |
| 44 | alarm.set_period_millis(60 * 60 * MS_PER_SEC); // 1hr |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 45 | int64_t startMillis = 100000000 * MS_PER_SEC; |
| 46 | AlarmTracker tracker(startMillis, startMillis, alarm, kConfigKey, subscriberAlarmMonitor); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 47 | |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 48 | EXPECT_EQ(tracker.mAlarmSec, (int64_t)(startMillis / MS_PER_SEC + 15)); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 49 | |
| 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-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 55 | EXPECT_EQ(tracker.mAlarmSec, (int64_t)(startMillis / MS_PER_SEC + 15)); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 56 | |
| 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-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 62 | EXPECT_EQ(tracker.mAlarmSec, (int64_t)(startMillis / MS_PER_SEC + 15 + 2 * 60 * 60)); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | } // namespace statsd |
| 66 | } // namespace os |
| 67 | } // namespace android |
| 68 | #else |
| 69 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 70 | #endif |