Bookatz | ece5f70 | 2017-10-09 14:59:38 -0700 | [diff] [blame] | 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 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 15 | #include "anomaly/AlarmMonitor.h" |
Bookatz | ece5f70 | 2017-10-09 14:59:38 -0700 | [diff] [blame] | 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | using namespace android::os::statsd; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 20 | using std::shared_ptr; |
Bookatz | ece5f70 | 2017-10-09 14:59:38 -0700 | [diff] [blame] | 21 | |
| 22 | #ifdef __ANDROID__ |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 23 | TEST(AlarmMonitor, popSoonerThan) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 24 | std::string emptyMetricId; |
| 25 | std::string emptyDimensionId; |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 26 | unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> set; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 27 | AlarmMonitor am(2, |
| 28 | [](const shared_ptr<IStatsCompanionService>&, int64_t){}, |
| 29 | [](const shared_ptr<IStatsCompanionService>&){}); |
Bookatz | ece5f70 | 2017-10-09 14:59:38 -0700 | [diff] [blame] | 30 | |
| 31 | set = am.popSoonerThan(5); |
| 32 | EXPECT_TRUE(set.empty()); |
| 33 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 34 | sp<const InternalAlarm> a = new InternalAlarm{10}; |
| 35 | sp<const InternalAlarm> b = new InternalAlarm{20}; |
| 36 | sp<const InternalAlarm> c = new InternalAlarm{20}; |
| 37 | sp<const InternalAlarm> d = new InternalAlarm{30}; |
| 38 | sp<const InternalAlarm> e = new InternalAlarm{40}; |
| 39 | sp<const InternalAlarm> f = new InternalAlarm{50}; |
Bookatz | ece5f70 | 2017-10-09 14:59:38 -0700 | [diff] [blame] | 40 | |
| 41 | am.add(a); |
| 42 | am.add(b); |
| 43 | am.add(c); |
| 44 | am.add(d); |
| 45 | am.add(e); |
| 46 | am.add(f); |
| 47 | |
| 48 | set = am.popSoonerThan(5); |
| 49 | EXPECT_TRUE(set.empty()); |
| 50 | |
| 51 | set = am.popSoonerThan(30); |
| 52 | EXPECT_EQ(4u, set.size()); |
| 53 | EXPECT_EQ(1u, set.count(a)); |
| 54 | EXPECT_EQ(1u, set.count(b)); |
| 55 | EXPECT_EQ(1u, set.count(c)); |
| 56 | EXPECT_EQ(1u, set.count(d)); |
| 57 | |
| 58 | set = am.popSoonerThan(60); |
| 59 | EXPECT_EQ(2u, set.size()); |
| 60 | EXPECT_EQ(1u, set.count(e)); |
| 61 | EXPECT_EQ(1u, set.count(f)); |
| 62 | |
| 63 | set = am.popSoonerThan(80); |
| 64 | EXPECT_EQ(0u, set.size()); |
| 65 | } |
| 66 | |
| 67 | #else |
| 68 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 69 | #endif |