metadata_map_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 
17 #include <gtest/gtest.h>
18 
23 
24 namespace grpc_core {
25 namespace testing {
26 
28  ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test"));
29 
30 struct EmptyMetadataMap : public MetadataMap<EmptyMetadataMap> {
32 };
33 
35  : public MetadataMap<TimeoutOnlyMetadataMap, GrpcTimeoutMetadata> {
37 };
38 
40  : public MetadataMap<StreamNetworkStateMetadataMap,
41  GrpcStreamNetworkState> {
44 };
45 
46 TEST(MetadataMapTest, Noop) {
48  EmptyMetadataMap(arena.get());
49 }
50 
51 TEST(MetadataMapTest, NoopWithDeadline) {
54 }
55 
56 TEST(MetadataMapTest, SimpleOps) {
59  EXPECT_EQ(map.get_pointer(GrpcTimeoutMetadata()), nullptr);
60  EXPECT_EQ(map.get(GrpcTimeoutMetadata()), absl::nullopt);
63  EXPECT_NE(map.get_pointer(GrpcTimeoutMetadata()), nullptr);
64  EXPECT_EQ(*map.get_pointer(GrpcTimeoutMetadata()),
68  map.Remove(GrpcTimeoutMetadata());
69  EXPECT_EQ(map.get_pointer(GrpcTimeoutMetadata()), nullptr);
70  EXPECT_EQ(map.get(GrpcTimeoutMetadata()), absl::nullopt);
71 }
72 
73 // Target for MetadataMap::Encode.
74 // Writes down some string representation of what it receives, so we can
75 // EXPECT_EQ it later.
76 class FakeEncoder {
77  public:
78  std::string output() { return output_; }
79 
80  void Encode(const Slice& key, const Slice& value) {
81  output_ += absl::StrCat("UNKNOWN METADATUM: key=", key.as_string_view(),
82  " value=", value.as_string_view(), "\n");
83  }
84 
86  output_ += absl::StrCat("grpc-timeout: deadline=",
87  deadline.milliseconds_after_process_epoch(), "\n");
88  }
89 
90  private:
92 };
93 
94 TEST(MetadataMapTest, EmptyEncodeTest) {
95  FakeEncoder encoder;
98  map.Encode(&encoder);
99  EXPECT_EQ(encoder.output(), "");
100 }
101 
102 TEST(MetadataMapTest, TimeoutEncodeTest) {
103  FakeEncoder encoder;
106  map.Set(GrpcTimeoutMetadata(),
108  map.Encode(&encoder);
109  EXPECT_EQ(encoder.output(), "grpc-timeout: deadline=1234\n");
110 }
111 
112 TEST(MetadataMapTest, NonEncodableTrait) {
113  struct EncoderWithNoTraitEncodeFunctions {
114  void Encode(const Slice&, const Slice&) {
115  abort(); // should not be called
116  }
117  };
123  EncoderWithNoTraitEncodeFunctions encoder;
124  map.Encode(&encoder);
125  EXPECT_EQ(map.DebugString(), "GrpcStreamNetworkState: not sent on wire");
126 }
127 
128 TEST(DebugStringBuilderTest, AddOne) {
130  b.Add("a", "b");
131  EXPECT_EQ(b.TakeOutput(), "a: b");
132 }
133 
134 TEST(DebugStringBuilderTest, AddTwo) {
136  b.Add("a", "b");
137  b.Add("c", "d");
138  EXPECT_EQ(b.TakeOutput(), "a: b, c: d");
139 }
140 
141 } // namespace testing
142 } // namespace grpc_core
143 
144 int main(int argc, char** argv) {
145  testing::InitGoogleTest(&argc, argv);
146  grpc::testing::TestEnvironment env(&argc, argv);
147  return RUN_ALL_TESTS();
148 };
grpc_core::MakeScopedArena
ScopedArenaPtr MakeScopedArena(size_t initial_size, MemoryAllocator *memory_allocator)
Definition: src/core/lib/resource_quota/arena.h:130
testing
Definition: aws_request_signer_test.cc:25
grpc_core::GrpcTimeoutMetadata
Definition: metadata_batch.h:59
grpc_core::metadata_detail::DebugStringBuilder
Definition: metadata_batch.h:396
metadata_batch.h
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
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:35
grpc_core::testing::StreamNetworkStateMetadataMap
Definition: metadata_map_test.cc:39
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::testing::FakeEncoder::output
std::string output()
Definition: metadata_map_test.cc:78
grpc_core::Slice
Definition: src/core/lib/slice/slice.h:282
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
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::TimeoutOnlyMetadataMap
Definition: metadata_map_test.cc:34
grpc_core::testing::FakeEncoder::Encode
void Encode(const Slice &key, const Slice &value)
Definition: metadata_map_test.cc:80
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
grpc_core::testing::FakeEncoder::Encode
void Encode(GrpcTimeoutMetadata, Timestamp deadline)
Definition: metadata_map_test.cc:85
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
grpc_core::MetadataMap
Definition: metadata_batch.h:984
main
int main(int argc, char **argv)
Definition: metadata_map_test.cc:144
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
grpc_core::Timestamp::milliseconds_after_process_epoch
uint64_t milliseconds_after_process_epoch() const
Definition: src/core/lib/gprpp/time.h:109
grpc_core::ResourceQuota::Default
static ResourceQuotaRefPtr Default()
Definition: resource_quota.cc:27
grpc_core::MetadataMap< StreamNetworkStateMetadataMap, GrpcStreamNetworkState >::MetadataMap
MetadataMap(Arena *arena)
Definition: metadata_batch.h:1193
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
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
resource_quota.h
grpc_core::GrpcStreamNetworkState::kNotSentOnWire
@ kNotSentOnWire
Definition: metadata_batch.h:369
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch
static constexpr Timestamp FromMillisecondsAfterProcessEpoch(int64_t millis)
Definition: src/core/lib/gprpp/time.h:73
grpc_core::testing::FakeEncoder::output_
std::string output_
Definition: metadata_map_test.cc:91
test_config.h
value
const char * value
Definition: hpack_parser_table.cc:165
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::FakeEncoder
Definition: metadata_map_test.cc:76
grpc_core::GrpcStreamNetworkState
Definition: metadata_batch.h:365
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_core::testing::g_memory_allocator
static auto * g_memory_allocator
Definition: chunked_vector_test.cc:24
grpc_core::testing::TEST
TEST(ServiceConfigParserTest, DoubleRegistration)
Definition: service_config_test.cc:448
grpc_core::testing::EmptyMetadataMap
Definition: metadata_map_test.cc:30


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