context_test.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <gtest/gtest.h>
  4. #include <stdint.h>
  5. #include <map>
  6. #include <string>
  7. #include "opentelemetry/context/context.h"
  8. #include "opentelemetry/context/context_value.h"
  9. #include "opentelemetry/nostd/string_view.h"
  10. #include "opentelemetry/nostd/variant.h"
  11. using namespace opentelemetry;
  12. // Tests that the context constructor accepts an std::map.
  13. TEST(ContextTest, ContextIterableAcceptsMap)
  14. {
  15. std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
  16. context::Context test_context = context::Context(map_test);
  17. }
  18. // Tests that the GetValue method returns the expected value.
  19. TEST(ContextTest, ContextGetValueReturnsExpectedValue)
  20. {
  21. std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)},
  22. {"foo_key", static_cast<int64_t>(456)}};
  23. const context::Context test_context = context::Context(map_test);
  24. EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("test_key")), 123);
  25. EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("foo_key")), 456);
  26. }
  27. // Tests that the SetValues method accepts an std::map.
  28. TEST(ContextTest, ContextSetValuesAcceptsMap)
  29. {
  30. std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
  31. std::map<std::string, context::ContextValue> map_test_write = {
  32. {"foo_key", static_cast<int64_t>(456)}};
  33. context::Context test_context = context::Context(map_test);
  34. context::Context foo_context = test_context.SetValues(map_test_write);
  35. EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("test_key")), 123);
  36. EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("foo_key")), 456);
  37. }
  38. // Tests that the SetValues method accepts a nostd::string_view and
  39. // context::ContextValue.
  40. TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue)
  41. {
  42. nostd::string_view string_view_test = "string_view";
  43. context::ContextValue context_value_test = static_cast<int64_t>(123);
  44. context::Context test_context = context::Context(string_view_test, context_value_test);
  45. context::Context foo_context = test_context.SetValue(string_view_test, context_value_test);
  46. EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue(string_view_test)), 123);
  47. }
  48. // Tests that the original context does not change when a value is
  49. // written to it.
  50. TEST(ContextTest, ContextImmutability)
  51. {
  52. std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
  53. context::Context context_test = context::Context(map_test);
  54. context::Context context_foo = context_test.SetValue("foo_key", static_cast<int64_t>(456));
  55. EXPECT_FALSE(nostd::holds_alternative<int64_t>(context_test.GetValue("foo_key")));
  56. }
  57. // Tests that writing the same to a context overwrites the original value.
  58. TEST(ContextTest, ContextKeyOverwrite)
  59. {
  60. std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
  61. context::Context context_test = context::Context(map_test);
  62. context::Context context_foo = context_test.SetValue("test_key", static_cast<int64_t>(456));
  63. EXPECT_EQ(nostd::get<int64_t>(context_foo.GetValue("test_key")), 456);
  64. }
  65. // Tests that the new Context Objects inherits the keys and values
  66. // of the original context object.
  67. TEST(ContextTest, ContextInheritance)
  68. {
  69. using M = std::map<std::string, context::ContextValue>;
  70. M m1 = {{"test_key", static_cast<int64_t>(123)}, {"foo_key", static_cast<int64_t>(321)}};
  71. M m2 = {{"other_key", static_cast<int64_t>(789)}, {"another_key", static_cast<int64_t>(987)}};
  72. context::Context test_context = context::Context(m1);
  73. context::Context foo_context = test_context.SetValues(m2);
  74. EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("test_key")), 123);
  75. EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("foo_key")), 321);
  76. EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("other_key")), 789);
  77. EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("another_key")), 987);
  78. EXPECT_TRUE(nostd::holds_alternative<nostd::monostate>(test_context.GetValue("other_key")));
  79. EXPECT_TRUE(nostd::holds_alternative<nostd::monostate>(test_context.GetValue("another_key")));
  80. }
  81. // Tests that copying a context copies the key value pairs as expected.
  82. TEST(ContextTest, ContextCopyOperator)
  83. {
  84. std::map<std::string, context::ContextValue> test_map = {
  85. {"test_key", static_cast<int64_t>(123)},
  86. {"foo_key", static_cast<int64_t>(456)},
  87. {"other_key", static_cast<int64_t>(789)}};
  88. context::Context test_context = context::Context(test_map);
  89. const context::Context &copied_context = test_context;
  90. EXPECT_EQ(nostd::get<int64_t>(copied_context.GetValue("test_key")), 123);
  91. EXPECT_EQ(nostd::get<int64_t>(copied_context.GetValue("foo_key")), 456);
  92. EXPECT_EQ(nostd::get<int64_t>(copied_context.GetValue("other_key")), 789);
  93. }
  94. // Tests that the Context accepts an empty map.
  95. TEST(ContextTest, ContextEmptyMap)
  96. {
  97. std::map<std::string, context::ContextValue> map_test = {};
  98. context::Context test_context = context::Context(map_test);
  99. }
  100. // Tests that if a key exists within a context has key will return true
  101. // false if not.
  102. TEST(ContextTest, ContextHasKey)
  103. {
  104. std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
  105. const context::Context context_test = context::Context(map_test);
  106. EXPECT_TRUE(context_test.HasKey("test_key"));
  107. EXPECT_FALSE(context_test.HasKey("foo_key"));
  108. }
  109. // Tests that a copied context returns true when compared
  110. TEST(ContextTest, ContextCopyCompare)
  111. {
  112. std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
  113. context::Context context_test = context::Context(map_test);
  114. const context::Context &copied_test = context_test;
  115. EXPECT_TRUE(context_test == copied_test);
  116. }
  117. // Tests that two differently constructed contexts return false when compared
  118. TEST(ContextTest, ContextDiffCompare)
  119. {
  120. std::map<std::string, context::ContextValue> map_test = {{"test_key", static_cast<int64_t>(123)}};
  121. std::map<std::string, context::ContextValue> map_foo = {{"foo_key", static_cast<int64_t>(123)}};
  122. context::Context context_test = context::Context(map_test);
  123. context::Context foo_test = context::Context(map_foo);
  124. EXPECT_FALSE(context_test == foo_test);
  125. }