|
| 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/context_util.h" |
| 16 | + |
| 17 | +#include "gtest/gtest.h" |
| 18 | +#include "opencensus/context/context.h" |
| 19 | +#include "opencensus/trace/span.h" |
| 20 | +#include "opencensus/trace/span_context.h" |
| 21 | +#include "opencensus/trace/with_span.h" |
| 22 | + |
| 23 | +// Not in namespace ::opencensus::context in order to better reflect what user |
| 24 | +// code should look like. |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +TEST(FromContextTest, GetCurrentSpan) { |
| 29 | + opencensus::trace::SpanContext zeroed_span_context; |
| 30 | + auto span = opencensus::trace::Span::StartSpan("MySpan"); |
| 31 | + EXPECT_EQ(zeroed_span_context, opencensus::trace::GetCurrentSpan().context()); |
| 32 | + { |
| 33 | + opencensus::trace::WithSpan ws(span); |
| 34 | + EXPECT_EQ(span.context(), opencensus::trace::GetCurrentSpan().context()); |
| 35 | + } |
| 36 | + span.End(); |
| 37 | +} |
| 38 | + |
| 39 | +TEST(FromContextTest, GetSpanFromContext) { |
| 40 | + opencensus::trace::SpanContext zeroed_span_context; |
| 41 | + auto span = opencensus::trace::Span::StartSpan("MySpan"); |
| 42 | + opencensus::context::Context ctx = opencensus::context::Context::Current(); |
| 43 | + EXPECT_EQ(zeroed_span_context, |
| 44 | + opencensus::trace::GetSpanFromContext(ctx).context()); |
| 45 | + { |
| 46 | + opencensus::trace::WithSpan ws(span); |
| 47 | + ctx = opencensus::context::Context::Current(); |
| 48 | + } |
| 49 | + EXPECT_EQ(zeroed_span_context, opencensus::trace::GetCurrentSpan().context()); |
| 50 | + EXPECT_EQ(span.context(), |
| 51 | + opencensus::trace::GetSpanFromContext(ctx).context()); |
| 52 | + span.End(); |
| 53 | +} |
| 54 | + |
| 55 | +} // namespace |
0 commit comments