Debian Codegraph @

test/core/transport/metadata_map_test.cc

raw
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 <stdlib.h>
18
19 #include <memory>
20 #include <string>
21
22 #include "absl/strings/str_cat.h"
23 #include "absl/types/optional.h"
24 #include "gtest/gtest.h"
25
26 #include <grpc/event_engine/memory_allocator.h>
27
28 #include "src/core/lib/gprpp/ref_counted_ptr.h"
29 #include "src/core/lib/gprpp/time.h"
30 #include "src/core/lib/resource_quota/arena.h"
31 #include "src/core/lib/resource_quota/memory_quota.h"
32 #include "src/core/lib/resource_quota/resource_quota.h"
33 #include "src/core/lib/slice/slice.h"
34 #include "src/core/lib/transport/metadata_batch.h"
35 #include "test/core/util/test_config.h"
36
37 namespace grpc_core {
38 namespace testing {
39
40 static auto* g_memory_allocator = new MemoryAllocator(
41 ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test"));
42
43 struct EmptyMetadataMap : public MetadataMap<EmptyMetadataMap> {
44 using MetadataMap<EmptyMetadataMap>::MetadataMap;
45 };
46
47 struct TimeoutOnlyMetadataMap
48 : public MetadataMap<TimeoutOnlyMetadataMap, GrpcTimeoutMetadata> {
49 using MetadataMap<TimeoutOnlyMetadataMap, GrpcTimeoutMetadata>::MetadataMap;
50 };
51
52 struct StreamNetworkStateMetadataMap
53 : public MetadataMap<StreamNetworkStateMetadataMap,
54 GrpcStreamNetworkState> {
55 using MetadataMap<StreamNetworkStateMetadataMap,
56 GrpcStreamNetworkState>::MetadataMap;
57 };
58
59 TEST(MetadataMapTest, Noop) {
60 auto arena = MakeScopedArena(1024, g_memory_allocator);
61 EmptyMetadataMap(arena.get());
62 }
63
64 TEST(MetadataMapTest, NoopWithDeadline) {
65 auto arena = MakeScopedArena(1024, g_memory_allocator);
66 TimeoutOnlyMetadataMap(arena.get());
67 }
68
69 TEST(MetadataMapTest, SimpleOps) {
70 auto arena = MakeScopedArena(1024, g_memory_allocator);
71 TimeoutOnlyMetadataMap map(arena.get());
72 EXPECT_EQ(map.get_pointer(GrpcTimeoutMetadata()), nullptr);
73 EXPECT_EQ(map.get(GrpcTimeoutMetadata()), absl::nullopt);
74 map.Set(GrpcTimeoutMetadata(),
75 Timestamp::FromMillisecondsAfterProcessEpoch(1234));
76 EXPECT_NE(map.get_pointer(GrpcTimeoutMetadata()), nullptr);
77 EXPECT_EQ(*map.get_pointer(GrpcTimeoutMetadata()),
78 Timestamp::FromMillisecondsAfterProcessEpoch(1234));
79 EXPECT_EQ(map.get(GrpcTimeoutMetadata()),
80 Timestamp::FromMillisecondsAfterProcessEpoch(1234));
81 map.Remove(GrpcTimeoutMetadata());
82 EXPECT_EQ(map.get_pointer(GrpcTimeoutMetadata()), nullptr);
83 EXPECT_EQ(map.get(GrpcTimeoutMetadata()), absl::nullopt);
84 }
85
86 // Target for MetadataMap::Encode.
87 // Writes down some string representation of what it receives, so we can
88 // EXPECT_EQ it later.
89 class FakeEncoder {
90 public:
91 std::string output() { return output_; }
92
93 void Encode(const Slice& key, const Slice& value) {
94 output_ += absl::StrCat("UNKNOWN METADATUM: key=", key.as_string_view(),
95 " value=", value.as_string_view(), "\n");
96 }
97
98 void Encode(GrpcTimeoutMetadata, Timestamp deadline) {
99 output_ += absl::StrCat("grpc-timeout: deadline=",
100 deadline.milliseconds_after_process_epoch(), "\n");
101 }
102
103 private:
104 std::string output_;
105 };
106
107 TEST(MetadataMapTest, EmptyEncodeTest) {
108 FakeEncoder encoder;
109 auto arena = MakeScopedArena(1024, g_memory_allocator);
110 TimeoutOnlyMetadataMap map(arena.get());
111 map.Encode(&encoder);
112 EXPECT_EQ(encoder.output(), "");
113 }
114
115 TEST(MetadataMapTest, TimeoutEncodeTest) {
116 FakeEncoder encoder;
117 auto arena = MakeScopedArena(1024, g_memory_allocator);
118 TimeoutOnlyMetadataMap map(arena.get());
119 map.Set(GrpcTimeoutMetadata(),
120 Timestamp::FromMillisecondsAfterProcessEpoch(1234));
121 map.Encode(&encoder);
122 EXPECT_EQ(encoder.output(), "grpc-timeout: deadline=1234\n");
123 }
124
125 TEST(MetadataMapTest, NonEncodableTrait) {
126 struct EncoderWithNoTraitEncodeFunctions {
127 void Encode(const Slice&, const Slice&) {
128 abort(); // should not be called
129 }
130 };
131 auto arena = MakeScopedArena(1024, g_memory_allocator);
132 StreamNetworkStateMetadataMap map(arena.get());
133 map.Set(GrpcStreamNetworkState(), GrpcStreamNetworkState::kNotSentOnWire);
134 EXPECT_EQ(map.get(GrpcStreamNetworkState()),
135 GrpcStreamNetworkState::kNotSentOnWire);
136 EncoderWithNoTraitEncodeFunctions encoder;
137 map.Encode(&encoder);
138 EXPECT_EQ(map.DebugString(), "GrpcStreamNetworkState: not sent on wire");
139 }
140
141 TEST(DebugStringBuilderTest, AddOne) {
142 metadata_detail::DebugStringBuilder b;
143 b.Add("a", "b");
144 EXPECT_EQ(b.TakeOutput(), "a: b");
145 }
146
147 TEST(DebugStringBuilderTest, AddTwo) {
148 metadata_detail::DebugStringBuilder b;
149 b.Add("a", "b");
150 b.Add("c", "d");
151 EXPECT_EQ(b.TakeOutput(), "a: b, c: d");
152 }
153
154 } // namespace testing
155 } // namespace grpc_core
156
157 int main(int argc, char** argv) {
158 testing::InitGoogleTest(&argc, argv);
159 grpc::testing::TestEnvironment env(&argc, argv);
160 return RUN_ALL_TESTS();
161 };