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