parsed_metadata_test.cc
Go to the documentation of this file.
1 //
2 // Copyright 2021 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
25 
26 namespace grpc_core {
27 namespace testing {
28 
29 struct CharTrait {
30  using MementoType = char;
31  static absl::string_view key() { return "key"; }
32  static char test_memento() { return 'a'; }
33  static char test_value() { return 'a'; }
34  static size_t test_memento_transport_size() { return 34; }
35  static char MementoToValue(char memento) { return memento; }
37  return slice[0];
38  }
39  static std::string DisplayValue(char value) { return std::string(1, value); }
40 };
41 
42 struct Int32Trait {
44  static absl::string_view key() { return "key2"; }
45  static int32_t test_memento() { return -1; }
46  static int32_t test_value() { return -1; }
47  static size_t test_memento_transport_size() { return 478; }
48  static int32_t MementoToValue(int32_t memento) { return memento; }
50  int32_t out;
51  GPR_ASSERT(absl::SimpleAtoi(slice.as_string_view(), &out));
52  return out;
53  }
55  return std::to_string(value);
56  }
57 };
58 
59 struct Int64Trait {
61  static absl::string_view key() { return "key3"; }
62  static int64_t test_memento() { return 83481847284179298; }
63  static int64_t test_value() { return -83481847284179298; }
64  static size_t test_memento_transport_size() { return 87; }
65  static int64_t MementoToValue(int64_t memento) { return -memento; }
67  int64_t out;
68  GPR_ASSERT(absl::SimpleAtoi(slice.as_string_view(), &out));
69  return out;
70  }
72  return std::to_string(value);
73  }
74 };
75 
76 struct IntptrTrait {
78  static absl::string_view key() { return "key4"; }
79  static intptr_t test_memento() { return 8374298; }
80  static intptr_t test_value() { return test_memento() / 2; }
81  static size_t test_memento_transport_size() { return 800; }
82  static intptr_t MementoToValue(intptr_t memento) { return memento / 2; }
84  intptr_t out;
85  GPR_ASSERT(absl::SimpleAtoi(slice.as_string_view(), &out));
86  return out;
87  }
89  return std::to_string(value);
90  }
91 };
92 
93 struct StringTrait {
95  static absl::string_view key() { return "key5-bin"; }
96  static std::string test_memento() { return "hello"; }
97  static std::string test_value() { return "hi hello"; }
98  static size_t test_memento_transport_size() { return 599; }
100  return "hi " + memento;
101  }
103  auto view = slice.as_string_view();
104  return std::string(view.begin(), view.end());
105  }
106  static std::string DisplayValue(const std::string& value) { return value; }
107 };
108 
110  public:
111  void Set(CharTrait, char x) { SetChar(x); }
112  void Set(Int32Trait, int32_t x) { SetInt32(x); }
113  void Set(Int64Trait, int64_t x) { SetInt64(x); }
114  void Set(IntptrTrait, intptr_t x) { SetIntptr(x); }
115  void Set(StringTrait, std::string x) { SetString(x); }
116 
118  metadata.SetOnContainer(this);
119  }
120 
121  MOCK_METHOD1(SetChar, void(char));
122  MOCK_METHOD1(SetInt32, void(int32_t));
123  MOCK_METHOD1(SetInt64, void(int64_t));
124  MOCK_METHOD1(SetIntptr, void(intptr_t));
125  MOCK_METHOD1(SetString, void(std::string));
126 };
127 
129 
130 TEST(ParsedMetadataTest, Noop) { FakeParsedMetadata(); }
131 
132 TEST(ParsedMetadataTest, DebugString) {
133  FakeParsedMetadata parsed(CharTrait(), 'x', 36);
134  EXPECT_EQ(parsed.DebugString(), "key: x");
135 }
136 
137 TEST(ParsedMetadataTest, IsNotBinary) {
138  FakeParsedMetadata parsed(CharTrait(), 'x', 36);
139  EXPECT_FALSE(parsed.is_binary_header());
140 }
141 
142 TEST(ParsedMetadataTest, IsBinary) {
143  FakeParsedMetadata parsed(StringTrait(), "s", 36);
144  EXPECT_TRUE(parsed.is_binary_header());
145 }
146 
147 TEST(ParsedMetadataTest, Set) {
148  FakeContainer c;
149  FakeParsedMetadata p(CharTrait(), 'x', 36);
150  EXPECT_CALL(c, SetChar('x')).Times(1);
151  c.Set(p);
152  p = FakeParsedMetadata(Int32Trait(), -1, 478);
153  EXPECT_CALL(c, SetInt32(-1)).Times(1);
154  c.Set(p);
155  p = FakeParsedMetadata(Int64Trait(), 83481847284179298, 87);
156  EXPECT_CALL(c, SetInt64(-83481847284179298)).Times(1);
157  c.Set(p);
158  p = FakeParsedMetadata(IntptrTrait(), 8374298, 800);
159  EXPECT_CALL(c, SetIntptr(4187149)).Times(1);
160  c.Set(p);
161  p = FakeParsedMetadata(StringTrait(), "hello", 599);
162  EXPECT_CALL(c, SetString("hi hello")).Times(1);
163  c.Set(p);
164 }
165 
166 template <typename T>
168 
170 
172  FakeParsedMetadata(TypeParam(), TypeParam::test_memento(),
173  TypeParam::test_memento_transport_size());
174 }
175 
177  FakeParsedMetadata a(TypeParam(), TypeParam::test_memento(),
178  TypeParam::test_memento_transport_size());
180  a = std::move(b);
181 }
182 
184  FakeParsedMetadata p(TypeParam(), TypeParam::test_memento(),
185  TypeParam::test_memento_transport_size());
186  EXPECT_EQ(p.DebugString(),
188  TypeParam::DisplayValue(TypeParam::test_memento())));
189 }
190 
192  FakeParsedMetadata p(TypeParam(), TypeParam::test_memento(),
193  TypeParam::test_memento_transport_size());
194  EXPECT_EQ(p.transport_size(), TypeParam::test_memento_transport_size());
195 }
196 
197 REGISTER_TYPED_TEST_SUITE_P(TraitSpecializedTest, Noop, CanMove, DebugString,
198  TransportSize);
199 
200 using InterestingTraits = ::testing::Types<CharTrait, Int32Trait, Int64Trait,
203 
204 TEST(KeyValueTest, Simple) {
206  using PMPtr = std::unique_ptr<PM>;
207  PMPtr p = absl::make_unique<PM>(Slice::FromCopiedString("key"),
208  Slice::FromCopiedString("value"));
209  EXPECT_EQ(p->DebugString(), "key: value");
210  EXPECT_EQ(p->transport_size(), 40);
211  PM p2 = p->WithNewValue(Slice::FromCopiedString("some_other_value"),
212  [](absl::string_view msg, const Slice& value) {
213  ASSERT_TRUE(false)
214  << "Should not be called: msg=" << msg
215  << ", value=" << value.as_string_view();
216  });
217  EXPECT_EQ(p->DebugString(), "key: value");
218  EXPECT_EQ(p2.DebugString(), "key: some_other_value");
219  EXPECT_EQ(p2.transport_size(), 51);
220  p.reset();
221  EXPECT_EQ(p2.DebugString(), "key: some_other_value");
222  EXPECT_EQ(p2.transport_size(), 51);
223  PM p3 = std::move(p2);
224  EXPECT_EQ(p3.DebugString(), "key: some_other_value");
225  EXPECT_EQ(p3.transport_size(), 51);
226 }
227 
228 TEST(KeyValueTest, LongKey) {
230  using PMPtr = std::unique_ptr<PM>;
231  PMPtr p = absl::make_unique<PM>(Slice::FromCopiedString(std::string(60, 'a')),
232  Slice::FromCopiedString("value"));
233  EXPECT_EQ(
234  p->DebugString(),
235  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: value");
236  EXPECT_EQ(p->transport_size(), 97);
237  PM p2 = p->WithNewValue(Slice::FromCopiedString("some_other_value"),
238  [](absl::string_view msg, const Slice& value) {
239  ASSERT_TRUE(false)
240  << "Should not be called: msg=" << msg
241  << ", value=" << value.as_string_view();
242  });
243  EXPECT_EQ(
244  p->DebugString(),
245  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: value");
246  EXPECT_EQ(p2.DebugString(),
247  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: "
248  "some_other_value");
249  EXPECT_EQ(p2.transport_size(), 108);
250  p.reset();
251  EXPECT_EQ(p2.DebugString(),
252  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: "
253  "some_other_value");
254  EXPECT_EQ(p2.transport_size(), 108);
255  PM p3 = std::move(p2);
256  EXPECT_EQ(p3.DebugString(),
257  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: "
258  "some_other_value");
259  EXPECT_EQ(p3.transport_size(), 108);
260 }
261 
262 } // namespace testing
263 } // namespace grpc_core
264 
265 int main(int argc, char** argv) {
266  testing::InitGoogleTest(&argc, argv);
267  grpc::testing::TestEnvironment env(&argc, argv);
268  return RUN_ALL_TESTS();
269 };
grpc_core::testing::Int64Trait::MementoType
int64_t MementoType
Definition: parsed_metadata_test.cc:60
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
grpc_core::testing::IntptrTrait
Definition: parsed_metadata_test.cc:76
testing
Definition: aws_request_signer_test.cc:25
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
grpc_core::testing::FakeContainer::Set
void Set(IntptrTrait, intptr_t x)
Definition: parsed_metadata_test.cc:114
metadata_batch.h
grpc_core::testing::CharTrait::test_value
static char test_value()
Definition: parsed_metadata_test.cc:33
generate.env
env
Definition: generate.py:37
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
metadata
Definition: cq_verifier.cc:48
grpc_core::testing::IntptrTrait::test_value
static intptr_t test_value()
Definition: parsed_metadata_test.cc:80
grpc_core::testing::CharTrait::MementoType
char MementoType
Definition: parsed_metadata_test.cc:30
grpc_core::testing::CharTrait::DisplayValue
static std::string DisplayValue(char value)
Definition: parsed_metadata_test.cc:39
parsed_metadata.h
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::testing::TYPED_TEST_P
TYPED_TEST_P(TraitSpecializedTest, Noop)
Definition: parsed_metadata_test.cc:171
grpc_core::Slice
Definition: src/core/lib/slice/slice.h:282
grpc_core::testing::Int64Trait::MementoToValue
static int64_t MementoToValue(int64_t memento)
Definition: parsed_metadata_test.cc:65
grpc_core::testing::Int32Trait::key
static absl::string_view key()
Definition: parsed_metadata_test.cc:44
grpc_core::testing::Int64Trait::key
static absl::string_view key()
Definition: parsed_metadata_test.cc:61
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
DebugString
std::string DebugString(const google::protobuf::Message &message)
Definition: bloaty/tests/test.h:60
grpc_core::testing::Int32Trait::DisplayValue
static std::string DisplayValue(int32_t value)
Definition: parsed_metadata_test.cc:54
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::testing::TraitSpecializedTest
Definition: parsed_metadata_test.cc:167
grpc_core::testing::Int64Trait::DisplayValue
static std::string DisplayValue(int64_t value)
Definition: parsed_metadata_test.cc:71
grpc_core::testing::Int32Trait::test_memento_transport_size
static size_t test_memento_transport_size()
Definition: parsed_metadata_test.cc:47
grpc_core::ParsedMetadata::DebugString
std::string DebugString() const
Definition: parsed_metadata.h:199
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
grpc_core::testing::INSTANTIATE_TYPED_TEST_SUITE_P
INSTANTIATE_TYPED_TEST_SUITE_P(My, TraitSpecializedTest, InterestingTraits)
grpc_core::testing::InterestingTraits
::testing::Types< CharTrait, Int32Trait, Int64Trait, IntptrTrait, StringTrait > InterestingTraits
Definition: parsed_metadata_test.cc:201
grpc_core::testing::CharTrait::key
static absl::string_view key()
Definition: parsed_metadata_test.cc:31
grpc_core::testing::CharTrait::test_memento
static char test_memento()
Definition: parsed_metadata_test.cc:32
grpc_core::testing::FakeContainer::Set
void Set(Int32Trait, int32_t x)
Definition: parsed_metadata_test.cc:112
grpc_core::testing::CharTrait::test_memento_transport_size
static size_t test_memento_transport_size()
Definition: parsed_metadata_test.cc:34
grpc_core::testing::IntptrTrait::key
static absl::string_view key()
Definition: parsed_metadata_test.cc:78
grpc_core::testing::TYPED_TEST_SUITE_P
TYPED_TEST_SUITE_P(TraitSpecializedTest)
grpc_core::testing::IntptrTrait::MementoType
intptr_t MementoType
Definition: parsed_metadata_test.cc:77
grpc_core::testing::FakeContainer::Set
void Set(const ParsedMetadata< FakeContainer > &metadata)
Definition: parsed_metadata_test.cc:117
grpc_core::testing::Int32Trait
Definition: parsed_metadata_test.cc:42
grpc_core::testing::IntptrTrait::MementoToValue
static intptr_t MementoToValue(intptr_t memento)
Definition: parsed_metadata_test.cc:82
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
main
int main(int argc, char **argv)
Definition: parsed_metadata_test.cc:265
grpc_core::testing::FakeContainer::Set
void Set(StringTrait, std::string x)
Definition: parsed_metadata_test.cc:115
grpc_core::testing::Int64Trait::test_value
static int64_t test_value()
Definition: parsed_metadata_test.cc:63
grpc_core::testing::StringTrait::test_memento_transport_size
static size_t test_memento_transport_size()
Definition: parsed_metadata_test.cc:98
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
grpc_core::testing::StringTrait::MementoToValue
static std::string MementoToValue(std::string memento)
Definition: parsed_metadata_test.cc:99
grpc_core::testing::REGISTER_TYPED_TEST_SUITE_P
REGISTER_TYPED_TEST_SUITE_P(TraitSpecializedTest, Noop, CanMove, DebugString, TransportSize)
testing::internal::ProxyTypeList
Definition: googletest/googletest/include/gtest/internal/gtest-type-util.h:155
absl::SimpleAtoi
ABSL_NAMESPACE_BEGIN ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view str, int_type *out)
Definition: abseil-cpp/absl/strings/numbers.h:271
grpc_core::testing::FakeContainer::Set
void Set(CharTrait, char x)
Definition: parsed_metadata_test.cc:111
grpc_core::slice_detail::CopyConstructors< Slice >::FromCopiedString
static Slice FromCopiedString(const char *s)
Definition: src/core/lib/slice/slice.h:173
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_core::testing::FakeContainer
Definition: parsed_metadata_test.cc:109
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
grpc_core::testing::Int32Trait::test_memento
static int32_t test_memento()
Definition: parsed_metadata_test.cc:45
grpc_core::testing::IntptrTrait::test_memento
static intptr_t test_memento()
Definition: parsed_metadata_test.cc:79
grpc_core::testing::StringTrait::DisplayValue
static std::string DisplayValue(const std::string &value)
Definition: parsed_metadata_test.cc:106
grpc_core::testing::Int32Trait::test_value
static int32_t test_value()
Definition: parsed_metadata_test.cc:46
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
grpc_core::testing::StringTrait
Definition: parsed_metadata_test.cc:93
grpc_core::testing::IntptrTrait::DisplayValue
static std::string DisplayValue(intptr_t value)
Definition: parsed_metadata_test.cc:88
grpc_core::testing::CharTrait::MementoToValue
static char MementoToValue(char memento)
Definition: parsed_metadata_test.cc:35
grpc_core::testing::FakeContainer::MOCK_METHOD1
MOCK_METHOD1(SetChar, void(char))
grpc_core::testing::IntptrTrait::test_memento_transport_size
static size_t test_memento_transport_size()
Definition: parsed_metadata_test.cc:81
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
grpc_core::testing::Int64Trait::test_memento
static int64_t test_memento()
Definition: parsed_metadata_test.cc:62
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
grpc_core::testing::StringTrait::ParseMemento
static std::string ParseMemento(Slice slice, MetadataParseErrorFn)
Definition: parsed_metadata_test.cc:102
grpc_core::testing::Int32Trait::MementoToValue
static int32_t MementoToValue(int32_t memento)
Definition: parsed_metadata_test.cc:48
slice_internal.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::testing::Int32Trait::ParseMemento
static int32_t ParseMemento(Slice slice, MetadataParseErrorFn)
Definition: parsed_metadata_test.cc:49
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
grpc_core::testing::FakeParsedMetadata
::grpc_core::ParsedMetadata< FakeContainer > FakeParsedMetadata
Definition: parsed_metadata_test.cc:128
grpc_core::testing::StringTrait::test_memento
static std::string test_memento()
Definition: parsed_metadata_test.cc:96
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
grpc_core::ParsedMetadata
Definition: parsed_metadata.h:117
grpc_core::testing::StringTrait::key
static absl::string_view key()
Definition: parsed_metadata_test.cc:95
EXPECT_CALL
#define EXPECT_CALL(obj, call)
test_config.h
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_core::testing::StringTrait::MementoType
std::string MementoType
Definition: parsed_metadata_test.cc:94
grpc_core::testing::Int32Trait::MementoType
int32_t MementoType
Definition: parsed_metadata_test.cc:43
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
key
const char * key
Definition: hpack_parser_table.cc:164
grpc_core::testing::CharTrait
Definition: parsed_metadata_test.cc:29
grpc_core::ParsedMetadata::is_binary_header
bool is_binary_header() const
Definition: parsed_metadata.h:186
grpc_core::testing::Int64Trait::test_memento_transport_size
static size_t test_memento_transport_size()
Definition: parsed_metadata_test.cc:64
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
grpc_core::testing::Int64Trait::ParseMemento
static int64_t ParseMemento(Slice slice, MetadataParseErrorFn)
Definition: parsed_metadata_test.cc:66
absl::FunctionRef< void(absl::string_view error, const Slice &value)>
grpc_core::testing::TEST
TEST(ServiceConfigParserTest, DoubleRegistration)
Definition: service_config_test.cc:448
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
grpc_core::testing::IntptrTrait::ParseMemento
static intptr_t ParseMemento(Slice slice, MetadataParseErrorFn)
Definition: parsed_metadata_test.cc:83
grpc_core::testing::FakeContainer::Set
void Set(Int64Trait, int64_t x)
Definition: parsed_metadata_test.cc:113
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
grpc_core::testing::CharTrait::ParseMemento
static char ParseMemento(Slice slice, MetadataParseErrorFn)
Definition: parsed_metadata_test.cc:36
to_string
static bool to_string(zval *from)
Definition: protobuf/php/ext/google/protobuf/convert.c:333
grpc_core::testing::Int64Trait
Definition: parsed_metadata_test.cc:59
grpc_core::testing::StringTrait::test_value
static std::string test_value()
Definition: parsed_metadata_test.cc:97


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:39