1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include <gtest/gtest.h>
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <map>
#include <string>
#include <utility>
#include <vector>
#include "opentelemetry/context/context.h"
#include "opentelemetry/context/context_value.h"
#include "opentelemetry/context/runtime_context.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/nostd/variant.h"
using namespace opentelemetry;
// Tests that GetCurrent returns the current context
TEST(RuntimeContextTest, GetCurrent)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context test_context = context::Context(map_test);
auto old_context = context::RuntimeContext::Attach(test_context);
EXPECT_EQ(context::RuntimeContext::GetCurrent(), test_context);
}
// Tests that detach resets the context to the previous context
TEST(RuntimeContextTest, Detach)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context test_context = context::Context(map_test);
context::Context foo_context = context::Context(map_test);
auto test_context_token = context::RuntimeContext::Attach(test_context);
auto foo_context_token = context::RuntimeContext::Attach(foo_context);
foo_context_token.reset();
EXPECT_EQ(context::RuntimeContext::GetCurrent(), test_context);
test_context_token.reset();
}
// Tests that detach returns false when the wrong context is provided
TEST(RuntimeContextTest, DetachWrongContext)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context test_context = context::Context(map_test);
auto test_context_token = context::RuntimeContext::Attach(test_context);
EXPECT_TRUE(context::RuntimeContext::Detach(*test_context_token));
EXPECT_FALSE(context::RuntimeContext::Detach(*test_context_token));
}
// Tests that the ThreadLocalContext can handle three attached contexts
TEST(RuntimeContextTest, ThreeAttachDetach)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context test_context = context::Context(map_test);
context::Context foo_context = context::Context(map_test);
context::Context other_context = context::Context(map_test);
auto test_context_token = context::RuntimeContext::Attach(test_context);
auto foo_context_token = context::RuntimeContext::Attach(foo_context);
auto other_context_token = context::RuntimeContext::Attach(other_context);
EXPECT_TRUE(context::RuntimeContext::Detach(*other_context_token));
EXPECT_TRUE(context::RuntimeContext::Detach(*foo_context_token));
EXPECT_TRUE(context::RuntimeContext::Detach(*test_context_token));
}
// Tests that SetValue returns a context with the passed in data and the
// RuntimeContext data when a context is not passed into the
// RuntimeContext::SetValue method.
TEST(RuntimeContextTest, SetValueRuntimeContext)
{
context::Context foo_context = context::Context("foo_key", static_cast<int64_t>(596));
auto old_context_token = context::RuntimeContext::Attach(foo_context);
context::Context test_context =
context::RuntimeContext::SetValue("test_key", static_cast<int64_t>(123));
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("test_key")), 123);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("foo_key")), 596);
}
// Tests that SetValue returns a context with the passed in data and the
// passed in context data when a context* is passed into the
// RuntimeContext::SetValue method.
TEST(RuntimeContextTest, SetValueOtherContext)
{
context::Context foo_context = context::Context("foo_key", static_cast<int64_t>(596));
context::Context test_context =
context::RuntimeContext::SetValue("test_key", static_cast<int64_t>(123), &foo_context);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("test_key")), 123);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("foo_key")), 596);
}
// Tests that SetValue returns the ContextValue associated with the
// passed in string and the current Runtime Context
TEST(RuntimeContextTest, GetValueRuntimeContext)
{
context::Context foo_context = context::Context("foo_key", static_cast<int64_t>(596));
auto old_context_token = context::RuntimeContext::Attach(foo_context);
EXPECT_EQ(nostd::get<int64_t>(context::RuntimeContext::GetValue("foo_key")), 596);
}
// Tests that SetValue returns the ContextValue associated with the
// passed in string and the passed in context
TEST(RuntimeContextTest, GetValueOtherContext)
{
context::Context foo_context = context::Context("foo_key", static_cast<int64_t>(596));
EXPECT_EQ(nostd::get<int64_t>(context::RuntimeContext::GetValue("foo_key", &foo_context)), 596);
}
// Test that any possible order of context detaching doesn't mess up the stack.
TEST(RuntimeContextTest, DetachOutOfOrder)
{
std::vector<size_t> indices;
indices.push_back(0);
indices.push_back(1);
indices.push_back(2);
indices.push_back(3);
std::vector<context::Context> contexts;
contexts.reserve(indices.size());
for (auto i : indices)
{
contexts.push_back(context::Context("index", static_cast<int64_t>(i)));
}
do
{
std::vector<nostd::unique_ptr<context::Token>> tokens;
tokens.reserve(contexts.size());
for (auto &c : contexts)
{
tokens.push_back(context::RuntimeContext::Attach(c));
}
for (size_t i : indices)
{
auto token = std::move(tokens.at(i));
context::RuntimeContext::Detach(*token);
}
EXPECT_EQ(context::RuntimeContext::GetCurrent(), context::Context());
} while (std::next_permutation(indices.begin(), indices.end()));
}
|