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 "src/metrics/duration_helper/MaxDurationTracker.h"
16 #include "src/condition/ConditionWizard.h"
17 #include "metrics_test_helper.h"
18 #include "tests/statsd_test_util.h"
19 
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <stdio.h>
23 #include <set>
24 #include <unordered_map>
25 #include <vector>
26 
27 using namespace android::os::statsd;
28 using namespace testing;
29 using android::sp;
30 using std::set;
31 using std::unordered_map;
32 using std::vector;
33 
34 #ifdef __ANDROID__
35 
36 namespace android {
37 namespace os {
38 namespace statsd {
39 
40 const ConfigKey kConfigKey(0, 12345);
41 
42 const int TagId = 1;
43 
44 const HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 0, "1");
45 const HashableDimensionKey conditionKey = getMockedDimensionKey(TagId, 4, "1");
46 const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1");
47 const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2");
48 const int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
49 
TEST(MaxDurationTrackerTest,TestSimpleMaxDuration)50 TEST(MaxDurationTrackerTest, TestSimpleMaxDuration) {
51     const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "1");
52     const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1");
53     const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2");
54 
55 
56     sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
57 
58     unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
59 
60     int64_t bucketStartTimeNs = 10000000000;
61     int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
62     int64_t bucketNum = 0;
63 
64     int64_t metricId = 1;
65     MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
66                                bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {});
67 
68     tracker.noteStart(key1, true, bucketStartTimeNs, ConditionKey());
69     // Event starts again. This would not change anything as it already starts.
70     tracker.noteStart(key1, true, bucketStartTimeNs + 3, ConditionKey());
71     // Stopped.
72     tracker.noteStop(key1, bucketStartTimeNs + 10, false);
73 
74     // Another event starts in this bucket.
75     tracker.noteStart(key2, true, bucketStartTimeNs + 20, ConditionKey());
76     tracker.noteStop(key2, bucketStartTimeNs + 40, false /*stop all*/);
77 
78     tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets);
79     EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
80     ASSERT_EQ(1u, buckets[eventKey].size());
81     EXPECT_EQ(20LL, buckets[eventKey][0].mDuration);
82 }
83 
TEST(MaxDurationTrackerTest,TestStopAll)84 TEST(MaxDurationTrackerTest, TestStopAll) {
85     const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "1");
86     const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1");
87     const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2");
88 
89     sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
90 
91     unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
92 
93     int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
94     int64_t bucketStartTimeNs = 10000000000;
95     int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
96     int64_t bucketNum = 0;
97 
98     int64_t metricId = 1;
99     MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
100                                bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {});
101 
102     tracker.noteStart(key1, true, bucketStartTimeNs + 1, ConditionKey());
103 
104     // Another event starts in this bucket.
105     tracker.noteStart(key2, true, bucketStartTimeNs + 20, ConditionKey());
106     tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 40, &buckets);
107     tracker.noteStopAll(bucketStartTimeNs + bucketSizeNs + 40);
108     EXPECT_TRUE(tracker.mInfos.empty());
109     EXPECT_TRUE(buckets.find(eventKey) == buckets.end());
110 
111     tracker.flushIfNeeded(bucketStartTimeNs + 3 * bucketSizeNs + 40, &buckets);
112     EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
113     ASSERT_EQ(1u, buckets[eventKey].size());
114     EXPECT_EQ(bucketSizeNs + 40 - 1, buckets[eventKey][0].mDuration);
115     EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[eventKey][0].mBucketStartNs);
116     EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[eventKey][0].mBucketEndNs);
117 }
118 
TEST(MaxDurationTrackerTest,TestCrossBucketBoundary)119 TEST(MaxDurationTrackerTest, TestCrossBucketBoundary) {
120     const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "1");
121     const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1");
122     const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2");
123     sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
124 
125     unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
126 
127     int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
128     int64_t bucketStartTimeNs = 10000000000;
129     int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
130     int64_t bucketNum = 0;
131 
132     int64_t metricId = 1;
133     MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
134                                bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {});
135 
136     // The event starts.
137     tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, ConditionKey());
138 
139     // Starts again. Does not DEFAULT_DIMENSION_KEY anything.
140     tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + bucketSizeNs + 1,
141                       ConditionKey());
142 
143     // The event stops at early 4th bucket.
144     // Notestop is called from DurationMetricProducer's onMatchedLogEvent, which calls
145     // flushIfneeded.
146     tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 20, &buckets);
147     tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + (3 * bucketSizeNs) + 20,
148                      false /*stop all*/);
149     EXPECT_TRUE(buckets.find(eventKey) == buckets.end());
150 
151     tracker.flushIfNeeded(bucketStartTimeNs + 4 * bucketSizeNs, &buckets);
152     ASSERT_EQ(1u, buckets[eventKey].size());
153     EXPECT_EQ((3 * bucketSizeNs) + 20 - 1, buckets[eventKey][0].mDuration);
154     EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs, buckets[eventKey][0].mBucketStartNs);
155     EXPECT_EQ(bucketStartTimeNs + 4 * bucketSizeNs, buckets[eventKey][0].mBucketEndNs);
156 }
157 
TEST(MaxDurationTrackerTest,TestCrossBucketBoundary_nested)158 TEST(MaxDurationTrackerTest, TestCrossBucketBoundary_nested) {
159     const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "1");
160     const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1");
161     const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2");
162     sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
163 
164     unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
165 
166     int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
167     int64_t bucketStartTimeNs = 10000000000;
168     int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
169     int64_t bucketNum = 0;
170 
171     int64_t metricId = 1;
172     MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, true, bucketStartTimeNs,
173                                bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {});
174 
175     // 2 starts
176     tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, ConditionKey());
177     tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 10, ConditionKey());
178     // one stop
179     tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + 20, false /*stop all*/);
180 
181     tracker.flushIfNeeded(bucketStartTimeNs + (2 * bucketSizeNs) + 1, &buckets);
182     // Because of nesting, still not stopped.
183     EXPECT_TRUE(buckets.find(eventKey) == buckets.end());
184 
185     // real stop now.
186     tracker.noteStop(DEFAULT_DIMENSION_KEY,
187                      bucketStartTimeNs + (2 * bucketSizeNs) + 5, false);
188     tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 1, &buckets);
189 
190     ASSERT_EQ(1u, buckets[eventKey].size());
191     EXPECT_EQ(2 * bucketSizeNs + 5 - 1, buckets[eventKey][0].mDuration);
192 }
193 
TEST(MaxDurationTrackerTest,TestMaxDurationWithCondition)194 TEST(MaxDurationTrackerTest, TestMaxDurationWithCondition) {
195     const HashableDimensionKey conditionDimKey = key1;
196 
197     sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
198 
199     ConditionKey conditionKey1;
200     MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 1, "1");
201     conditionKey1[StringToId("APP_BACKGROUND")] = conditionDimKey;
202 
203     /**
204     Start in first bucket, stop in second bucket. Condition turns on and off in the first bucket
205     and again turns on and off in the second bucket.
206     */
207     int64_t bucketStartTimeNs = 10000000000;
208     int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
209     int64_t eventStartTimeNs = bucketStartTimeNs + 1 * NS_PER_SEC;
210     int64_t conditionStarts1 = bucketStartTimeNs + 11 * NS_PER_SEC;
211     int64_t conditionStops1 = bucketStartTimeNs + 14 * NS_PER_SEC;
212     int64_t conditionStarts2 = bucketStartTimeNs + bucketSizeNs + 5 * NS_PER_SEC;
213     int64_t conditionStops2 = conditionStarts2 + 10 * NS_PER_SEC;
214     int64_t eventStopTimeNs = conditionStops2 + 8 * NS_PER_SEC;
215 
216     int64_t metricId = 1;
217     MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs,
218                                0, bucketStartTimeNs, bucketSizeNs, true, false, {});
219     EXPECT_TRUE(tracker.mAnomalyTrackers.empty());
220 
221     tracker.noteStart(key1, false, eventStartTimeNs, conditionKey1);
222     tracker.noteConditionChanged(key1, true, conditionStarts1);
223     tracker.noteConditionChanged(key1, false, conditionStops1);
224     unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
225     tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets);
226     ASSERT_EQ(0U, buckets.size());
227 
228     tracker.noteConditionChanged(key1, true, conditionStarts2);
229     tracker.noteConditionChanged(key1, false, conditionStops2);
230     tracker.noteStop(key1, eventStopTimeNs, false);
231     tracker.flushIfNeeded(bucketStartTimeNs + 2 * bucketSizeNs + 1, &buckets);
232     ASSERT_EQ(1U, buckets.size());
233     vector<DurationBucket> item = buckets.begin()->second;
234     ASSERT_EQ(1UL, item.size());
235     EXPECT_EQ((int64_t)(13LL * NS_PER_SEC), item[0].mDuration);
236 }
237 
TEST(MaxDurationTrackerTest,TestAnomalyDetection)238 TEST(MaxDurationTrackerTest, TestAnomalyDetection) {
239     sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
240 
241     ConditionKey conditionKey1;
242     MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 2, "maps");
243     conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey;
244 
245     unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
246 
247     int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
248     int64_t bucketStartTimeNs = 10000000000;
249     int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
250     int64_t bucketNum = 0;
251     int64_t eventStartTimeNs = 13000000000;
252     int64_t durationTimeNs = 2 * 1000;
253 
254     int64_t metricId = 1;
255     Alert alert;
256     alert.set_id(101);
257     alert.set_metric_id(1);
258     alert.set_trigger_if_sum_gt(40 * NS_PER_SEC);
259     alert.set_num_buckets(2);
260     const int32_t refPeriodSec = 45;
261     alert.set_refractory_period_secs(refPeriodSec);
262     sp<AlarmMonitor> alarmMonitor;
263     sp<DurationAnomalyTracker> anomalyTracker =
264         new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor);
265     MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs,
266                                bucketNum, bucketStartTimeNs, bucketSizeNs, true, false,
267                                {anomalyTracker});
268 
269     tracker.noteStart(key1, true, eventStartTimeNs, conditionKey1);
270     sp<const InternalAlarm> alarm = anomalyTracker->mAlarms.begin()->second;
271     EXPECT_EQ((long long)(53ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC));
272 
273     // Remove the anomaly alarm when the duration is no longer fully met.
274     tracker.noteConditionChanged(key1, false, eventStartTimeNs + 15 * NS_PER_SEC);
275     ASSERT_EQ(0U, anomalyTracker->mAlarms.size());
276 
277     // Since the condition was off for 10 seconds, the anomaly should trigger 10 sec later.
278     tracker.noteConditionChanged(key1, true, eventStartTimeNs + 25 * NS_PER_SEC);
279     ASSERT_EQ(1U, anomalyTracker->mAlarms.size());
280     alarm = anomalyTracker->mAlarms.begin()->second;
281     EXPECT_EQ((long long)(63ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC));
282 }
283 
284 // This tests that we correctly compute the predicted time of an anomaly assuming that the current
285 // state continues forward as-is.
TEST(MaxDurationTrackerTest,TestAnomalyPredictedTimestamp)286 TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp) {
287     sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
288 
289     ConditionKey conditionKey1;
290     MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 2, "maps");
291     conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey;
292     ConditionKey conditionKey2;
293     conditionKey2[StringToId("APP_BACKGROUND")] = getMockedDimensionKey(TagId, 4, "2");
294 
295     unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
296 
297     /**
298      * Suppose we have two sub-dimensions that we're taking the MAX over. In the first of these
299      * nested dimensions, we enter the pause state after 3 seconds. When we resume, the second
300      * dimension has already been running for 4 seconds. Thus, we have 40-4=36 seconds remaining
301      * before we trigger the anomaly.
302      */
303     int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
304     int64_t bucketStartTimeNs = 10000000000;
305     int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
306     int64_t bucketNum = 0;
307     int64_t eventStartTimeNs = bucketStartTimeNs + 5 * NS_PER_SEC;  // Condition is off at start.
308     int64_t conditionStarts1 = bucketStartTimeNs + 11 * NS_PER_SEC;
309     int64_t conditionStops1 = bucketStartTimeNs + 14 * NS_PER_SEC;
310     int64_t conditionStarts2 = bucketStartTimeNs + 20 * NS_PER_SEC;
311     int64_t eventStartTimeNs2 = conditionStarts2 - 4 * NS_PER_SEC;
312 
313     int64_t metricId = 1;
314     Alert alert;
315     alert.set_id(101);
316     alert.set_metric_id(1);
317     alert.set_trigger_if_sum_gt(40 * NS_PER_SEC);
318     alert.set_num_buckets(2);
319     const int32_t refPeriodSec = 45;
320     alert.set_refractory_period_secs(refPeriodSec);
321     sp<AlarmMonitor> alarmMonitor;
322     sp<DurationAnomalyTracker> anomalyTracker =
323         new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor);
324     MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs,
325                                bucketNum, bucketStartTimeNs, bucketSizeNs, true, false,
326                                {anomalyTracker});
327 
328     tracker.noteStart(key1, false, eventStartTimeNs, conditionKey1);
329     tracker.noteConditionChanged(key1, true, conditionStarts1);
330     tracker.noteConditionChanged(key1, false, conditionStops1);
331     tracker.noteStart(key2, true, eventStartTimeNs2, conditionKey2);  // Condition is on already.
332     tracker.noteConditionChanged(key1, true, conditionStarts2);
333     ASSERT_EQ(1U, anomalyTracker->mAlarms.size());
334     auto alarm = anomalyTracker->mAlarms.begin()->second;
335     int64_t anomalyFireTimeSec = alarm->timestampSec;
336     EXPECT_EQ(conditionStarts2 + 36 * NS_PER_SEC,
337             (long long)anomalyFireTimeSec * NS_PER_SEC);
338 
339     // Now we test the calculation now that there's a refractory period.
340     // At the correct time, declare the anomaly. This will set a refractory period. Make sure it
341     // gets correctly taken into account in future predictAnomalyTimestampNs calculations.
342     std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> firedAlarms({alarm});
343     anomalyTracker->informAlarmsFired(anomalyFireTimeSec * NS_PER_SEC, firedAlarms);
344     ASSERT_EQ(0u, anomalyTracker->mAlarms.size());
345     int64_t refractoryPeriodEndsSec = anomalyFireTimeSec + refPeriodSec;
346     EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), refractoryPeriodEndsSec);
347 
348     // Now stop and start again. Make sure the new predictAnomalyTimestampNs takes into account
349     // the refractory period correctly.
350     int64_t eventStopTimeNs = anomalyFireTimeSec * NS_PER_SEC + 10;
351     tracker.noteStop(key1, eventStopTimeNs, false);
352     tracker.noteStop(key2, eventStopTimeNs, false);
353     tracker.noteStart(key1, true, eventStopTimeNs + 1000000, conditionKey1);
354     // Anomaly is ongoing, but we're still in the refractory period.
355     ASSERT_EQ(1U, anomalyTracker->mAlarms.size());
356     alarm = anomalyTracker->mAlarms.begin()->second;
357     EXPECT_EQ(refractoryPeriodEndsSec, (long long)(alarm->timestampSec));
358 
359     // Makes sure it is correct after the refractory period is over.
360     tracker.noteStop(key1, eventStopTimeNs + 2000000, false);
361     int64_t justBeforeRefPeriodNs = (refractoryPeriodEndsSec - 2) * NS_PER_SEC;
362     tracker.noteStart(key1, true, justBeforeRefPeriodNs, conditionKey1);
363     alarm = anomalyTracker->mAlarms.begin()->second;
364     EXPECT_EQ(justBeforeRefPeriodNs + 40 * NS_PER_SEC,
365                 (unsigned long long)(alarm->timestampSec * NS_PER_SEC));
366 }
367 
368 // Suppose that within one tracker there are two dimensions A and B.
369 // Suppose A starts, then B starts, and then A stops. We still need to set an anomaly based on the
370 // elapsed duration of B.
TEST(MaxDurationTrackerTest,TestAnomalyPredictedTimestamp_UpdatedOnStop)371 TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp_UpdatedOnStop) {
372     sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
373 
374     ConditionKey conditionKey1;
375     MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 2, "maps");
376     conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey;
377     ConditionKey conditionKey2;
378     conditionKey2[StringToId("APP_BACKGROUND")] = getMockedDimensionKey(TagId, 4, "2");
379 
380     unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
381 
382     /**
383      * Suppose we have two sub-dimensions that we're taking the MAX over. In the first of these
384      * nested dimensions, are started for 8 seconds. When we stop, the other nested dimension has
385      * been started for 5 seconds. So we can only allow 35 more seconds from now.
386      */
387     int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
388     int64_t bucketStartTimeNs = 10000000000;
389     int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
390     int64_t bucketNum = 0;
391     int64_t eventStartTimeNs1 = bucketStartTimeNs + 5 * NS_PER_SEC;  // Condition is off at start.
392     int64_t eventStopTimeNs1 = bucketStartTimeNs + 13 * NS_PER_SEC;
393     int64_t eventStartTimeNs2 = bucketStartTimeNs + 8 * NS_PER_SEC;
394 
395     int64_t metricId = 1;
396     Alert alert;
397     alert.set_id(101);
398     alert.set_metric_id(1);
399     alert.set_trigger_if_sum_gt(40 * NS_PER_SEC);
400     alert.set_num_buckets(2);
401     const int32_t refPeriodSec = 45;
402     alert.set_refractory_period_secs(refPeriodSec);
403     sp<AlarmMonitor> alarmMonitor;
404     sp<DurationAnomalyTracker> anomalyTracker =
405         new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor);
406     MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs,
407                                bucketNum, bucketStartTimeNs, bucketSizeNs, true, false,
408                                {anomalyTracker});
409 
410     tracker.noteStart(key1, true, eventStartTimeNs1, conditionKey1);
411     tracker.noteStart(key2, true, eventStartTimeNs2, conditionKey2);
412     tracker.noteStop(key1, eventStopTimeNs1, false);
413     ASSERT_EQ(1U, anomalyTracker->mAlarms.size());
414     auto alarm = anomalyTracker->mAlarms.begin()->second;
415     EXPECT_EQ(eventStopTimeNs1 + 35 * NS_PER_SEC,
416               (unsigned long long)(alarm->timestampSec * NS_PER_SEC));
417 }
418 
419 }  // namespace statsd
420 }  // namespace os
421 }  // namespace android
422 #else
423 GTEST_LOG_(INFO) << "This test does nothing.\n";
424 #endif
425