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
3863TEST (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
4477TEST (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
5290TEST (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