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 150 151 152 153 154 155
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include <gtest/gtest.h>
#include <stdint.h>
#include <map>
#include <string>
#include <utility>
#include "opentelemetry/context/context.h"
#include "opentelemetry/context/context_value.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/variant.h"
using namespace opentelemetry;
// Tests that the context constructor accepts an std::map.
TEST(ContextTest, ContextIterableAcceptsMap)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context test_context = context::Context(map_test);
}
// Tests that the GetValue method returns the expected value.
TEST(ContextTest, ContextGetValueReturnsExpectedValue)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)},
{"foo_key", static_cast<int64_t>(456)}};
const context::Context test_context = context::Context(map_test);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("test_key")), 123);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("foo_key")), 456);
}
// Tests that the SetValues method accepts an std::map.
TEST(ContextTest, ContextSetValuesAcceptsMap)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
std::map<std::string, context::ContextValue> map_test_write = {
{"foo_key", static_cast<int64_t>(456)}};
context::Context test_context = context::Context(map_test);
context::Context foo_context = test_context.SetValues(map_test_write);
EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("test_key")), 123);
EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("foo_key")), 456);
}
// Tests that the SetValues method accepts a nostd::string_view and
// context::ContextValue.
TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue)
{
nostd::string_view string_view_test = "string_view";
context::ContextValue context_value_test = static_cast<int64_t>(123);
context::Context test_context = context::Context(string_view_test, context_value_test);
context::Context foo_context = test_context.SetValue(string_view_test, context_value_test);
EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue(string_view_test)), 123);
}
// Tests that the original context does not change when a value is
// written to it.
TEST(ContextTest, ContextImmutability)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context context_test = context::Context(map_test);
context::Context context_foo = context_test.SetValue("foo_key", static_cast<int64_t>(456));
EXPECT_FALSE(nostd::holds_alternative<int64_t>(context_test.GetValue("foo_key")));
}
// Tests that writing the same to a context overwrites the original value.
TEST(ContextTest, ContextKeyOverwrite)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context context_test = context::Context(map_test);
context::Context context_foo = context_test.SetValue("test_key", static_cast<int64_t>(456));
EXPECT_EQ(nostd::get<int64_t>(context_foo.GetValue("test_key")), 456);
}
// Tests that the new Context Objects inherits the keys and values
// of the original context object.
TEST(ContextTest, ContextInheritance)
{
using M = std::map<std::string, context::ContextValue>;
M m1 = {{"test_key", static_cast<int64_t>(123)}, {"foo_key", static_cast<int64_t>(321)}};
M m2 = {{"other_key", static_cast<int64_t>(789)}, {"another_key", static_cast<int64_t>(987)}};
context::Context test_context = context::Context(m1);
context::Context foo_context = test_context.SetValues(m2);
EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("test_key")), 123);
EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("foo_key")), 321);
EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("other_key")), 789);
EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("another_key")), 987);
EXPECT_TRUE(nostd::holds_alternative<nostd::monostate>(test_context.GetValue("other_key")));
EXPECT_TRUE(nostd::holds_alternative<nostd::monostate>(test_context.GetValue("another_key")));
}
// Tests that copying a context copies the key value pairs as expected.
TEST(ContextTest, ContextCopyOperator)
{
std::map<std::string, context::ContextValue> test_map = {
{"test_key", static_cast<int64_t>(123)},
{"foo_key", static_cast<int64_t>(456)},
{"other_key", static_cast<int64_t>(789)}};
context::Context test_context = context::Context(test_map);
const context::Context &copied_context = test_context;
EXPECT_EQ(nostd::get<int64_t>(copied_context.GetValue("test_key")), 123);
EXPECT_EQ(nostd::get<int64_t>(copied_context.GetValue("foo_key")), 456);
EXPECT_EQ(nostd::get<int64_t>(copied_context.GetValue("other_key")), 789);
}
// Tests that the Context accepts an empty map.
TEST(ContextTest, ContextEmptyMap)
{
std::map<std::string, context::ContextValue> map_test = {};
context::Context test_context = context::Context(map_test);
}
// Tests that if a key exists within a context has key will return true
// false if not.
TEST(ContextTest, ContextHasKey)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
const context::Context context_test = context::Context(map_test);
EXPECT_TRUE(context_test.HasKey("test_key"));
EXPECT_FALSE(context_test.HasKey("foo_key"));
}
// Tests that a copied context returns true when compared
TEST(ContextTest, ContextCopyCompare)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
context::Context context_test = context::Context(map_test);
const context::Context &copied_test = context_test;
EXPECT_TRUE(context_test == copied_test);
}
// Tests that two differently constructed contexts return false when compared
TEST(ContextTest, ContextDiffCompare)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
std::map<std::string, context::ContextValue> map_foo = {{"foo_key", static_cast<int64_t>(123)}};
context::Context context_test = context::Context(map_test);
context::Context foo_test = context::Context(map_foo);
EXPECT_FALSE(context_test == foo_test);
}
|