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
15 #include <gtest/gtest.h>
16
17 #include "src/StatsLogProcessor.h"
18 #include "src/stats_log_util.h"
19 #include "tests/statsd_test_util.h"
20
21 #include <vector>
22
23 namespace android {
24 namespace os {
25 namespace statsd {
26
27 #ifdef __ANDROID__
28
29 namespace {
30
CreateStatsdConfig()31 StatsdConfig CreateStatsdConfig() {
32 StatsdConfig config;
33 config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
34
35 auto alarm = config.add_alarm();
36 alarm->set_id(123456);
37 alarm->set_offset_millis(TimeUnitToBucketSizeInMillis(TEN_MINUTES));
38 alarm->set_period_millis(TimeUnitToBucketSizeInMillis(ONE_HOUR));
39
40 alarm = config.add_alarm();
41 alarm->set_id(654321);
42 alarm->set_offset_millis(TimeUnitToBucketSizeInMillis(FIVE_MINUTES));
43 alarm->set_period_millis(TimeUnitToBucketSizeInMillis(THIRTY_MINUTES));
44 return config;
45 }
46
47 } // namespace
48
TEST(AlarmE2eTest,TestMultipleAlarms)49 TEST(AlarmE2eTest, TestMultipleAlarms) {
50 auto config = CreateStatsdConfig();
51 int64_t bucketStartTimeNs = 10000000000;
52
53 ConfigKey cfgKey;
54 auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
55 EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
56 EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid());
57 EXPECT_EQ(2u, processor->mMetricsManagers.begin()->second->mAllPeriodicAlarmTrackers.size());
58
59 auto alarmTracker1 = processor->mMetricsManagers.begin()->second->mAllPeriodicAlarmTrackers[0];
60 auto alarmTracker2 = processor->mMetricsManagers.begin()->second->mAllPeriodicAlarmTrackers[1];
61
62 int64_t alarmTimestampSec0 = bucketStartTimeNs / NS_PER_SEC + 10 * 60;
63 int64_t alarmTimestampSec1 = bucketStartTimeNs / NS_PER_SEC + 5 * 60;
64 EXPECT_EQ(alarmTimestampSec0, alarmTracker1->getAlarmTimestampSec());
65 EXPECT_EQ(alarmTimestampSec1, alarmTracker2->getAlarmTimestampSec());
66
67 // Alarm fired.
68 const int64_t alarmFiredTimestampSec0 = alarmTimestampSec1 + 5;
69 auto alarmSet = processor->getPeriodicAlarmMonitor()->popSoonerThan(
70 static_cast<uint32_t>(alarmFiredTimestampSec0));
71 EXPECT_EQ(1u, alarmSet.size());
72 processor->onPeriodicAlarmFired(alarmFiredTimestampSec0 * NS_PER_SEC, alarmSet);
73 EXPECT_EQ(alarmTimestampSec0, alarmTracker1->getAlarmTimestampSec());
74 EXPECT_EQ(alarmTimestampSec1 + 30 * 60, alarmTracker2->getAlarmTimestampSec());
75
76 // Alarms fired very late.
77 const int64_t alarmFiredTimestampSec1 = alarmTimestampSec0 + 2 * 60 * 60 + 125;
78 alarmSet = processor->getPeriodicAlarmMonitor()->popSoonerThan(
79 static_cast<uint32_t>(alarmFiredTimestampSec1));
80 EXPECT_EQ(2u, alarmSet.size());
81 processor->onPeriodicAlarmFired(alarmFiredTimestampSec1 * NS_PER_SEC, alarmSet);
82 EXPECT_EQ(alarmTimestampSec0 + 60 * 60 * 3, alarmTracker1->getAlarmTimestampSec());
83 EXPECT_EQ(alarmTimestampSec1 + 30 * 60 * 5, alarmTracker2->getAlarmTimestampSec());
84 }
85
86 #else
87 GTEST_LOG_(INFO) << "This test does nothing.\n";
88 #endif
89
90 } // namespace statsd
91 } // namespace os
92 } // namespace android
93