blob: 1fccb35eccb57309806d32894a1df40c9107d717 [file] [log] [blame]
Bookatzece5f702017-10-09 14:59:38 -07001// 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-mac932ecec2018-02-01 10:23:52 -080015#include "anomaly/AlarmMonitor.h"
Bookatzece5f702017-10-09 14:59:38 -070016
17#include <gtest/gtest.h>
18
19using namespace android::os::statsd;
20
21#ifdef __ANDROID__
Yangster-mac932ecec2018-02-01 10:23:52 -080022TEST(AlarmMonitor, popSoonerThan) {
Yangster-mace2cd6d52017-11-09 20:38:30 -080023 std::string emptyMetricId;
24 std::string emptyDimensionId;
Yangster-mac932ecec2018-02-01 10:23:52 -080025 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> set;
26 AlarmMonitor am(2, [](const sp<IStatsCompanionService>&, int64_t){},
27 [](const sp<IStatsCompanionService>&){});
Bookatzece5f702017-10-09 14:59:38 -070028
29 set = am.popSoonerThan(5);
30 EXPECT_TRUE(set.empty());
31
Yangster-mac932ecec2018-02-01 10:23:52 -080032 sp<const InternalAlarm> a = new InternalAlarm{10};
33 sp<const InternalAlarm> b = new InternalAlarm{20};
34 sp<const InternalAlarm> c = new InternalAlarm{20};
35 sp<const InternalAlarm> d = new InternalAlarm{30};
36 sp<const InternalAlarm> e = new InternalAlarm{40};
37 sp<const InternalAlarm> f = new InternalAlarm{50};
Bookatzece5f702017-10-09 14:59:38 -070038
39 am.add(a);
40 am.add(b);
41 am.add(c);
42 am.add(d);
43 am.add(e);
44 am.add(f);
45
46 set = am.popSoonerThan(5);
47 EXPECT_TRUE(set.empty());
48
49 set = am.popSoonerThan(30);
50 EXPECT_EQ(4u, set.size());
51 EXPECT_EQ(1u, set.count(a));
52 EXPECT_EQ(1u, set.count(b));
53 EXPECT_EQ(1u, set.count(c));
54 EXPECT_EQ(1u, set.count(d));
55
56 set = am.popSoonerThan(60);
57 EXPECT_EQ(2u, set.size());
58 EXPECT_EQ(1u, set.count(e));
59 EXPECT_EQ(1u, set.count(f));
60
61 set = am.popSoonerThan(80);
62 EXPECT_EQ(0u, set.size());
63}
64
65#else
66GTEST_LOG_(INFO) << "This test does nothing.\n";
67#endif