Debian Codegraph @

test/core/iomgr/timer_heap_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/lib/iomgr/timer_heap.h"
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <gtest/gtest.h>
25
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28
29 #include "src/core/lib/gpr/useful.h"
30 #include "src/core/lib/iomgr/port.h"
31 #include "test/core/util/test_config.h"
32
33 static gpr_atm random_deadline(void) { return rand(); }
34
35 static grpc_timer* create_test_elements(size_t num_elements) {
36 grpc_timer* elems =
37 static_cast<grpc_timer*>(gpr_malloc(num_elements * sizeof(grpc_timer)));
38 size_t i;
39 for (i = 0; i < num_elements; i++) {
40 elems[i].deadline = random_deadline();
41 }
42 return elems;
43 }
44
45 static int contains(grpc_timer_heap* pq, grpc_timer* el) {
46 size_t i;
47 for (i = 0; i < pq->timer_count; i++) {
48 if (pq->timers[i] == el) return 1;
49 }
50 return 0;
51 }
52
53 static void check_valid(grpc_timer_heap* pq) {
54 size_t i;
55 for (i = 0; i < pq->timer_count; ++i) {
56 size_t left_child = 1u + 2u * i;
57 size_t right_child = left_child + 1u;
58 if (left_child < pq->timer_count) {
59 ASSERT_LE(pq->timers[i]->deadline, pq->timers[left_child]->deadline);
60 }
61 if (right_child < pq->timer_count) {
62 ASSERT_LE(pq->timers[i]->deadline, pq->timers[right_child]->deadline);
63 }
64 }
65 }
66
67 /*******************************************************************************
68 * test1
69 */
70
71 static void test1(void) {
72 grpc_timer_heap pq;
73 const size_t num_test_elements = 200;
74 const size_t num_test_operations = 10000;
75 size_t i;
76 grpc_timer* test_elements = create_test_elements(num_test_elements);
77 uint8_t* inpq = static_cast<uint8_t*>(gpr_malloc(num_test_elements));
78
79 gpr_log(GPR_INFO, "test1");
80
81 grpc_timer_heap_init(&pq);
82 memset(inpq, 0, num_test_elements);
83 ASSERT_TRUE(grpc_timer_heap_is_empty(&pq));
84 check_valid(&pq);
85 for (i = 0; i < num_test_elements; ++i) {
86 ASSERT_FALSE(contains(&pq, &test_elements[i]));
87 grpc_timer_heap_add(&pq, &test_elements[i]);
88 check_valid(&pq);
89 ASSERT_TRUE(contains(&pq, &test_elements[i]));
90 inpq[i] = 1;
91 }
92 for (i = 0; i < num_test_elements; ++i) {
93 /* Test that check still succeeds even for element that wasn't just
94 inserted. */
95 ASSERT_TRUE(contains(&pq, &test_elements[i]));
96 }
97
98 ASSERT_EQ(pq.timer_count, num_test_elements);
99
100 check_valid(&pq);
101
102 for (i = 0; i < num_test_operations; ++i) {
103 size_t elem_num = static_cast<size_t>(rand()) % num_test_elements;
104 grpc_timer* el = &test_elements[elem_num];
105 if (!inpq[elem_num]) { /* not in pq */
106 ASSERT_FALSE(contains(&pq, el));
107 el->deadline = random_deadline();
108 grpc_timer_heap_add(&pq, el);
109 ASSERT_TRUE(contains(&pq, el));
110 inpq[elem_num] = 1;
111 check_valid(&pq);
112 } else {
113 ASSERT_TRUE(contains(&pq, el));
114 grpc_timer_heap_remove(&pq, el);
115 ASSERT_FALSE(contains(&pq, el));
116 inpq[elem_num] = 0;
117 check_valid(&pq);
118 }
119 }
120
121 grpc_timer_heap_destroy(&pq);
122 gpr_free(test_elements);
123 gpr_free(inpq);
124 }
125
126 /*******************************************************************************
127 * test2
128 */
129
130 typedef struct {
131 grpc_timer elem;
132 bool inserted;
133 } elem_struct;
134
135 static elem_struct* search_elems(elem_struct* elems, size_t count,
136 bool inserted) {
137 size_t* search_order =
138 static_cast<size_t*>(gpr_malloc(count * sizeof(*search_order)));
139 for (size_t i = 0; i < count; i++) {
140 search_order[i] = i;
141 }
142 for (size_t i = 0; i < count * 2; i++) {
143 size_t a = static_cast<size_t>(rand()) % count;
144 size_t b = static_cast<size_t>(rand()) % count;
145 std::swap(search_order[a], search_order[b]);
146 }
147 elem_struct* out = nullptr;
148 for (size_t i = 0; out == nullptr && i < count; i++) {
149 if (elems[search_order[i]].inserted == inserted) {
150 out = &elems[search_order[i]];
151 }
152 }
153 gpr_free(search_order);
154 return out;
155 }
156
157 static void test2(void) {
158 gpr_log(GPR_INFO, "test2");
159
160 grpc_timer_heap pq;
161
162 static const size_t elems_size = 1000;
163 elem_struct* elems =
164 static_cast<elem_struct*>(gpr_malloc(elems_size * sizeof(elem_struct)));
165 size_t num_inserted = 0;
166
167 grpc_timer_heap_init(&pq);
168 memset(elems, 0, elems_size * sizeof(elems[0]));
169
170 for (size_t round = 0; round < 10000; round++) {
171 int r = rand() % 1000;
172 if (r <= 550) {
173 /* 55% of the time we try to add something */
174 elem_struct* el = search_elems(elems, elems_size, false);
175 if (el != nullptr) {
176 el->elem.deadline = random_deadline();
177 grpc_timer_heap_add(&pq, &el->elem);
178 el->inserted = true;
179 num_inserted++;
180 check_valid(&pq);
181 }
182 } else if (r <= 650) {
183 /* 10% of the time we try to remove something */
184 elem_struct* el = search_elems(elems, elems_size, true);
185 if (el != nullptr) {
186 grpc_timer_heap_remove(&pq, &el->elem);
187 el->inserted = false;
188 num_inserted--;
189 check_valid(&pq);
190 }
191 } else {
192 /* the remaining times we pop */
193 if (num_inserted > 0) {
194 grpc_timer* top = grpc_timer_heap_top(&pq);
195 grpc_timer_heap_pop(&pq);
196 for (size_t i = 0; i < elems_size; i++) {
197 if (top == &elems[i].elem) {
198 ASSERT_TRUE(elems[i].inserted);
199 elems[i].inserted = false;
200 }
201 }
202 num_inserted--;
203 check_valid(&pq);
204 }
205 }
206
207 if (num_inserted) {
208 int64_t* min_deadline = nullptr;
209 for (size_t i = 0; i < elems_size; i++) {
210 if (elems[i].inserted) {
211 if (min_deadline == nullptr) {
212 min_deadline = &elems[i].elem.deadline;
213 } else {
214 if (elems[i].elem.deadline < *min_deadline) {
215 min_deadline = &elems[i].elem.deadline;
216 }
217 }
218 }
219 }
220 ASSERT_EQ(grpc_timer_heap_top(&pq)->deadline, *min_deadline);
221 }
222 }
223
224 grpc_timer_heap_destroy(&pq);
225 gpr_free(elems);
226 }
227
228 static void shrink_test(void) {
229 gpr_log(GPR_INFO, "shrink_test");
230
231 grpc_timer_heap pq;
232 size_t i;
233 size_t expected_size;
234
235 /* A large random number to allow for multiple shrinkages, at least 512. */
236 const size_t num_elements = static_cast<size_t>(rand()) % 2000 + 512;
237
238 grpc_timer_heap_init(&pq);
239
240 /* Create a priority queue with many elements. Make sure the Size() is
241 correct. */
242 for (i = 0; i < num_elements; ++i) {
243 ASSERT_EQ(i, pq.timer_count);
244 grpc_timer_heap_add(&pq, create_test_elements(1));
245 }
246 ASSERT_EQ(num_elements, pq.timer_count);
247
248 /* Remove elements until the Size is 1/4 the original size. */
249 while (pq.timer_count > num_elements / 4) {
250 grpc_timer* const te = pq.timers[pq.timer_count - 1];
251 grpc_timer_heap_remove(&pq, te);
252 gpr_free(te);
253 }
254 ASSERT_EQ(num_elements / 4, pq.timer_count);
255
256 /* Expect that Capacity is in the right range:
257 Size * 2 <= Capacity <= Size * 4 */
258 ASSERT_LE(pq.timer_count * 2, pq.timer_capacity);
259 ASSERT_LE(pq.timer_capacity, pq.timer_count * 4);
260 check_valid(&pq);
261
262 /* Remove the rest of the elements. Check that the Capacity is not more than
263 4 times the Size and not less than 2 times, but never goes below 16. */
264 expected_size = pq.timer_count;
265 while (pq.timer_count > 0) {
266 const size_t which = static_cast<size_t>(rand()) % pq.timer_count;
267 grpc_timer* te = pq.timers[which];
268 grpc_timer_heap_remove(&pq, te);
269 gpr_free(te);
270 expected_size--;
271 ASSERT_EQ(expected_size, pq.timer_count);
272 ASSERT_LE(pq.timer_count * 2, pq.timer_capacity);
273 if (pq.timer_count >= 8) {
274 ASSERT_LE(pq.timer_capacity, pq.timer_count * 4);
275 } else {
276 ASSERT_LE(16, pq.timer_capacity);
277 }
278 check_valid(&pq);
279 }
280
281 ASSERT_EQ(pq.timer_count, 0);
282 ASSERT_GE(pq.timer_capacity, 16);
283 ASSERT_LT(pq.timer_capacity, 32);
284
285 grpc_timer_heap_destroy(&pq);
286 }
287
288 TEST(TimerHeapTest, MainTest) {
289 for (int i = 0; i < 5; i++) {
290 test1();
291 test2();
292 shrink_test();
293 }
294 }
295
296 int main(int argc, char** argv) {
297 grpc::testing::TestEnvironment env(&argc, argv);
298 ::testing::InitGoogleTest(&argc, argv);
299 return RUN_ALL_TESTS();
300 }