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

Commit 7a58e1f

Browse files
authored
Make context_test expect the right tags and span. (#222)
Especially when Wrap()ing functions. This is easier now that we have the With* classes and utilities.
1 parent e9a943b commit 7a58e1f

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

opencensus/context/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ cc_test(
4949
copts = TEST_COPTS,
5050
deps = [
5151
":context",
52+
"//opencensus/tags:context_util",
53+
"//opencensus/tags:with_tag_map",
54+
"//opencensus/trace:context_util",
55+
"//opencensus/trace:with_span",
5256
"@com_google_googletest//:gtest_main",
5357
],
5458
)

opencensus/context/internal/context_test.cc

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@
1414

1515
#include "opencensus/context/context.h"
1616

17+
#include <functional>
1718
#include <iostream>
1819

1920
#include "gtest/gtest.h"
21+
#include "opencensus/tags/context_util.h"
22+
#include "opencensus/tags/tag_key.h"
23+
#include "opencensus/tags/tag_map.h"
24+
#include "opencensus/tags/with_tag_map.h"
25+
#include "opencensus/trace/context_util.h"
26+
#include "opencensus/trace/span.h"
27+
#include "opencensus/trace/span_context.h"
28+
#include "opencensus/trace/with_span.h"
2029

2130
// Not in namespace ::opencensus::context in order to better reflect what user
2231
// code should look like.
@@ -28,32 +37,62 @@ void LogCurrentContext() {
2837
std::cout << " current: " << s << "\n";
2938
}
3039

31-
TEST(ContextTest, DefaultContext) { LogCurrentContext(); }
40+
void ExpectEmptyContext() {
41+
EXPECT_TRUE(opencensus::tags::GetCurrentTagMap().tags().empty());
42+
opencensus::trace::SpanContext zeroed_span_context;
43+
EXPECT_EQ(zeroed_span_context, opencensus::trace::GetCurrentSpan().context());
44+
}
45+
46+
opencensus::tags::TagMap ExampleTagMap() {
47+
static const auto k1 = opencensus::tags::TagKey::Register("key1");
48+
static const auto k2 = opencensus::tags::TagKey::Register("key2");
49+
return opencensus::tags::TagMap({{k1, "v1"}, {k2, "v2"}});
50+
}
3251

33-
void Callback1() {
34-
std::cout << " inside function\n";
52+
TEST(ContextTest, DefaultContext) {
3553
LogCurrentContext();
54+
ExpectEmptyContext();
55+
}
56+
57+
void Callback1(const opencensus::trace::Span& expected_span) {
58+
EXPECT_EQ(ExampleTagMap(), opencensus::tags::GetCurrentTagMap());
59+
EXPECT_EQ(expected_span.context(),
60+
opencensus::trace::GetCurrentSpan().context());
3661
}
3762

3863
TEST(ContextTest, Wrap) {
39-
std::function<void()> fn =
40-
opencensus::context::Context::Current().Wrap(Callback1);
64+
auto span = opencensus::trace::Span::StartSpan("MySpan");
65+
std::function<void()> fn;
66+
{
67+
opencensus::tags::WithTagMap wt(ExampleTagMap());
68+
opencensus::trace::WithSpan ws(span);
69+
fn = opencensus::context::Context::Current().Wrap(
70+
[span]() { Callback1(span); });
71+
}
72+
ExpectEmptyContext();
4173
fn();
74+
span.End();
4275
}
4376

4477
TEST(ContextTest, WrapDoesNotLeak) {
78+
// Leak-sanitizer (part of ASAN) throws an error if this leaks.
79+
auto span = opencensus::trace::Span::StartSpan("MySpan");
4580
{
46-
std::function<void()> fn =
47-
opencensus::context::Context::Current().Wrap(Callback1);
81+
opencensus::tags::WithTagMap wt(ExampleTagMap());
82+
opencensus::trace::WithSpan ws(span);
83+
std::function<void()> fn = opencensus::context::Context::Current().Wrap(
84+
[span]() { Callback1(span); });
4885
}
4986
// We never call fn().
87+
span.End();
5088
}
5189

5290
TEST(ContextTest, WrappedFnIsCopiable) {
53-
std::function<void()> fn2;
91+
std::function<void()> fn1, fn2;
5492
{
55-
std::function<void()> fn1 =
56-
opencensus::context::Context::Current().Wrap(Callback1);
93+
opencensus::tags::WithTagMap wt(ExampleTagMap());
94+
fn1 = opencensus::context::Context::Current().Wrap(
95+
[]() { Callback1(opencensus::trace::Span::BlankSpan()); });
5796
fn2 = fn1;
5897
fn1();
5998
}

0 commit comments

Comments
 (0)