This is part one of a mult-part change to revise some metrics. Several metrics are currently EventMetric type when they should be a simpler type. Test: Added unit tests for the new types. Also, re-ran existing tests. Verified playback works with Google Play, and re-ran Widevine GTS tests. Bug: 36220619 Change-Id: I2ec8fc355f66ad4834dd722aacd22541fb9c94ad
178 lines
5.4 KiB
C++
178 lines
5.4 KiB
C++
// Copyright 2017 Google Inc. All Rights Reserved.
|
|
//
|
|
// Unit tests for CounterMetric
|
|
|
|
#include "counter_metric.h"
|
|
|
|
#include "gmock/gmock.h"
|
|
#include "gtest/gtest.h"
|
|
#include "metric_serialization.h"
|
|
#include "scoped_ptr.h"
|
|
|
|
using testing::IsNull;
|
|
using testing::NotNull;
|
|
|
|
namespace wvcdm {
|
|
namespace metrics {
|
|
|
|
class MockCounterMetricSerializer : public MetricSerializer {
|
|
public:
|
|
MOCK_METHOD2(SetString, void(const std::string& metric_id,
|
|
const std::string& value));
|
|
MOCK_METHOD2(SetInt32, void(const std::string& metric_id,
|
|
int32_t value));
|
|
MOCK_METHOD2(SetInt64, void(const std::string& metric_id,
|
|
int64_t value));
|
|
MOCK_METHOD2(SetDouble, void(const std::string& metric_id,
|
|
double value));
|
|
};
|
|
|
|
class CounterMetricTest : public ::testing::Test {
|
|
public:
|
|
void SetUp() {
|
|
mock_serializer_.reset(new MockCounterMetricSerializer());
|
|
}
|
|
protected:
|
|
template<typename F1,
|
|
typename F2,
|
|
typename F3,
|
|
typename F4>
|
|
const std::map<std::string, int64_t>
|
|
GetValueMap(
|
|
const wvcdm::metrics::CounterMetric<F1, F2, F3, F4>&
|
|
metric) {
|
|
return metric.value_map_;
|
|
}
|
|
|
|
scoped_ptr<MockCounterMetricSerializer> mock_serializer_;
|
|
};
|
|
|
|
TEST_F(CounterMetricTest, NoFieldsSuccessNullCallback) {
|
|
wvcdm::metrics::CounterMetric<> metric("no/fields/metric");
|
|
metric.Increment();
|
|
metric.Increment(10);
|
|
|
|
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
|
ASSERT_EQ(1u, GetValueMap(metric).size());
|
|
EXPECT_EQ(11, value_map.begin()->second);
|
|
EXPECT_EQ("", value_map.begin()->first);
|
|
}
|
|
|
|
TEST_F(CounterMetricTest, NoFieldsSuccessWithCallback) {
|
|
wvcdm::metrics::CounterMetric<> metric("no/fields/metric");
|
|
EXPECT_CALL(*mock_serializer_,
|
|
SetInt64("no/fields/metric/count", 11));
|
|
|
|
metric.Increment();
|
|
metric.Increment(10);
|
|
metric.Serialize(mock_serializer_.get());
|
|
|
|
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
|
ASSERT_EQ(1u, GetValueMap(metric).size());
|
|
EXPECT_EQ(11, value_map.begin()->second);
|
|
EXPECT_EQ("", value_map.begin()->first);
|
|
}
|
|
|
|
TEST_F(CounterMetricTest, NoFieldsSuccessSingleIncrementWithCallback) {
|
|
wvcdm::metrics::CounterMetric<> metric("no/fields/metric");
|
|
EXPECT_CALL(*mock_serializer_,
|
|
SetInt64("no/fields/metric/count", 1));
|
|
|
|
metric.Increment();
|
|
metric.Serialize(mock_serializer_.get());
|
|
|
|
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
|
ASSERT_EQ(1u, GetValueMap(metric).size());
|
|
EXPECT_EQ(1, value_map.begin()->second);
|
|
EXPECT_EQ("", value_map.begin()->first);
|
|
}
|
|
|
|
TEST_F(CounterMetricTest, OneFieldSuccessNoCallback) {
|
|
wvcdm::metrics::CounterMetric<int> metric(
|
|
"single/fields/metric",
|
|
"error_code");
|
|
metric.Increment(7);
|
|
metric.Increment(10, 7);
|
|
metric.Increment(13);
|
|
metric.Increment(20, 13);
|
|
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
|
ASSERT_EQ(2u, GetValueMap(metric).size());
|
|
|
|
// Verify both instances.
|
|
EXPECT_EQ(11, value_map["{error_code:7}"]);
|
|
EXPECT_EQ(21, value_map["{error_code:13}"]);
|
|
}
|
|
|
|
TEST_F(CounterMetricTest, TwoFieldsSuccess) {
|
|
wvcdm::metrics::CounterMetric<int, int> metric(
|
|
"two/fields/metric",
|
|
"error_code",
|
|
"size");
|
|
metric.Increment(7, 23);
|
|
metric.Increment(2, 7, 29);
|
|
metric.Increment(3, 11, 23);
|
|
metric.Increment(4, 11, 29);
|
|
metric.Increment(5, 7, 23);
|
|
metric.Increment(-5, 7, 29);
|
|
|
|
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
|
ASSERT_EQ(4u, GetValueMap(metric).size());
|
|
|
|
// Verify all instances.
|
|
EXPECT_EQ(6, value_map["{error_code:7&size:23}"]);
|
|
EXPECT_EQ(-3, value_map["{error_code:7&size:29}"]);
|
|
EXPECT_EQ(3, value_map["{error_code:11&size:23}"]);
|
|
EXPECT_EQ(4, value_map["{error_code:11&size:29}"]);
|
|
|
|
// Verify that a non-existent distribution returns default 0
|
|
EXPECT_EQ(0, value_map["error_code:1,size:1"]);
|
|
}
|
|
|
|
TEST_F(CounterMetricTest, TwoFieldsSuccessWithCallback) {
|
|
wvcdm::metrics::CounterMetric<int, std::string> metric("two/fields/metric",
|
|
"error_code",
|
|
"stringval");
|
|
|
|
EXPECT_CALL(
|
|
*mock_serializer_,
|
|
SetInt64("two/fields/metric/count{error_code:11&stringval:foo}", 5));
|
|
metric.Increment(11, "foo");
|
|
metric.Increment(4, 11, "foo");
|
|
metric.Serialize(mock_serializer_.get());
|
|
}
|
|
|
|
TEST_F(CounterMetricTest, ThreeFieldsSuccess) {
|
|
wvcdm::metrics::CounterMetric<int, int, bool> metric(
|
|
"three/fields/metric",
|
|
"error_code",
|
|
"size",
|
|
"woke up happy");
|
|
metric.Increment(7, 13, false);
|
|
|
|
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
|
ASSERT_EQ(1u, GetValueMap(metric).size());
|
|
EXPECT_EQ("{error_code:7&size:13&woke up happy:0}",
|
|
value_map.begin()->first);
|
|
EXPECT_EQ(1, value_map.begin()->second);
|
|
}
|
|
|
|
TEST_F(CounterMetricTest, FourFieldsSuccess) {
|
|
wvcdm::metrics::CounterMetric<int, int, bool, std::string> metric(
|
|
"Four/fields/metric",
|
|
"error_code",
|
|
"size",
|
|
"woke up happy",
|
|
"horoscope");
|
|
metric.Increment(10LL, 7, 13, true, "find your true love");
|
|
|
|
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
|
ASSERT_EQ(1u, GetValueMap(metric).size());
|
|
EXPECT_EQ(
|
|
"{error_code:7&size:13&woke up happy:1&horoscope:find your true love}",
|
|
value_map.begin()->first);
|
|
EXPECT_EQ(10, value_map.begin()->second);
|
|
}
|
|
|
|
} // namespace metrics
|
|
} // namespace wvcdm
|