|
| 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/with_tag_map.h" |
| 16 | + |
| 17 | +#include <iostream> |
| 18 | +#include <thread> |
| 19 | + |
| 20 | +#include "gtest/gtest.h" |
| 21 | +#include "opencensus/context/context.h" |
| 22 | +#include "opencensus/tags/tag_key.h" |
| 23 | +#include "opencensus/tags/tag_map.h" |
| 24 | + |
| 25 | +namespace opencensus { |
| 26 | +namespace context { |
| 27 | +class ContextTestPeer { |
| 28 | + public: |
| 29 | + static const opencensus::tags::TagMap& CurrentTags() { |
| 30 | + return Context::InternalMutableCurrent()->tags_; |
| 31 | + } |
| 32 | +}; |
| 33 | +} // namespace context |
| 34 | +} // namespace opencensus |
| 35 | + |
| 36 | +// Not in namespace ::opencensus::context in order to better reflect what user |
| 37 | +// code should look like. |
| 38 | + |
| 39 | +namespace { |
| 40 | + |
| 41 | +using opencensus::context::ContextTestPeer; |
| 42 | + |
| 43 | +void LogCurrentContext() { |
| 44 | + const std::string s = opencensus::context::Context::Current().DebugString(); |
| 45 | + std::cout << " current: " << s << "\n"; |
| 46 | +} |
| 47 | + |
| 48 | +void ExpectNoTags() { |
| 49 | + EXPECT_EQ(opencensus::tags::TagMap({}), ContextTestPeer::CurrentTags()); |
| 50 | +} |
| 51 | + |
| 52 | +// Returns an example TagMap for testing. |
| 53 | +opencensus::tags::TagMap Tags1() { |
| 54 | + static const auto k1 = opencensus::tags::TagKey::Register("key1"); |
| 55 | + static const auto k2 = opencensus::tags::TagKey::Register("key2"); |
| 56 | + return opencensus::tags::TagMap({{k1, "val1"}, {k2, "val2"}}); |
| 57 | +} |
| 58 | + |
| 59 | +// Returns a different TagMap for testing. |
| 60 | +opencensus::tags::TagMap Tags2() { |
| 61 | + static const auto k3 = opencensus::tags::TagKey::Register("key3"); |
| 62 | + return opencensus::tags::TagMap({{k3, "val3"}}); |
| 63 | +} |
| 64 | + |
| 65 | +TEST(WithTagsTest, NoTagsInDefaultContext) { ExpectNoTags(); } |
| 66 | + |
| 67 | +TEST(WithTagsTest, TagsAreAddedAndRemoved) { |
| 68 | + const auto tags = Tags1(); |
| 69 | + ExpectNoTags(); |
| 70 | + { |
| 71 | + opencensus::tags::WithTagMap wt(tags); |
| 72 | + LogCurrentContext(); |
| 73 | + EXPECT_EQ(tags, ContextTestPeer::CurrentTags()) << "Tags were installed."; |
| 74 | + } |
| 75 | + ExpectNoTags(); |
| 76 | +} |
| 77 | + |
| 78 | +TEST(WithTagsTest, RvalueConstructor) { |
| 79 | + auto tags = Tags1(); |
| 80 | + ExpectNoTags(); |
| 81 | + { |
| 82 | + opencensus::tags::WithTagMap wt(std::move(tags)); |
| 83 | + EXPECT_EQ(Tags1(), ContextTestPeer::CurrentTags()) |
| 84 | + << "Tags were installed."; |
| 85 | + } |
| 86 | + ExpectNoTags(); |
| 87 | +} |
| 88 | + |
| 89 | +TEST(WithTagsTest, Nesting) { |
| 90 | + const auto tags1 = Tags1(); |
| 91 | + const auto tags2 = Tags2(); |
| 92 | + |
| 93 | + ExpectNoTags(); |
| 94 | + { |
| 95 | + opencensus::tags::WithTagMap wt1(tags1); |
| 96 | + EXPECT_EQ(tags1, ContextTestPeer::CurrentTags()) << "Tags1 installed."; |
| 97 | + { |
| 98 | + opencensus::tags::WithTagMap wt2(tags2); |
| 99 | + EXPECT_EQ(tags2, ContextTestPeer::CurrentTags()) << "Tags2 installed."; |
| 100 | + } |
| 101 | + EXPECT_EQ(tags1, ContextTestPeer::CurrentTags()) |
| 102 | + << "Tags2 uninstalled, back to Tags1."; |
| 103 | + } |
| 104 | + ExpectNoTags(); |
| 105 | +} |
| 106 | + |
| 107 | +TEST(WithTagsTest, DisabledViaConditional) { |
| 108 | + const auto tags1 = Tags1(); |
| 109 | + const auto tags2 = Tags2(); |
| 110 | + |
| 111 | + ExpectNoTags(); |
| 112 | + { |
| 113 | + opencensus::tags::WithTagMap wt1(tags1); |
| 114 | + EXPECT_EQ(tags1, ContextTestPeer::CurrentTags()) << "Tags1 installed."; |
| 115 | + { |
| 116 | + opencensus::tags::WithTagMap wt2(tags2, false); |
| 117 | + EXPECT_EQ(tags1, ContextTestPeer::CurrentTags()) |
| 118 | + << "Tags2 was NOT installed, blocked by condition."; |
| 119 | + } |
| 120 | + EXPECT_EQ(tags1, ContextTestPeer::CurrentTags()) |
| 121 | + << "Still Tags1 after WithTags2 goes out of scope."; |
| 122 | + } |
| 123 | + ExpectNoTags(); |
| 124 | +} |
| 125 | + |
| 126 | +#ifndef NDEBUG |
| 127 | +TEST(WithTagsDeathTest, DestructorOnWrongThread) { |
| 128 | + const auto tags = Tags1(); |
| 129 | + |
| 130 | + EXPECT_DEATH_IF_SUPPORTED( |
| 131 | + { |
| 132 | + auto* wt = new opencensus::tags::WithTagMap(tags); |
| 133 | + std::thread t([wt]() { |
| 134 | + // Running the destructor in a different thread corrupts its |
| 135 | + // thread-local Context. In debug mode, it assert()s. |
| 136 | + delete wt; |
| 137 | + }); |
| 138 | + t.join(); |
| 139 | + }, |
| 140 | + "must be destructed on the same thread"); |
| 141 | +} |
| 142 | +#endif |
| 143 | + |
| 144 | +} // namespace |
0 commit comments