propagation_test.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. #include <gtest/gtest.h>
  6. #include <algorithm>
  7. #include <string>
  8. #include <system_error>
  9. #include <unordered_map>
  10. #include <vector>
  11. #include "opentracing/expected/expected.hpp"
  12. #include "opentracing/propagation.h"
  13. #include "opentracing/string_view.h"
  14. #include "opentracing/util.h"
  15. #include "opentelemetry/baggage/baggage.h"
  16. #include "opentelemetry/common/attribute_value.h"
  17. #include "opentelemetry/nostd/function_ref.h"
  18. #include "opentelemetry/nostd/string_view.h"
  19. #include "opentelemetry/opentracingshim/propagation.h"
  20. #include "shim_mocks.h"
  21. namespace baggage = opentelemetry::baggage;
  22. namespace common = opentelemetry::common;
  23. namespace nostd = opentelemetry::nostd;
  24. namespace shim = opentelemetry::opentracingshim;
  25. TEST(PropagationTest, TextMapReader_Get_LookupKey_Unsupported)
  26. {
  27. std::unordered_map<std::string, std::string> text_map;
  28. TextMapCarrier testee{text_map};
  29. ASSERT_FALSE(testee.supports_lookup);
  30. ASSERT_EQ(testee.foreach_key_call_count, 0);
  31. shim::CarrierReaderShim tester{testee};
  32. auto lookup_unsupported = testee.LookupKey("foo");
  33. ASSERT_TRUE(text_map.empty());
  34. ASSERT_TRUE(opentracing::are_errors_equal(lookup_unsupported.error(),
  35. opentracing::lookup_key_not_supported_error));
  36. ASSERT_EQ(tester.Get("foo"), nostd::string_view{});
  37. ASSERT_EQ(testee.foreach_key_call_count, 1);
  38. text_map["foo"] = "bar";
  39. auto lookup_still_unsupported = testee.LookupKey("foo");
  40. ASSERT_FALSE(text_map.empty());
  41. ASSERT_TRUE(opentracing::are_errors_equal(lookup_still_unsupported.error(),
  42. opentracing::lookup_key_not_supported_error));
  43. ASSERT_EQ(tester.Get("foo"), nostd::string_view{"bar"});
  44. ASSERT_EQ(testee.foreach_key_call_count, 2);
  45. }
  46. TEST(PropagationTest, TextMapReader_Get_LookupKey_Supported)
  47. {
  48. std::unordered_map<std::string, std::string> text_map;
  49. TextMapCarrier testee{text_map};
  50. testee.supports_lookup = true;
  51. ASSERT_TRUE(testee.supports_lookup);
  52. ASSERT_EQ(testee.foreach_key_call_count, 0);
  53. shim::CarrierReaderShim tester{testee};
  54. auto lookup_not_found = testee.LookupKey("foo");
  55. ASSERT_TRUE(text_map.empty());
  56. ASSERT_TRUE(
  57. opentracing::are_errors_equal(lookup_not_found.error(), opentracing::key_not_found_error));
  58. ASSERT_EQ(tester.Get("foo"), nostd::string_view{});
  59. ASSERT_EQ(testee.foreach_key_call_count, 1);
  60. text_map["foo"] = "bar";
  61. auto lookup_found = testee.LookupKey("foo");
  62. ASSERT_FALSE(text_map.empty());
  63. ASSERT_EQ(lookup_found.value(), opentracing::string_view{"bar"});
  64. ASSERT_EQ(tester.Get("foo"), nostd::string_view{"bar"});
  65. ASSERT_EQ(testee.foreach_key_call_count, 1);
  66. }
  67. TEST(PropagationTest, TextMapReader_Keys)
  68. {
  69. std::unordered_map<std::string, std::string> text_map;
  70. TextMapCarrier testee{text_map};
  71. ASSERT_EQ(testee.foreach_key_call_count, 0);
  72. shim::CarrierReaderShim tester{testee};
  73. std::vector<nostd::string_view> kvs;
  74. auto callback = [&text_map, &kvs](nostd::string_view k) {
  75. kvs.emplace_back(k);
  76. return !text_map.empty();
  77. };
  78. ASSERT_TRUE(tester.Keys(callback));
  79. ASSERT_TRUE(text_map.empty());
  80. ASSERT_TRUE(kvs.empty());
  81. ASSERT_EQ(testee.foreach_key_call_count, 1);
  82. text_map["foo"] = "bar";
  83. text_map["bar"] = "baz";
  84. text_map["baz"] = "foo";
  85. ASSERT_TRUE(tester.Keys(callback));
  86. ASSERT_FALSE(text_map.empty());
  87. ASSERT_EQ(text_map.size(), kvs.size());
  88. ASSERT_EQ(testee.foreach_key_call_count, 2);
  89. }
  90. TEST(PropagationTest, TextMapWriter_Set)
  91. {
  92. std::unordered_map<std::string, std::string> text_map;
  93. TextMapCarrier testee{text_map};
  94. shim::CarrierWriterShim tester{testee};
  95. ASSERT_TRUE(text_map.empty());
  96. tester.Set("foo", "bar");
  97. tester.Set("bar", "baz");
  98. tester.Set("baz", "foo");
  99. ASSERT_EQ(text_map.size(), 3);
  100. ASSERT_EQ(text_map["foo"], "bar");
  101. ASSERT_EQ(text_map["bar"], "baz");
  102. ASSERT_EQ(text_map["baz"], "foo");
  103. }