]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/api/test/context/runtime_context_test.cc
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
4 #include "opentelemetry/context/runtime_context.h"
5 #include "opentelemetry/context/context.h"
7 #include <gtest/gtest.h>
9 using namespace opentelemetry
;
11 // Tests that GetCurrent returns the current context
12 TEST(RuntimeContextTest
, GetCurrent
)
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
);
20 // Tests that detach resets the context to the previous context
21 TEST(RuntimeContextTest
, Detach
)
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
);
27 auto test_context_token
= context::RuntimeContext::Attach(test_context
);
28 auto foo_context_token
= context::RuntimeContext::Attach(foo_context
);
30 foo_context_token
.reset();
31 EXPECT_EQ(context::RuntimeContext::GetCurrent(), test_context
);
32 test_context_token
.reset();
35 // Tests that detach returns false when the wrong context is provided
36 TEST(RuntimeContextTest
, DetachWrongContext
)
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
));
45 // Tests that the ThreadLocalContext can handle three attached contexts
46 TEST(RuntimeContextTest
, ThreeAttachDetach
)
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
);
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
));
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
)
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);
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
)
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);
85 // Tests that SetValue returns the ContextValue associated with the
86 // passed in string and the current Runtime Context
87 TEST(RuntimeContextTest
, GetValueRuntimeContext
)
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);
94 // Tests that SetValue returns the ContextValue associated with the
95 // passed in string and the passed in context
96 TEST(RuntimeContextTest
, GetValueOtherContext
)
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);
102 // Test that any possible order of context detaching doesn't mess up the stack.
103 TEST(RuntimeContextTest
, DetachOutOfOrder
)
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);
111 std::vector
<context::Context
> contexts
;
112 for (auto i
: indices
)
114 contexts
.push_back(context::Context("index", (int64_t)i
));
119 std::vector
<nostd::unique_ptr
<context::Token
>> tokens
;
121 for (auto &c
: contexts
)
123 tokens
.push_back(context::RuntimeContext::Attach(c
));
126 for (size_t i
: indices
)
128 auto token
= std::move(tokens
.at(i
));
129 context::RuntimeContext::Detach(*token
);
132 EXPECT_EQ(context::RuntimeContext::GetCurrent(), context::Context());
134 } while (std::next_permutation(indices
.begin(), indices
.end()));