|
| 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/trace/with_span.h" |
| 16 | + |
| 17 | +#include <iostream> |
| 18 | + |
| 19 | +#include "gtest/gtest.h" |
| 20 | +#include "opencensus/context/context.h" |
| 21 | +#include "opencensus/trace/span.h" |
| 22 | +#include "opencensus/trace/span_context.h" |
| 23 | + |
| 24 | +namespace opencensus { |
| 25 | +namespace context { |
| 26 | +class ContextTestPeer { |
| 27 | + public: |
| 28 | + static const opencensus::trace::SpanContext& CurrentCtx() { |
| 29 | + return Context::InternalMutableCurrent()->span_.context(); |
| 30 | + } |
| 31 | +}; |
| 32 | +} // namespace context |
| 33 | +} // namespace opencensus |
| 34 | + |
| 35 | +// Not in namespace ::opencensus::context in order to better reflect what user |
| 36 | +// code should look like. |
| 37 | + |
| 38 | +namespace { |
| 39 | + |
| 40 | +using opencensus::context::ContextTestPeer; |
| 41 | + |
| 42 | +void LogCurrentContext() { |
| 43 | + const std::string s = opencensus::context::Context::Current().DebugString(); |
| 44 | + std::cout << " current: " << s << "\n"; |
| 45 | +} |
| 46 | + |
| 47 | +void ExpectNoSpan() { |
| 48 | + opencensus::trace::SpanContext zeroed_span_context; |
| 49 | + EXPECT_EQ(zeroed_span_context, ContextTestPeer::CurrentCtx()); |
| 50 | +} |
| 51 | + |
| 52 | +TEST(WithSpanTest, NoSpanInDefaultContext) { ExpectNoSpan(); } |
| 53 | + |
| 54 | +TEST(WithSpanTest, SpanIsAddedAndRemoved) { |
| 55 | + auto span = opencensus::trace::Span::StartSpan("MySpan"); |
| 56 | + ExpectNoSpan(); |
| 57 | + { |
| 58 | + opencensus::trace::WithSpan ws(span); |
| 59 | + LogCurrentContext(); |
| 60 | + EXPECT_EQ(span.context(), ContextTestPeer::CurrentCtx()) |
| 61 | + << "Span was installed."; |
| 62 | + } |
| 63 | + ExpectNoSpan(); |
| 64 | + span.End(); |
| 65 | +} |
| 66 | + |
| 67 | +TEST(WithSpanTest, Nesting) { |
| 68 | + auto span1 = opencensus::trace::Span::StartSpan("MySpan1"); |
| 69 | + auto span2 = opencensus::trace::Span::StartSpan("MySpan2"); |
| 70 | + ExpectNoSpan(); |
| 71 | + { |
| 72 | + opencensus::trace::WithSpan ws1(span1); |
| 73 | + EXPECT_EQ(span1.context(), ContextTestPeer::CurrentCtx()) |
| 74 | + << "Span1 was installed."; |
| 75 | + { |
| 76 | + opencensus::trace::WithSpan ws2(span2); |
| 77 | + EXPECT_EQ(span2.context(), ContextTestPeer::CurrentCtx()) |
| 78 | + << "Span2 was installed."; |
| 79 | + } |
| 80 | + EXPECT_EQ(span1.context(), ContextTestPeer::CurrentCtx()) |
| 81 | + << "Span2 was uninstalled, back to Span1."; |
| 82 | + } |
| 83 | + ExpectNoSpan(); |
| 84 | + span2.End(); |
| 85 | + span1.End(); |
| 86 | +} |
| 87 | + |
| 88 | +TEST(WithSpanTest, DisabledViaConditional) { |
| 89 | + auto span1 = opencensus::trace::Span::StartSpan("MySpan1"); |
| 90 | + auto span2 = opencensus::trace::Span::StartSpan("MySpan2"); |
| 91 | + ExpectNoSpan(); |
| 92 | + { |
| 93 | + opencensus::trace::WithSpan ws1(span1); |
| 94 | + EXPECT_EQ(span1.context(), ContextTestPeer::CurrentCtx()) |
| 95 | + << "Span1 was installed."; |
| 96 | + { |
| 97 | + opencensus::trace::WithSpan ws2(span2, false); |
| 98 | + EXPECT_EQ(span1.context(), ContextTestPeer::CurrentCtx()) |
| 99 | + << "Span2 was NOT installed, blocked by condition."; |
| 100 | + } |
| 101 | + EXPECT_EQ(span1.context(), ContextTestPeer::CurrentCtx()) |
| 102 | + << "Still Span1 after WithSpan2 goes out of scope."; |
| 103 | + } |
| 104 | + ExpectNoSpan(); |
| 105 | + span2.End(); |
| 106 | + span1.End(); |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace |
0 commit comments