2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2010, Digium, Inc.
6 * David Vossel <dvossel@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief astobj2 test module
23 * \author David Vossel <dvossel@digium.com>
27 <depend>TEST_FRAMEWORK</depend>
28 <support_level>core</support_level>
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include "asterisk/utils.h"
36 #include "asterisk/module.h"
37 #include "asterisk/test.h"
38 #include "asterisk/astobj2.h"
42 int *destructor_count;
45 static void test_obj_destructor(void *obj)
47 struct test_obj *test_obj = (struct test_obj *) obj;
48 *test_obj->destructor_count = *test_obj->destructor_count - 1;
51 static int increment_cb(void *obj, void *arg, int flag)
59 static int all_but_one_cb(void *obj, void *arg, int flag)
61 struct test_obj *test_obj = (struct test_obj *) obj;
63 return (test_obj->i > 1) ? CMP_MATCH : 0;
66 static int multiple_cb(void *obj, void *arg, int flag)
69 struct test_obj *test_obj = (struct test_obj *) obj;
71 return (test_obj->i <= *i) ? CMP_MATCH : 0;
74 static int test_cmp_cb(void *obj, void *arg, int flags)
76 struct test_obj *cmp_obj = (struct test_obj *) obj;
82 if (flags & OBJ_KEY) {
84 return (cmp_obj->i == *i) ? CMP_MATCH | CMP_STOP : 0;
86 struct test_obj *test_obj = (struct test_obj *) arg;
87 return (cmp_obj->i == test_obj->i) ? CMP_MATCH | CMP_STOP : 0;
91 static int test_hash_cb(const void *obj, const int flags)
97 if (flags & OBJ_KEY) {
102 const struct test_obj *test_obj = obj;
108 static int astobj2_test_helper(int use_hash, int use_cmp, unsigned int lim, struct ast_test *test)
110 struct ao2_container *c1;
111 struct ao2_container *c2;
112 struct ao2_container *c3 = NULL;
113 struct ao2_iterator it;
114 struct ao2_iterator *mult_it;
115 struct test_obj *obj;
116 struct test_obj *obj2;
117 struct test_obj tmp_obj;
120 int destructor_count = 0;
122 int res = AST_TEST_PASS;
124 /* This test needs at least 5 objects */
129 bucket_size = (ast_random() % ((lim / 4) + 1)) + 1;
130 c1 = ao2_t_container_alloc(bucket_size, use_hash ? test_hash_cb : NULL, use_cmp ? test_cmp_cb : NULL, "test");
131 c2 = ao2_t_container_alloc(bucket_size, test_hash_cb, test_cmp_cb, "test");
134 ast_test_status_update(test, "ao2_container_alloc failed.\n");
139 /* Create objects and link into container */
140 destructor_count = lim;
141 for (num = 1; num <= lim; num++) {
142 if (!(obj = ao2_t_alloc(sizeof(struct test_obj), test_obj_destructor, "making zombies"))) {
143 ast_test_status_update(test, "ao2_alloc failed.\n");
147 obj->destructor_count = &destructor_count;
150 ao2_t_ref(obj, -1, "test");
151 if (ao2_container_count(c1) != num) {
152 ast_test_status_update(test, "container did not link correctly\n");
157 ast_test_status_update(test, "Container created: random bucket size %d: number of items: %d\n", bucket_size, lim);
159 /* Testing ao2_container_clone */
160 c3 = ao2_container_clone(c1, 0);
162 ast_test_status_update(test, "ao2_container_clone failed.\n");
166 if (ao2_container_count(c1) != ao2_container_count(c3)) {
167 ast_test_status_update(test, "Cloned container does not have the same number of objects.\n");
170 it = ao2_iterator_init(c1, 0);
171 for (; (obj = ao2_t_iterator_next(&it, "test orig")); ao2_t_ref(obj, -1, "test orig")) {
173 * Unlink the matching object from the cloned container to make
174 * the next search faster. This is a big speed optimization!
175 * It reduces the container with 100000 objects test time from
176 * 18 seconds to 200 ms.
178 obj2 = ao2_t_callback(c3, OBJ_POINTER | OBJ_UNLINK, ao2_match_by_addr, obj,
181 ao2_t_ref(obj2, -1, "test clone");
184 ast_test_status_update(test,
185 "Orig container has an object %p not in the clone container.\n", obj);
188 ao2_iterator_destroy(&it);
189 if (ao2_container_count(c3)) {
190 ast_test_status_update(test, "Cloned container still has objects.\n");
194 ao2_t_ref(c3, -1, "bye c3");
197 /* Testing ao2_find with no flags */
200 int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
202 if (!(obj = ao2_find(c1, &tmp_obj, 0))) {
204 ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with no flags failed.\n", i);
206 /* a correct match will only take place when the custom cmp function is used */
207 if (use_cmp && obj->i != i) {
208 ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
211 ao2_t_ref(obj, -1, "test");
215 /* Testing ao2_find with OBJ_POINTER */
218 int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
220 if (!(obj = ao2_find(c1, &tmp_obj, OBJ_POINTER))) {
222 ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_POINTER flag failed.\n", i);
224 /* a correct match will only take place when the custom cmp function is used */
225 if (use_cmp && obj->i != i) {
226 ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
229 ao2_t_ref(obj, -1, "test");
233 /* Testing ao2_find with OBJ_KEY */
236 int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
237 if (!(obj = ao2_find(c1, &i, OBJ_KEY))) {
239 ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_KEY flag failed.\n", i);
241 /* a correct match will only take place when the custom cmp function is used */
242 if (use_cmp && obj->i != i) {
243 ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
246 ao2_t_ref(obj, -1, "test");
250 /* Testing ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE.
251 * In this test items are unlinked from c1 and placed in c2. Then
252 * unlinked from c2 and placed back into c1.
254 * For this module and set of custom hash/cmp functions, an object
255 * should only be found if the astobj2 default cmp function is used.
256 * This test is designed to mimic the chan_iax.c call number use case. */
257 num = lim < 25 ? lim : 25;
259 if (!(obj = ao2_find(c1, NULL, OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE))) {
261 ast_test_status_update(test, "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with default hash function.\n");
266 ast_test_status_update(test, "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with custom hash function.\n");
270 ao2_t_ref(obj, -1, "test");
273 it = ao2_iterator_init(c2, 0);
274 while ((obj = ao2_t_iterator_next(&it, "test"))) {
275 ao2_t_unlink(c2, obj, "test");
276 ao2_t_link(c1, obj, "test");
277 ao2_t_ref(obj, -1, "test");
279 ao2_iterator_destroy(&it);
281 /* Test Callback with no flags. */
283 ao2_t_callback(c1, 0, increment_cb, &increment, "test callback");
284 if (increment != lim) {
285 ast_test_status_update(test, "callback with no flags failed. Increment is %d\n", increment);
289 /* Test Callback with OBJ_NODATA. This should do nothing different than with no flags here. */
291 ao2_t_callback(c1, OBJ_NODATA, increment_cb, &increment, "test callback");
292 if (increment != lim) {
293 ast_test_status_update(test, "callback with OBJ_NODATA failed. Increment is %d\n", increment);
297 /* Test OBJ_MULTIPLE with OBJ_UNLINK*/
298 num = lim < 25 ? lim : 25;
299 if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE | OBJ_UNLINK, multiple_cb, &num, "test multiple"))) {
300 ast_test_status_update(test, "OBJ_MULTIPLE iwth OBJ_UNLINK test failed.\n");
303 /* make sure num items unlinked is as expected */
304 if ((lim - ao2_container_count(c1)) != num) {
305 ast_test_status_update(test, "OBJ_MULTIPLE | OBJ_UNLINK test failed, did not unlink correct number of objects.\n");
309 /* link what was unlinked back into c1 */
310 while ((obj = ao2_t_iterator_next(mult_it, "test"))) {
311 ao2_t_link(c1, obj, "test");
312 ao2_t_ref(obj, -1, "test"); /* remove ref from iterator */
314 ao2_iterator_destroy(mult_it);
317 /* Test OBJ_MULTIPLE without unlink, add items back afterwards */
318 num = lim < 25 ? lim : 25;
319 if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE, multiple_cb, &num, "test multiple"))) {
320 ast_test_status_update(test, "OBJ_MULTIPLE without OBJ_UNLINK test failed.\n");
323 while ((obj = ao2_t_iterator_next(mult_it, "test"))) {
324 ao2_t_ref(obj, -1, "test"); /* remove ref from iterator */
326 ao2_iterator_destroy(mult_it);
329 /* Test OBJ_MULTIPLE without unlink and no iterating */
330 num = lim < 5 ? lim : 5;
331 if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE, multiple_cb, &num, "test multiple"))) {
332 ast_test_status_update(test, "OBJ_MULTIPLE with no OBJ_UNLINK and no iterating failed.\n");
335 ao2_iterator_destroy(mult_it);
338 /* Is the container count what we expect after all the finds and unlinks?*/
339 if (ao2_container_count(c1) != lim) {
340 ast_test_status_update(test, "container count does not match what is expected after ao2_find tests.\n");
344 /* Testing iterator. Unlink a single object and break. do not add item back */
345 it = ao2_iterator_init(c1, 0);
347 while ((obj = ao2_t_iterator_next(&it, "test"))) {
349 ao2_t_ref(obj, -1, "test");
350 ao2_t_unlink(c1, obj, "test");
353 ao2_t_ref(obj, -1, "test");
355 ao2_iterator_destroy(&it);
357 /* Is the container count what we expect after removing a single item? */
358 if (ao2_container_count(c1) != (lim - 1)) {
359 ast_test_status_update(test, "unlink during iterator failed. Number %d was not removed.\n", num);
363 /* Test unlink all with OBJ_MULTIPLE, leave a single object for the container to destroy */
364 ao2_t_callback(c1, OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA, all_but_one_cb, NULL, "test multiple");
365 /* check to make sure all test_obj destructors were called except for 1 */
366 if (destructor_count != 1) {
367 ast_test_status_update(test, "OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA failed. destructor count %d\n", destructor_count);
372 /* destroy containers */
374 ao2_t_ref(c1, -1, "bye c1");
377 ao2_t_ref(c2, -1, "bye c2");
380 ao2_t_ref(c3, -1, "bye c3");
383 if (destructor_count > 0) {
384 ast_test_status_update(test, "all destructors were not called, destructor count is %d\n", destructor_count);
386 } else if (destructor_count < 0) {
387 ast_test_status_update(test, "Destructor was called too many times, destructor count is %d\n", destructor_count);
394 AST_TEST_DEFINE(astobj2_test_1)
396 int res = AST_TEST_PASS;
400 info->name = "astobj2_test1";
401 info->category = "/main/astobj2/";
402 info->summary = "astobj2 test using ao2 objects, containers, callbacks, and iterators";
404 "Builds ao2_containers with various item numbers, bucket sizes, cmp and hash "
405 "functions. Runs a series of tests to manipulate the container using callbacks "
406 "and iterators. Verifies expected behavior.";
407 return AST_TEST_NOT_RUN;
413 /* Test 1, 500 items with custom hash and cmp functions */
414 ast_test_status_update(test, "Test 1, astobj2 test with 500 items.\n");
415 if ((res = astobj2_test_helper(1, 1, 500, test)) == AST_TEST_FAIL) {
419 /* Test 2, 1000 items with custom hash and default cmp functions */
420 ast_test_status_update(test, "Test 2, astobj2 test with 1000 items.\n");
421 if ((res = astobj2_test_helper(1, 0, 1000, test)) == AST_TEST_FAIL) {
425 /* Test 3, 10000 items with default hash and custom cmp functions */
426 ast_test_status_update(test, "Test 3, astobj2 test with 10000 items.\n");
427 if ((res = astobj2_test_helper(0, 1, 10000, test)) == AST_TEST_FAIL) {
431 /* Test 4, 100000 items with default hash and cmp functions */
432 ast_test_status_update(test, "Test 4, astobj2 test with 100000 items.\n");
433 if ((res = astobj2_test_helper(0, 0, 100000, test)) == AST_TEST_FAIL) {
440 AST_TEST_DEFINE(astobj2_test_2)
442 int res = AST_TEST_PASS;
443 struct ao2_container *c;
444 struct ao2_iterator i;
445 struct test_obj *obj;
447 static const int NUM_OBJS = 5;
448 int destructor_count = NUM_OBJS;
449 struct test_obj tmp_obj = { 0, };
453 info->name = "astobj2_test2";
454 info->category = "/main/astobj2/";
455 info->summary = "Test a certain scenario using ao2 iterators";
457 "This test is aimed at testing for a specific regression that occurred. "
458 "Add some objects into a container. Mix finds and iteration and make "
459 "sure that the iterator still sees all objects.";
460 return AST_TEST_NOT_RUN;
465 c = ao2_container_alloc(1, NULL, test_cmp_cb);
467 ast_test_status_update(test, "ao2_container_alloc failed.\n");
472 for (num = 1; num <= NUM_OBJS; num++) {
473 if (!(obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor))) {
474 ast_test_status_update(test, "ao2_alloc failed.\n");
478 obj->destructor_count = &destructor_count;
482 if (ao2_container_count(c) != num) {
483 ast_test_status_update(test, "container did not link correctly\n");
489 * Iteration take 1. Just make sure we see all NUM_OBJS objects.
492 i = ao2_iterator_init(c, 0);
493 while ((obj = ao2_iterator_next(&i))) {
497 ao2_iterator_destroy(&i);
499 if (num != NUM_OBJS) {
500 ast_test_status_update(test, "iterate take 1, expected '%d', only saw '%d' objects\n",
506 * Iteration take 2. Do a find for the last object, then iterate and make
507 * sure we find all NUM_OBJS objects.
509 tmp_obj.i = NUM_OBJS;
510 obj = ao2_find(c, &tmp_obj, OBJ_POINTER);
512 ast_test_status_update(test, "ao2_find() failed.\n");
519 i = ao2_iterator_init(c, 0);
520 while ((obj = ao2_iterator_next(&i))) {
524 ao2_iterator_destroy(&i);
526 if (num != NUM_OBJS) {
527 ast_test_status_update(test, "iterate take 2, expected '%d', only saw '%d' objects\n",
533 * Iteration take 3. Do a find for an object while in the middle
537 i = ao2_iterator_init(c, 0);
538 while ((obj = ao2_iterator_next(&i))) {
540 struct test_obj *obj2;
541 tmp_obj.i = NUM_OBJS - 1;
542 obj2 = ao2_find(c, &tmp_obj, OBJ_POINTER);
544 ast_test_status_update(test, "ao2_find() failed.\n");
553 ao2_iterator_destroy(&i);
555 if (num != NUM_OBJS) {
556 ast_test_status_update(test, "iterate take 3, expected '%d', only saw '%d' objects\n",
570 static int unload_module(void)
572 AST_TEST_UNREGISTER(astobj2_test_1);
573 AST_TEST_UNREGISTER(astobj2_test_2);
577 static int load_module(void)
579 AST_TEST_REGISTER(astobj2_test_1);
580 AST_TEST_REGISTER(astobj2_test_2);
581 return AST_MODULE_LOAD_SUCCESS;
584 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ASTOBJ2 Unit Tests");