|
| 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/tags/context_util.h" |
| 16 | + |
| 17 | +#include "gtest/gtest.h" |
| 18 | +#include "opencensus/context/context.h" |
| 19 | +#include "opencensus/tags/tag_key.h" |
| 20 | +#include "opencensus/tags/tag_map.h" |
| 21 | +#include "opencensus/tags/with_tag_map.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 | +// Returns an example TagMap for testing. |
| 29 | +opencensus::tags::TagMap Tags1() { |
| 30 | + static const auto k1 = opencensus::tags::TagKey::Register("key1"); |
| 31 | + static const auto k2 = opencensus::tags::TagKey::Register("key2"); |
| 32 | + return opencensus::tags::TagMap({{k1, "val1"}, {k2, "val2"}}); |
| 33 | +} |
| 34 | + |
| 35 | +TEST(FromContextTest, GetCurrentTagMap) { |
| 36 | + EXPECT_TRUE(opencensus::tags::GetCurrentTagMap().tags().empty()); |
| 37 | + { |
| 38 | + opencensus::tags::WithTagMap wt(Tags1()); |
| 39 | + EXPECT_EQ(Tags1().tags(), opencensus::tags::GetCurrentTagMap().tags()); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +TEST(FromContextTest, GetTagMapFromContext) { |
| 44 | + opencensus::context::Context ctx = opencensus::context::Context::Current(); |
| 45 | + EXPECT_TRUE(opencensus::tags::GetTagMapFromContext(ctx).tags().empty()); |
| 46 | + { |
| 47 | + opencensus::tags::WithTagMap wt(Tags1()); |
| 48 | + ctx = opencensus::context::Context::Current(); |
| 49 | + } |
| 50 | + EXPECT_EQ(Tags1().tags(), opencensus::tags::GetTagMapFromContext(ctx).tags()); |
| 51 | +} |
| 52 | + |
| 53 | +} // namespace |
0 commit comments