Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [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 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 15 | #include "src/metrics/GaugeMetricProducer.h" |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 16 | #include "src/stats_log_util.h" |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 17 | #include "logd/LogEvent.h" |
| 18 | #include "metrics_test_helper.h" |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 19 | #include "tests/statsd_test_util.h" |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 20 | |
| 21 | #include <gmock/gmock.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | #include <stdio.h> |
| 24 | #include <vector> |
| 25 | |
| 26 | using namespace testing; |
| 27 | using android::sp; |
| 28 | using std::set; |
| 29 | using std::unordered_map; |
| 30 | using std::vector; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 31 | using std::make_shared; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 32 | |
| 33 | #ifdef __ANDROID__ |
| 34 | |
| 35 | namespace android { |
| 36 | namespace os { |
| 37 | namespace statsd { |
| 38 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 39 | const ConfigKey kConfigKey(0, 12345); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 40 | const int tagId = 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 41 | const int64_t metricId = 123; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 42 | const int64_t bucketStartTimeNs = 10000000000; |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 43 | const int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 44 | const int64_t bucket2StartTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 45 | const int64_t bucket3StartTimeNs = bucketStartTimeNs + 2 * bucketSizeNs; |
| 46 | const int64_t bucket4StartTimeNs = bucketStartTimeNs + 3 * bucketSizeNs; |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 47 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 48 | TEST(GaugeMetricProducerTest, TestNoCondition) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 49 | GaugeMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 50 | metric.set_id(metricId); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 51 | metric.set_bucket(ONE_MINUTE); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 52 | metric.mutable_gauge_fields_filter()->set_include_all(false); |
| 53 | auto gaugeFieldMatcher = metric.mutable_gauge_fields_filter()->mutable_fields(); |
| 54 | gaugeFieldMatcher->set_field(tagId); |
| 55 | gaugeFieldMatcher->add_child()->set_field(1); |
| 56 | gaugeFieldMatcher->add_child()->set_field(3); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 57 | |
| 58 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 59 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 60 | // TODO: pending refactor of StatsPullerManager |
| 61 | // For now we still need this so that it doesn't do real pulling. |
| 62 | shared_ptr<MockStatsPullerManager> pullerManager = |
| 63 | make_shared<StrictMock<MockStatsPullerManager>>(); |
| 64 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _)).WillOnce(Return()); |
| 65 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 66 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 67 | GaugeMetricProducer gaugeProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Yangster-mac | a070b6a | 2018-01-04 13:28:38 -0800 | [diff] [blame] | 68 | tagId, bucketStartTimeNs, pullerManager); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 69 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 70 | vector<shared_ptr<LogEvent>> allData; |
| 71 | allData.clear(); |
| 72 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 73 | event->write(10); |
| 74 | event->write("some value"); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 75 | event->write(11); |
| 76 | event->init(); |
| 77 | allData.push_back(event); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 78 | |
| 79 | gaugeProducer.onDataPulled(allData); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 80 | EXPECT_EQ(1UL, gaugeProducer.mCurrentSlicedBucket->size()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 81 | auto it = gaugeProducer.mCurrentSlicedBucket->begin()->second->begin(); |
| 82 | EXPECT_EQ(10, it->second.value_int()); |
| 83 | it++; |
| 84 | EXPECT_EQ(11, it->second.value_int()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 85 | EXPECT_EQ(0UL, gaugeProducer.mPastBuckets.size()); |
| 86 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 87 | allData.clear(); |
| 88 | std::shared_ptr<LogEvent> event2 = |
| 89 | std::make_shared<LogEvent>(tagId, bucket3StartTimeNs + 10); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 90 | event2->write(24); |
| 91 | event2->write("some value"); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 92 | event2->write(25); |
| 93 | event2->init(); |
| 94 | allData.push_back(event2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 95 | gaugeProducer.onDataPulled(allData); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 96 | EXPECT_EQ(1UL, gaugeProducer.mCurrentSlicedBucket->size()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 97 | it = gaugeProducer.mCurrentSlicedBucket->begin()->second->begin(); |
| 98 | EXPECT_EQ(24, it->second.value_int()); |
| 99 | it++; |
| 100 | EXPECT_EQ(25, it->second.value_int()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 101 | // One dimension. |
| 102 | EXPECT_EQ(1UL, gaugeProducer.mPastBuckets.size()); |
| 103 | EXPECT_EQ(1UL, gaugeProducer.mPastBuckets.begin()->second.size()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 104 | it = gaugeProducer.mPastBuckets.begin()->second.back().mGaugeFields->begin(); |
| 105 | EXPECT_EQ(10L, it->second.value_int()); |
| 106 | it++; |
| 107 | EXPECT_EQ(11L, it->second.value_int()); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 108 | EXPECT_EQ(1UL, gaugeProducer.mPastBuckets.begin()->second.back().mBucketNum); |
| 109 | |
| 110 | gaugeProducer.flushIfNeededLocked(bucket4StartTimeNs); |
| 111 | EXPECT_EQ(0UL, gaugeProducer.mCurrentSlicedBucket->size()); |
| 112 | // One dimension. |
| 113 | EXPECT_EQ(1UL, gaugeProducer.mPastBuckets.size()); |
| 114 | EXPECT_EQ(2UL, gaugeProducer.mPastBuckets.begin()->second.size()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 115 | it = gaugeProducer.mPastBuckets.begin()->second.back().mGaugeFields->begin(); |
| 116 | EXPECT_EQ(24L, it->second.value_int()); |
| 117 | it++; |
| 118 | EXPECT_EQ(25L, it->second.value_int()); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 119 | EXPECT_EQ(2UL, gaugeProducer.mPastBuckets.begin()->second.back().mBucketNum); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 122 | TEST(GaugeMetricProducerTest, TestWithCondition) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 123 | GaugeMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 124 | metric.set_id(metricId); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 125 | metric.set_bucket(ONE_MINUTE); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 126 | auto gaugeFieldMatcher = metric.mutable_gauge_fields_filter()->mutable_fields(); |
| 127 | gaugeFieldMatcher->set_field(tagId); |
| 128 | gaugeFieldMatcher->add_child()->set_field(2); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 129 | metric.set_condition(StringToId("SCREEN_ON")); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 130 | |
| 131 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 132 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 133 | shared_ptr<MockStatsPullerManager> pullerManager = |
| 134 | make_shared<StrictMock<MockStatsPullerManager>>(); |
| 135 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _)).WillOnce(Return()); |
| 136 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); |
| 137 | EXPECT_CALL(*pullerManager, Pull(tagId, _)) |
| 138 | .WillOnce(Invoke([](int tagId, vector<std::shared_ptr<LogEvent>>* data) { |
| 139 | data->clear(); |
| 140 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucketStartTimeNs + 10); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 141 | event->write("some value"); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 142 | event->write(100); |
| 143 | event->init(); |
| 144 | data->push_back(event); |
| 145 | return true; |
| 146 | })); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 147 | |
Yangster-mac | a070b6a | 2018-01-04 13:28:38 -0800 | [diff] [blame] | 148 | GaugeMetricProducer gaugeProducer(kConfigKey, metric, 1, wizard, tagId, |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 149 | bucketStartTimeNs, pullerManager); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 150 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 151 | gaugeProducer.onConditionChanged(true, bucketStartTimeNs + 8); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 152 | EXPECT_EQ(1UL, gaugeProducer.mCurrentSlicedBucket->size()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 153 | EXPECT_EQ(100, |
| 154 | gaugeProducer.mCurrentSlicedBucket->begin()->second->begin()->second.value_int()); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 155 | EXPECT_EQ(0UL, gaugeProducer.mPastBuckets.size()); |
| 156 | |
| 157 | vector<shared_ptr<LogEvent>> allData; |
| 158 | allData.clear(); |
| 159 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 160 | event->write("some value"); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 161 | event->write(110); |
| 162 | event->init(); |
| 163 | allData.push_back(event); |
| 164 | gaugeProducer.onDataPulled(allData); |
| 165 | |
| 166 | EXPECT_EQ(1UL, gaugeProducer.mCurrentSlicedBucket->size()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 167 | EXPECT_EQ(110, |
| 168 | gaugeProducer.mCurrentSlicedBucket->begin()->second->begin()->second.value_int()); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 169 | EXPECT_EQ(1UL, gaugeProducer.mPastBuckets.size()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 170 | EXPECT_EQ(100, gaugeProducer.mPastBuckets.begin()->second.back() |
| 171 | .mGaugeFields->begin()->second.value_int()); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 172 | |
| 173 | gaugeProducer.onConditionChanged(false, bucket2StartTimeNs + 10); |
| 174 | gaugeProducer.flushIfNeededLocked(bucket3StartTimeNs + 10); |
| 175 | EXPECT_EQ(1UL, gaugeProducer.mPastBuckets.size()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 176 | EXPECT_EQ(2UL, gaugeProducer.mPastBuckets.begin()->second.size()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 177 | EXPECT_EQ(110L, gaugeProducer.mPastBuckets.begin()->second.back() |
| 178 | .mGaugeFields->begin()->second.value_int()); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 179 | EXPECT_EQ(1UL, gaugeProducer.mPastBuckets.begin()->second.back().mBucketNum); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | TEST(GaugeMetricProducerTest, TestAnomalyDetection) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 183 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 184 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 185 | shared_ptr<MockStatsPullerManager> pullerManager = |
| 186 | make_shared<StrictMock<MockStatsPullerManager>>(); |
| 187 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _)).WillOnce(Return()); |
| 188 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); |
| 189 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 190 | GaugeMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 191 | metric.set_id(metricId); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 192 | metric.set_bucket(ONE_MINUTE); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 193 | auto gaugeFieldMatcher = metric.mutable_gauge_fields_filter()->mutable_fields(); |
| 194 | gaugeFieldMatcher->set_field(tagId); |
| 195 | gaugeFieldMatcher->add_child()->set_field(2); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 196 | GaugeMetricProducer gaugeProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Yangster-mac | a070b6a | 2018-01-04 13:28:38 -0800 | [diff] [blame] | 197 | tagId, bucketStartTimeNs, pullerManager); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 198 | |
| 199 | Alert alert; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 200 | alert.set_id(101); |
| 201 | alert.set_metric_id(metricId); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 202 | alert.set_trigger_if_sum_gt(25); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 203 | alert.set_num_buckets(2); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 204 | const int32_t refPeriodSec = 60; |
| 205 | alert.set_refractory_period_secs(refPeriodSec); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 206 | sp<AnomalyTracker> anomalyTracker = gaugeProducer.addAnomalyTracker(alert); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 207 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 208 | int tagId = 1; |
| 209 | std::shared_ptr<LogEvent> event1 = std::make_shared<LogEvent>(tagId, bucketStartTimeNs + 1); |
| 210 | event1->write("some value"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 211 | event1->write(13); |
| 212 | event1->init(); |
| 213 | |
| 214 | gaugeProducer.onDataPulled({event1}); |
| 215 | EXPECT_EQ(1UL, gaugeProducer.mCurrentSlicedBucket->size()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 216 | EXPECT_EQ(13L, |
| 217 | gaugeProducer.mCurrentSlicedBucket->begin()->second->begin()->second.value_int()); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 218 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_DIMENSION_KEY), 0U); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 219 | |
| 220 | std::shared_ptr<LogEvent> event2 = |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 221 | std::make_shared<LogEvent>(tagId, bucketStartTimeNs + bucketSizeNs + 20); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 222 | event2->write("some value"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 223 | event2->write(15); |
| 224 | event2->init(); |
| 225 | |
| 226 | gaugeProducer.onDataPulled({event2}); |
| 227 | EXPECT_EQ(1UL, gaugeProducer.mCurrentSlicedBucket->size()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 228 | EXPECT_EQ(15L, |
| 229 | gaugeProducer.mCurrentSlicedBucket->begin()->second->begin()->second.value_int()); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 230 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_DIMENSION_KEY), |
| 231 | event2->GetTimestampNs() / NS_PER_SEC + refPeriodSec); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 232 | |
| 233 | std::shared_ptr<LogEvent> event3 = |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 234 | std::make_shared<LogEvent>(tagId, bucketStartTimeNs + 2 * bucketSizeNs + 10); |
| 235 | event3->write("some value"); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 236 | event3->write(26); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 237 | event3->init(); |
| 238 | |
| 239 | gaugeProducer.onDataPulled({event3}); |
| 240 | EXPECT_EQ(1UL, gaugeProducer.mCurrentSlicedBucket->size()); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 241 | EXPECT_EQ(26L, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 242 | gaugeProducer.mCurrentSlicedBucket->begin()->second->begin()->second.value_int()); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 243 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_DIMENSION_KEY), |
| 244 | event2->GetTimestampNs() / NS_PER_SEC + refPeriodSec); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 245 | |
| 246 | // The event4 does not have the gauge field. Thus the current bucket value is 0. |
| 247 | std::shared_ptr<LogEvent> event4 = |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 248 | std::make_shared<LogEvent>(tagId, bucketStartTimeNs + 3 * bucketSizeNs + 10); |
| 249 | event4->write("some value"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 250 | event4->init(); |
| 251 | gaugeProducer.onDataPulled({event4}); |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 252 | EXPECT_EQ(1UL, gaugeProducer.mCurrentSlicedBucket->size()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 253 | EXPECT_TRUE(gaugeProducer.mCurrentSlicedBucket->begin()->second->empty()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | } // namespace statsd |
| 257 | } // namespace os |
| 258 | } // namespace android |
| 259 | #else |
| 260 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 261 | #endif |