Debian Codegraph @

test/core/transport/chttp2/stream_map_test.cc

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "src/core/ext/transport/chttp2/transport/stream_map.h"
20
21 #include "gtest/gtest.h"
22
23 #include <grpc/support/log.h>
24
25 #include "test/core/util/test_config.h"
26
27 #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
28
29 /* test creation & destruction */
30 static void test_no_op(void) {
31 grpc_chttp2_stream_map map;
32
33 LOG_TEST("test_no_op");
34
35 grpc_chttp2_stream_map_init(&map, 8);
36 grpc_chttp2_stream_map_destroy(&map);
37 }
38
39 /* test lookup on an empty map */
40 static void test_empty_find(void) {
41 grpc_chttp2_stream_map map;
42
43 LOG_TEST("test_empty_find");
44
45 grpc_chttp2_stream_map_init(&map, 8);
46 ASSERT_EQ(nullptr, grpc_chttp2_stream_map_find(&map, 39128));
47 grpc_chttp2_stream_map_destroy(&map);
48 }
49
50 /* test add & lookup */
51 static void test_basic_add_find(uint32_t n) {
52 grpc_chttp2_stream_map map;
53 uint32_t i;
54 size_t got;
55
56 LOG_TEST("test_basic_add_find");
57 gpr_log(GPR_INFO, "n = %d", n);
58
59 grpc_chttp2_stream_map_init(&map, 8);
60 ASSERT_EQ(0, grpc_chttp2_stream_map_size(&map));
61 for (i = 1; i <= n; i++) {
62 grpc_chttp2_stream_map_add(&map, i, reinterpret_cast<void*>(i));
63 }
64 ASSERT_EQ(n, grpc_chttp2_stream_map_size(&map));
65 ASSERT_EQ(nullptr, grpc_chttp2_stream_map_find(&map, 0));
66 ASSERT_EQ(nullptr, grpc_chttp2_stream_map_find(&map, n + 1));
67 for (i = 1; i <= n; i++) {
68 got = reinterpret_cast<uintptr_t>(grpc_chttp2_stream_map_find(&map, i));
69 ASSERT_EQ(i, got);
70 }
71 grpc_chttp2_stream_map_destroy(&map);
72 }
73
74 /* verify that for_each gets the right values during test_delete_evens_XXX */
75 static void verify_for_each(void* user_data, uint32_t stream_id, void* ptr) {
76 uint32_t* for_each_check = static_cast<uint32_t*>(user_data);
77 ASSERT_TRUE(ptr);
78 ASSERT_EQ(*for_each_check, stream_id);
79 *for_each_check += 2;
80 }
81
82 static void check_delete_evens(grpc_chttp2_stream_map* map, uint32_t n) {
83 uint32_t for_each_check = 1;
84 uint32_t i;
85 size_t got;
86
87 ASSERT_EQ(nullptr, grpc_chttp2_stream_map_find(map, 0));
88 ASSERT_EQ(nullptr, grpc_chttp2_stream_map_find(map, n + 1));
89 for (i = 1; i <= n; i++) {
90 if (i & 1) {
91 got = reinterpret_cast<uintptr_t>(grpc_chttp2_stream_map_find(map, i));
92 ASSERT_EQ(i, got);
93 } else {
94 ASSERT_EQ(nullptr, grpc_chttp2_stream_map_find(map, i));
95 }
96 }
97
98 grpc_chttp2_stream_map_for_each(map, verify_for_each, &for_each_check);
99 if (n & 1) {
100 ASSERT_EQ(for_each_check, n + 2);
101 } else {
102 ASSERT_EQ(for_each_check, n + 1);
103 }
104 }
105
106 /* add a bunch of keys, delete the even ones, and make sure the map is
107 consistent */
108 static void test_delete_evens_sweep(uint32_t n) {
109 grpc_chttp2_stream_map map;
110 uint32_t i;
111
112 LOG_TEST("test_delete_evens_sweep");
113 gpr_log(GPR_INFO, "n = %d", n);
114
115 grpc_chttp2_stream_map_init(&map, 8);
116 for (i = 1; i <= n; i++) {
117 grpc_chttp2_stream_map_add(&map, i, reinterpret_cast<void*>(i));
118 }
119 for (i = 1; i <= n; i++) {
120 if ((i & 1) == 0) {
121 ASSERT_EQ((void*)(uintptr_t)i, grpc_chttp2_stream_map_delete(&map, i));
122 }
123 }
124 check_delete_evens(&map, n);
125 grpc_chttp2_stream_map_destroy(&map);
126 }
127
128 /* add a bunch of keys, delete the even ones immediately, and make sure the map
129 is consistent */
130 static void test_delete_evens_incremental(uint32_t n) {
131 grpc_chttp2_stream_map map;
132 uint32_t i;
133
134 LOG_TEST("test_delete_evens_incremental");
135 gpr_log(GPR_INFO, "n = %d", n);
136
137 grpc_chttp2_stream_map_init(&map, 8);
138 for (i = 1; i <= n; i++) {
139 grpc_chttp2_stream_map_add(&map, i, reinterpret_cast<void*>(i));
140 if ((i & 1) == 0) {
141 grpc_chttp2_stream_map_delete(&map, i);
142 }
143 }
144 check_delete_evens(&map, n);
145 grpc_chttp2_stream_map_destroy(&map);
146 }
147
148 /* add a bunch of keys, delete old ones after some time, ensure the
149 backing array does not grow */
150 static void test_periodic_compaction(uint32_t n) {
151 grpc_chttp2_stream_map map;
152 uint32_t i;
153 uint32_t del;
154
155 LOG_TEST("test_periodic_compaction");
156 gpr_log(GPR_INFO, "n = %d", n);
157
158 grpc_chttp2_stream_map_init(&map, 16);
159 ASSERT_EQ(map.capacity, 16);
160 for (i = 1; i <= n; i++) {
161 grpc_chttp2_stream_map_add(&map, i, reinterpret_cast<void*>(i));
162 if (i > 8) {
163 del = i - 8;
164 ASSERT_EQ((void*)(uintptr_t)del,
165 grpc_chttp2_stream_map_delete(&map, del));
166 }
167 }
168 ASSERT_EQ(map.capacity, 16);
169 grpc_chttp2_stream_map_destroy(&map);
170 }
171
172 TEST(StreamMapTest, MainTest) {
173 uint32_t n = 1;
174 uint32_t prev = 1;
175 uint32_t tmp;
176
177 test_no_op();
178 test_empty_find();
179
180 while (n < 100000) {
181 test_basic_add_find(n);
182 test_delete_evens_sweep(n);
183 test_delete_evens_incremental(n);
184 test_periodic_compaction(n);
185
186 tmp = n;
187 n += prev;
188 prev = tmp;
189 }
190 }
191
192 int main(int argc, char** argv) {
193 grpc::testing::TestEnvironment env(&argc, argv);
194 ::testing::InitGoogleTest(&argc, argv);
195 return RUN_ALL_TESTS();
196 }