|
| 1 | +// Copyright 2018, OpenCensus Authors |
| 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 "opencensus/stats/view_data.h" |
| 16 | + |
| 17 | +#include "absl/time/time.h" |
| 18 | +#include "gmock/gmock.h" |
| 19 | +#include "gtest/gtest.h" |
| 20 | +#include "opencensus/stats/aggregation.h" |
| 21 | +#include "opencensus/stats/aggregation_window.h" |
| 22 | +#include "opencensus/stats/bucket_boundaries.h" |
| 23 | +#include "opencensus/stats/distribution.h" |
| 24 | +#include "opencensus/stats/internal/view_data_impl.h" |
| 25 | +#include "opencensus/stats/testing/test_utils.h" |
| 26 | +#include "opencensus/stats/view_descriptor.h" |
| 27 | + |
| 28 | +namespace opencensus { |
| 29 | +namespace stats { |
| 30 | +namespace { |
| 31 | + |
| 32 | +TEST(ViewDataTest, CumulativeSum) { |
| 33 | + const auto descriptor = |
| 34 | + ViewDescriptor() |
| 35 | + .set_aggregation(Aggregation::Sum()) |
| 36 | + .set_aggregation_window(AggregationWindow::Cumulative()); |
| 37 | + ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 2.0}}); |
| 38 | + EXPECT_EQ(Aggregation::Sum(), data.aggregation()); |
| 39 | + EXPECT_EQ(AggregationWindow::Cumulative(), data.aggregation_window()); |
| 40 | + ASSERT_EQ(ViewData::Type::kDouble, data.type()); |
| 41 | + EXPECT_THAT(data.double_data(), |
| 42 | + ::testing::UnorderedElementsAre( |
| 43 | + ::testing::Pair(::testing::ElementsAre(), 2.0))); |
| 44 | +} |
| 45 | + |
| 46 | +TEST(ViewDataTest, IntervalSum) { |
| 47 | + const auto descriptor = |
| 48 | + ViewDescriptor() |
| 49 | + .set_aggregation(Aggregation::Sum()) |
| 50 | + .set_aggregation_window( |
| 51 | + AggregationWindow::Interval(absl::Minutes(1))); |
| 52 | + ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 2.0}}); |
| 53 | + EXPECT_EQ(Aggregation::Sum(), data.aggregation()); |
| 54 | + EXPECT_EQ(AggregationWindow::Type::kInterval, |
| 55 | + data.aggregation_window().type()); |
| 56 | + EXPECT_EQ(absl::Minutes(1), data.aggregation_window().duration()); |
| 57 | + ASSERT_EQ(ViewData::Type::kDouble, data.type()); |
| 58 | + EXPECT_THAT(data.double_data(), |
| 59 | + ::testing::UnorderedElementsAre( |
| 60 | + ::testing::Pair(::testing::ElementsAre(), 2.0))); |
| 61 | +} |
| 62 | + |
| 63 | +TEST(ViewDataTest, CumulativeCount) { |
| 64 | + const auto descriptor = |
| 65 | + ViewDescriptor() |
| 66 | + .set_aggregation(Aggregation::Count()) |
| 67 | + .set_aggregation_window(AggregationWindow::Cumulative()); |
| 68 | + ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 2.0}}); |
| 69 | + EXPECT_EQ(Aggregation::Count(), data.aggregation()); |
| 70 | + EXPECT_EQ(AggregationWindow::Cumulative(), data.aggregation_window()); |
| 71 | + ASSERT_EQ(ViewData::Type::kInt64, data.type()); |
| 72 | + EXPECT_THAT(data.int_data(), ::testing::UnorderedElementsAre(::testing::Pair( |
| 73 | + ::testing::ElementsAre(), 1))); |
| 74 | +} |
| 75 | + |
| 76 | +TEST(ViewDataTest, IntervalCount) { |
| 77 | + const auto window = AggregationWindow::Interval(absl::Minutes(1)); |
| 78 | + const auto descriptor = ViewDescriptor() |
| 79 | + .set_aggregation(Aggregation::Count()) |
| 80 | + .set_aggregation_window(window); |
| 81 | + ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 2.0}}); |
| 82 | + EXPECT_EQ(Aggregation::Count(), data.aggregation()); |
| 83 | + EXPECT_EQ(window, data.aggregation_window()); |
| 84 | + ASSERT_EQ(ViewData::Type::kDouble, data.type()); |
| 85 | + EXPECT_THAT(data.double_data(), |
| 86 | + ::testing::UnorderedElementsAre( |
| 87 | + ::testing::Pair(::testing::ElementsAre(), 1.0))); |
| 88 | +} |
| 89 | + |
| 90 | +TEST(ViewDataTest, CumulativeDistribution) { |
| 91 | + const auto aggregation = |
| 92 | + Aggregation::Distribution(BucketBoundaries::Explicit({0})); |
| 93 | + const auto descriptor = |
| 94 | + ViewDescriptor() |
| 95 | + .set_aggregation(aggregation) |
| 96 | + .set_aggregation_window(AggregationWindow::Cumulative()); |
| 97 | + ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 2.0}}); |
| 98 | + EXPECT_EQ(aggregation, data.aggregation()); |
| 99 | + EXPECT_EQ(AggregationWindow::Cumulative(), data.aggregation_window()); |
| 100 | + ASSERT_EQ(ViewData::Type::kDistribution, data.type()); |
| 101 | + EXPECT_EQ(data.distribution_data().size(), 1); |
| 102 | +} |
| 103 | + |
| 104 | +TEST(ViewDataTest, IntervalDistribution) { |
| 105 | + const auto aggregation = |
| 106 | + Aggregation::Distribution(BucketBoundaries::Explicit({0})); |
| 107 | + const auto window = AggregationWindow::Interval(absl::Minutes(1)); |
| 108 | + const auto descriptor = ViewDescriptor() |
| 109 | + .set_aggregation(aggregation) |
| 110 | + .set_aggregation_window(window); |
| 111 | + ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 2.0}}); |
| 112 | + EXPECT_EQ(aggregation, data.aggregation()); |
| 113 | + EXPECT_EQ(window, data.aggregation_window()); |
| 114 | + ASSERT_EQ(ViewData::Type::kDistribution, data.type()); |
| 115 | + EXPECT_EQ(data.distribution_data().size(), 1); |
| 116 | +} |
| 117 | + |
| 118 | +TEST(ViewDataDeathTest, DoubleData) { |
| 119 | + const auto descriptor = |
| 120 | + ViewDescriptor() |
| 121 | + .set_aggregation(Aggregation::Sum()) |
| 122 | + .set_aggregation_window(AggregationWindow::Cumulative()); |
| 123 | + ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 1.0}}); |
| 124 | + EXPECT_DEBUG_DEATH({ EXPECT_TRUE(data.int_data().empty()); }, ""); |
| 125 | + EXPECT_DEBUG_DEATH({ EXPECT_TRUE(data.distribution_data().empty()); }, ""); |
| 126 | +} |
| 127 | + |
| 128 | +TEST(ViewDataDeathTest, IntData) { |
| 129 | + const auto descriptor = |
| 130 | + ViewDescriptor() |
| 131 | + .set_aggregation(Aggregation::Count()) |
| 132 | + .set_aggregation_window(AggregationWindow::Cumulative()); |
| 133 | + ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 1.0}}); |
| 134 | + EXPECT_DEBUG_DEATH({ EXPECT_TRUE(data.double_data().empty()); }, ""); |
| 135 | + EXPECT_DEBUG_DEATH({ EXPECT_TRUE(data.distribution_data().empty()); }, ""); |
| 136 | +} |
| 137 | + |
| 138 | +TEST(ViewDataDeathTest, DistributionData) { |
| 139 | + const auto descriptor = |
| 140 | + ViewDescriptor() |
| 141 | + .set_aggregation( |
| 142 | + Aggregation::Distribution(BucketBoundaries::Explicit({}))) |
| 143 | + .set_aggregation_window(AggregationWindow::Cumulative()); |
| 144 | + ViewData data = testing::TestUtils::MakeViewData(descriptor, {{{}, 1.0}}); |
| 145 | + EXPECT_DEBUG_DEATH({ EXPECT_TRUE(data.double_data().empty()); }, ""); |
| 146 | + EXPECT_DEBUG_DEATH({ EXPECT_TRUE(data.int_data().empty()); }, ""); |
| 147 | +} |
| 148 | + |
| 149 | +} // namespace |
| 150 | +} // namespace stats |
| 151 | +} // namespace opencensus |
0 commit comments