Debian Codegraph @

c/tests/refcount_test.cpp

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include "./pn_test.hpp"
23 #include "catch_extra.hpp"
24
25 #include <proton/connection.h>
26 #include <proton/delivery.h>
27 #include <proton/event.h>
28 #include <proton/link.h>
29 #include <proton/session.h>
30 #include <proton/transport.h>
31
32 /**
33 * The decref order tests validate that whenever the last pointer to a
34 * child object, e.g. a session or a link, is about to go away, the
35 * parent object takes ownership of that reference if the child object
36 * has not been freed, this avoids reference cycles but allows
37 * navigation from parents to children.
38 **/
39
40 #define SETUP_CSL \
41 pn_connection_t *conn = pn_connection(); \
42 pn_session_t *ssn = pn_session(conn); \
43 pn_incref(ssn); \
44 pn_link_t *lnk = pn_sender(ssn, "sender"); \
45 pn_incref(lnk); \
46 \
47 REQUIRE(pn_refcount(conn) == 2); \
48 REQUIRE(pn_refcount(ssn) == 2); \
49 REQUIRE(pn_refcount(lnk) == 1);
50
51 TEST_CASE("test_decref_order_csl") {
52 SETUP_CSL;
53
54 pn_decref(conn);
55 REQUIRE(pn_refcount(conn) == 1); // session keeps alive
56 pn_decref(ssn);
57 REQUIRE(pn_refcount(ssn) == 1); // link keeps alive
58 pn_decref(lnk);
59 // all gone now (requires valgrind to validate)
60 }
61
62 TEST_CASE("test_decref_order_cls") {
63 SETUP_CSL;
64
65 pn_decref(conn);
66 REQUIRE(pn_refcount(conn) == 1); // session keeps alive
67 pn_decref(lnk);
68 REQUIRE(pn_refcount(lnk) == 1); // session takes over ownership
69 pn_decref(ssn);
70 // all gone now (requires valgrind to validate)
71 }
72
73 TEST_CASE("test_decref_order_lcs") {
74 SETUP_CSL;
75
76 pn_decref(lnk);
77 REQUIRE(pn_refcount(lnk) == 1); // session takes over ownership
78 pn_decref(conn);
79 REQUIRE(pn_refcount(conn) == 1); // session keeps alive
80 pn_decref(ssn);
81 // all gone now (requires valgrind to validate)
82 }
83
84 TEST_CASE("test_decref_order_scl") {
85 SETUP_CSL;
86
87 pn_decref(ssn);
88 REQUIRE(pn_refcount(ssn) == 1); // link keeps alive
89 pn_decref(conn);
90 REQUIRE(pn_refcount(conn) == 1); // session keeps alive
91 pn_decref(lnk);
92 // all gone now (requires valgrind to validate)
93 }
94
95 TEST_CASE("test_decref_order_slc") {
96 SETUP_CSL;
97
98 pn_decref(ssn);
99 REQUIRE(pn_refcount(ssn) == 1); // link keeps alive
100 pn_decref(lnk);
101 REQUIRE(pn_refcount(ssn) == 1); // connection takes over ownership
102 REQUIRE(pn_refcount(lnk) == 1); // session takes over ownership
103 pn_decref(conn);
104 // all gone now (requires valgrind to validate)
105 }
106
107 TEST_CASE("test_decref_order_lsc") {
108 SETUP_CSL;
109
110 pn_decref(lnk);
111 REQUIRE(pn_refcount(lnk) == 1); // session takes over ownership
112 REQUIRE(pn_refcount(ssn) == 1);
113 pn_decref(ssn);
114 REQUIRE(pn_refcount(lnk) == 1);
115 REQUIRE(pn_refcount(ssn) == 1); // connection takes over ownership
116 pn_decref(conn);
117 // all gone now (requires valgrind to validate)
118 }
119
120 /**
121 * The incref order tests verify that once ownership of the last
122 * pointer to a child is taken over by a parent, it is reassigned when
123 * the child is increfed.
124 **/
125
126 #define SETUP_INCREF_ORDER \
127 SETUP_CSL; \
128 pn_decref(lnk); \
129 pn_decref(ssn); \
130 REQUIRE(pn_refcount(lnk) == 1); \
131 REQUIRE(pn_refcount(ssn) == 1); \
132 REQUIRE(pn_refcount(conn) == 1);
133
134 TEST_CASE("test_incref_order_sl") {
135 SETUP_INCREF_ORDER;
136
137 pn_incref(ssn);
138 REQUIRE(pn_refcount(conn) == 2);
139 REQUIRE(pn_refcount(ssn) == 1);
140 REQUIRE(pn_refcount(lnk) == 1);
141 pn_incref(lnk);
142 REQUIRE(pn_refcount(conn) == 2);
143 REQUIRE(pn_refcount(ssn) == 2);
144 REQUIRE(pn_refcount(lnk) == 1);
145
146 pn_decref(conn);
147 pn_decref(ssn);
148 pn_decref(lnk);
149 }
150
151 TEST_CASE("test_incref_order_ls") {
152 SETUP_INCREF_ORDER;
153
154 pn_incref(lnk);
155 REQUIRE(pn_refcount(conn) == 2);
156 REQUIRE(pn_refcount(ssn) == 1);
157 REQUIRE(pn_refcount(lnk) == 1);
158 pn_incref(ssn);
159 REQUIRE(pn_refcount(conn) == 2);
160 REQUIRE(pn_refcount(ssn) == 2);
161 REQUIRE(pn_refcount(lnk) == 1);
162
163 pn_decref(conn);
164 pn_decref(ssn);
165 pn_decref(lnk);
166 }
167
168 static void swap(int array[], int i, int j) {
169 int a = array[i];
170 int b = array[j];
171 array[j] = a;
172 array[i] = b;
173 }
174
175 static void setup(void **objects) {
176 pn_connection_t *conn = pn_connection();
177 pn_session_t *ssn = pn_session(conn);
178 pn_incref(ssn);
179 pn_link_t *lnk = pn_sender(ssn, "sender");
180 pn_incref(lnk);
181 pn_delivery_t *dlv = pn_delivery(lnk, pn_dtag("dtag", 4));
182 pn_incref(dlv);
183
184 REQUIRE(pn_refcount(conn) == 2);
185 REQUIRE(pn_refcount(ssn) == 2);
186 REQUIRE(pn_refcount(lnk) == 2);
187 REQUIRE(pn_refcount(dlv) == 1);
188
189 objects[0] = conn;
190 objects[1] = ssn;
191 objects[2] = lnk;
192 objects[3] = dlv;
193 }
194
195 static bool decreffed(int *indexes, void **objects, int step, void *object) {
196 for (int i = 0; i <= step; i++) {
197 if (object == objects[indexes[i]]) {
198 return true;
199 }
200 }
201 return false;
202 }
203
204 static bool live_descendent(int *indexes, void **objects, int step,
205 int objidx) {
206 for (int i = objidx + 1; i < 4; i++) {
207 if (!decreffed(indexes, objects, step, objects[i])) {
208 return true;
209 }
210 }
211
212 return false;
213 }
214
215 static void assert_refcount(void *object, int expected) {
216 int rc = pn_refcount(object);
217 // printf("pn_refcount(%s) = %d\n", pn_object_reify(object)->name, rc);
218 REQUIRE(rc == expected);
219 }
220
221 static void test_decref_order(int *indexes, void **objects) {
222 setup(objects);
223
224 // printf("-----------\n");
225 for (int i = 0; i < 3; i++) {
226 int idx = indexes[i];
227 void *obj = objects[idx];
228 // printf("decreffing %s\n", pn_object_reify(obj)->name);
229 pn_decref(obj);
230 for (int j = 0; j <= i; j++) {
231 // everything we've decreffed already should have a refcount of
232 // 1 because it has been preserved by its parent
233 assert_refcount(objects[indexes[j]], 1);
234 }
235 for (int j = i + 1; j < 4; j++) {
236 // everything we haven't decreffed yet should have a refcount of
237 // 2 unless it has a descendant that has not been decrefed (or
238 // it has no child) in which case it should have a refcount of 1
239 int idx = indexes[j];
240 void *obj = objects[idx];
241 REQUIRE(!decreffed(indexes, objects, i, obj));
242 if (live_descendent(indexes, objects, i, idx)) {
243 assert_refcount(obj, 2);
244 } else {
245 assert_refcount(obj, 1);
246 }
247 }
248 }
249
250 void *last = objects[indexes[3]];
251 // printf("decreffing %s\n", pn_object_reify(last)->name);
252 pn_decref(last);
253 // all should be gone now, need to run with valgrind to check
254 }
255
256 static void permute(int n, int *indexes, void **objects) {
257 int j;
258 if (n == 1) {
259 test_decref_order(indexes, objects);
260 } else {
261 for (int i = 1; i <= n; i++) {
262 permute(n - 1, indexes, objects);
263 if ((n % 2) == 1) {
264 j = 1;
265 } else {
266 j = i;
267 }
268 swap(indexes, j - 1, n - 1);
269 }
270 }
271 }
272
273 TEST_CASE("test_decref_permutations") {
274 void *objects[4];
275 int indexes[4] = {0, 1, 2, 3};
276 permute(4, indexes, objects);
277 }
278
279 TEST_CASE("test_transport") {
280 pn_transport_t *transport = pn_transport();
281 REQUIRE(pn_refcount(transport) == 1);
282 pn_incref(transport);
283 REQUIRE(pn_refcount(transport) == 2);
284 pn_decref(transport);
285 REQUIRE(pn_refcount(transport) == 1);
286 pn_free(transport);
287 }
288
289 TEST_CASE("test_connection_transport") {
290 pn_connection_t *connection = pn_connection();
291 REQUIRE(pn_refcount(connection) == 1);
292 pn_transport_t *transport = pn_transport();
293 REQUIRE(pn_refcount(transport) == 1);
294 pn_transport_bind(transport, connection);
295 REQUIRE(pn_refcount(connection) == 2);
296 pn_decref(transport);
297 REQUIRE(pn_refcount(transport) == 1); // preserved by the bind
298 REQUIRE(pn_refcount(connection) == 1);
299 pn_free(connection);
300 }
301
302 TEST_CASE("test_transport_connection") {
303 pn_transport_t *transport = pn_transport();
304 REQUIRE(pn_refcount(transport) == 1);
305 pn_connection_t *connection = pn_connection();
306 REQUIRE(pn_refcount(connection) == 1);
307 pn_transport_bind(transport, connection);
308 REQUIRE(pn_refcount(connection) == 2);
309 pn_decref(connection);
310 REQUIRE(pn_refcount(connection) == 1);
311 REQUIRE(pn_refcount(transport) == 1);
312 pn_free(transport);
313 }
314
315 static void drain(pn_collector_t *collector) {
316 while (pn_collector_next(collector))
317 ;
318 }
319
320 TEST_CASE("test_collector_connection_transport") {
321 pn_collector_t *collector = pn_collector();
322 REQUIRE(pn_refcount(collector) == 1);
323 pn_connection_t *connection = pn_connection();
324 REQUIRE(pn_refcount(connection) == 1);
325 pn_connection_collect(connection, collector);
326 REQUIRE(pn_refcount(collector) == 2);
327 REQUIRE(pn_refcount(connection) == 2);
328 drain(collector);
329 REQUIRE(pn_refcount(connection) == 1);
330 pn_transport_t *transport = pn_transport();
331 REQUIRE(pn_refcount(transport) == 1);
332 pn_transport_bind(transport, connection);
333 REQUIRE(pn_refcount(transport) == 1);
334 REQUIRE(pn_refcount(connection) == 3);
335 drain(collector);
336 REQUIRE(pn_refcount(connection) == 2);
337 pn_decref(transport);
338 REQUIRE(pn_refcount(transport) == 1); // preserved by the bind
339 REQUIRE(pn_refcount(connection) == 1);
340 pn_free(connection);
341 REQUIRE(pn_refcount(transport) == 1); // events
342 REQUIRE(pn_refcount(connection) == 1); // events
343 pn_collector_free(collector);
344 }
345
346 TEST_CASE("test_collector_transport_connection") {
347 pn_collector_t *collector = pn_collector();
348 REQUIRE(pn_refcount(collector) == 1);
349 pn_transport_t *transport = pn_transport();
350 REQUIRE(pn_refcount(transport) == 1);
351 pn_connection_t *connection = pn_connection();
352 REQUIRE(pn_refcount(connection) == 1);
353 pn_connection_collect(connection, collector);
354 REQUIRE(pn_refcount(collector) == 2);
355 REQUIRE(pn_refcount(connection) == 2);
356 drain(collector);
357 REQUIRE(pn_refcount(connection) == 1);
358 pn_transport_bind(transport, connection);
359 REQUIRE(pn_refcount(connection) == 3);
360 REQUIRE(pn_refcount(transport) == 1);
361 drain(collector);
362 REQUIRE(pn_refcount(connection) == 2);
363 REQUIRE(pn_refcount(transport) == 1);
364 pn_decref(connection);
365 REQUIRE(pn_refcount(connection) == 1);
366 REQUIRE(pn_refcount(transport) == 1);
367 pn_free(transport);
368 REQUIRE(pn_refcount(connection) == 1);
369 REQUIRE(pn_refcount(transport) == 1);
370 pn_collector_free(collector);
371 }