|
| 1 | +#include "opentelemetry/context/context.h" |
| 2 | +#include "opentelemetry/context/threadlocal_context.h" |
| 3 | + |
| 4 | +#include <gtest/gtest.h> |
| 5 | + |
| 6 | +using namespace opentelemetry; |
| 7 | + |
| 8 | +// Tests that GetCurrent returns the current context |
| 9 | +TEST(RuntimeContextTest, GetCurrent) |
| 10 | +{ |
| 11 | + std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}}; |
| 12 | + context::Context test_context = context::Context(map_test); |
| 13 | + context::RuntimeContext::Token old_context = context::RuntimeContext::Attach(test_context); |
| 14 | + EXPECT_TRUE(context::RuntimeContext::GetCurrent() == test_context); |
| 15 | + context::RuntimeContext::Detach(old_context); |
| 16 | +} |
| 17 | + |
| 18 | +// Tests that detach resets the context to the previous context |
| 19 | +TEST(RuntimeContextTest, Detach) |
| 20 | +{ |
| 21 | + std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}}; |
| 22 | + context::Context test_context = context::Context(map_test); |
| 23 | + context::Context foo_context = context::Context(map_test); |
| 24 | + |
| 25 | + context::RuntimeContext::Token test_context_token = context::RuntimeContext::Attach(test_context); |
| 26 | + context::RuntimeContext::Token foo_context_token = context::RuntimeContext::Attach(foo_context); |
| 27 | + |
| 28 | + context::RuntimeContext::Detach(foo_context_token); |
| 29 | + EXPECT_TRUE(context::RuntimeContext::GetCurrent() == test_context); |
| 30 | + context::RuntimeContext::Detach(test_context_token); |
| 31 | +} |
| 32 | + |
| 33 | +// Tests that detach returns false when the wrong context is provided |
| 34 | +TEST(RuntimeContextTest, DetachWrongContext) |
| 35 | +{ |
| 36 | + std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}}; |
| 37 | + context::Context test_context = context::Context(map_test); |
| 38 | + context::Context foo_context = context::Context(map_test); |
| 39 | + context::RuntimeContext::Token test_context_token = context::RuntimeContext::Attach(test_context); |
| 40 | + context::RuntimeContext::Token foo_context_token = context::RuntimeContext::Attach(foo_context); |
| 41 | + EXPECT_FALSE(context::RuntimeContext::Detach(test_context_token)); |
| 42 | + context::RuntimeContext::Detach(foo_context_token); |
| 43 | + context::RuntimeContext::Detach(test_context_token); |
| 44 | +} |
| 45 | + |
| 46 | +// Tests that the ThreadLocalContext can handle three attached contexts |
| 47 | +TEST(RuntimeContextTest, ThreeAttachDetach) |
| 48 | +{ |
| 49 | + std::map<std::string, context::ContextValue> map_test = {{"test_key", (int64_t)123}}; |
| 50 | + context::Context test_context = context::Context(map_test); |
| 51 | + context::Context foo_context = context::Context(map_test); |
| 52 | + context::Context other_context = context::Context(map_test); |
| 53 | + context::RuntimeContext::Token test_context_token = context::RuntimeContext::Attach(test_context); |
| 54 | + context::RuntimeContext::Token foo_context_token = context::RuntimeContext::Attach(foo_context); |
| 55 | + context::RuntimeContext::Token other_context_token = |
| 56 | + context::RuntimeContext::Attach(other_context); |
| 57 | + |
| 58 | + EXPECT_TRUE(context::RuntimeContext::Detach(other_context_token)); |
| 59 | + EXPECT_TRUE(context::RuntimeContext::Detach(foo_context_token)); |
| 60 | + EXPECT_TRUE(context::RuntimeContext::Detach(test_context_token)); |
| 61 | +} |
0 commit comments