Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 870fa67

Browse files
authored
Add a test-only library granting access to the ViewData constructor. (#15)
This is needed for testing exporters without using the full View/Record workflow.
1 parent 2b887c5 commit 870fa67

5 files changed

Lines changed: 102 additions & 3 deletions

File tree

opencensus/stats/BUILD

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ cc_library(
3030
],
3131
)
3232

33+
# Test-only utilities.
34+
cc_library(
35+
name = "test_utils",
36+
testonly = 1,
37+
srcs = ["testing/test_utils.cc"],
38+
hdrs = ["testing/test_utils.h"],
39+
visibility = ["//visibility:public"],
40+
deps = [
41+
":core",
42+
"@com_google_absl//absl/memory",
43+
"@com_google_absl//absl/time",
44+
],
45+
)
46+
3347
cc_library(
3448
name = "core",
3549
srcs = [

opencensus/stats/examples/exporter_example.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,13 @@ TEST_F(ExporterExample, Distribution) {
102102
opencensus::stats::ViewDescriptor()
103103
.set_name("example.com/Bar/FooUsage-sum-interval-foo_id")
104104
.set_measure(kFooUsageMeasureName)
105-
.set_name("example.com/Bar/FooUsage-sum-delta-foo_id")
106105
.set_aggregation(opencensus::stats::Aggregation::Sum())
107106
.set_aggregation_window(
108107
opencensus::stats::AggregationWindow::Interval(absl::Hours(1)))
109108
.add_column("foo_id")
110109
.set_description(
111-
"Cumulative sum of example.com/Foo/FooUsage broken down "
112-
"by 'foo_id'.");
110+
"Rolling sum of example.com/Foo/FooUsage over the previous hour "
111+
"broken down by 'foo_id'.");
113112

114113
// The order of view registration and exporter creation does not matter, as
115114
// long as both precede data recording.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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/testing/test_utils.h"
16+
17+
#include <memory>
18+
19+
#include "absl/memory/memory.h"
20+
#include "absl/time/time.h"
21+
22+
namespace opencensus {
23+
namespace stats {
24+
namespace testing {
25+
26+
// static
27+
ViewData TestUtils::MakeViewData(
28+
const ViewDescriptor& descriptor,
29+
std::initializer_list<std::pair<std::vector<std::string>, double>> values) {
30+
auto impl = absl::make_unique<ViewDataImpl>(absl::UnixEpoch(), descriptor);
31+
for (const auto& value : values) {
32+
impl->Add(value.second, value.first, absl::UnixEpoch());
33+
}
34+
return ViewData(std::move(impl));
35+
}
36+
37+
} // namespace testing
38+
} // namespace stats
39+
} // namespace opencensus
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
#ifndef OPENCENSUS_STATS_TESTING_TEST_UTILS_H_
16+
#define OPENCENSUS_STATS_TESTING_TEST_UTILS_H_
17+
18+
#include <initializer_list>
19+
#include <string>
20+
#include <utility>
21+
#include <vector>
22+
23+
#include "opencensus/stats/internal/view_data_impl.h"
24+
#include "opencensus/stats/view_data.h"
25+
26+
namespace opencensus {
27+
namespace stats {
28+
namespace testing {
29+
30+
class TestUtils final {
31+
public:
32+
static ViewData MakeViewData(
33+
const ViewDescriptor& descriptor,
34+
std::initializer_list<std::pair<std::vector<std::string>, double>>
35+
values);
36+
};
37+
38+
} // namespace testing
39+
} // namespace stats
40+
} // namespace opencensus
41+
42+
#endif // OPENCENSUS_STATS_TESTING_TEST_UTILS_H_

opencensus/stats/view_data.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
namespace opencensus {
3131
namespace stats {
3232

33+
// Forward declarations of friends.
3334
class ViewDataImpl;
35+
namespace testing {
36+
class TestUtils;
37+
}
3438

3539
// ViewData is an immutable snapshot of data for a particular View, aggregated
3640
// according to the View's Aggregation and AggregationWindow.
@@ -69,6 +73,7 @@ class ViewData {
6973

7074
private:
7175
friend class View; // Allowed to call the private constructor.
76+
friend class testing::TestUtils;
7277
explicit ViewData(std::unique_ptr<ViewDataImpl> data);
7378

7479
const std::unique_ptr<ViewDataImpl> impl_;

0 commit comments

Comments
 (0)