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_iterator it;
113 struct ao2_iterator *mult_it;
114 struct test_obj *obj;
115 struct test_obj tmp_obj;
118 int destructor_count = 0;
120 int res = AST_TEST_PASS;
122 /* This test needs at least 5 objects */
127 bucket_size = (ast_random() % ((lim / 4) + 1)) + 1;
128 c1 = ao2_t_container_alloc(bucket_size, use_hash ? test_hash_cb : NULL, use_cmp ? test_cmp_cb : NULL, "test");
129 c2 = ao2_t_container_alloc(bucket_size, test_hash_cb, test_cmp_cb, "test");
132 ast_test_status_update(test, "ao2_container_alloc failed.\n");
137 /* Create objects and link into container */
138 destructor_count = lim;
139 for (num = 1; num <= lim; num++) {
140 if (!(obj = ao2_t_alloc(sizeof(struct test_obj), test_obj_destructor, "making zombies"))) {
141 ast_test_status_update(test, "ao2_alloc failed.\n");
145 obj->destructor_count = &destructor_count;
148 ao2_t_ref(obj, -1, "test");
149 if (ao2_container_count(c1) != num) {
150 ast_test_status_update(test, "container did not link correctly\n");
155 ast_test_status_update(test, "Container created: random bucket size %d: number of items: %d\n", bucket_size, lim);
157 /* Testing ao2_find with no flags */
160 int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
162 if (!(obj = ao2_find(c1, &tmp_obj, 0))) {
164 ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with no flags failed.\n", i);
166 /* a correct match will only take place when the custom cmp function is used */
167 if (use_cmp && obj->i != i) {
168 ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
171 ao2_t_ref(obj, -1, "test");
175 /* Testing ao2_find with OBJ_POINTER */
178 int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
180 if (!(obj = ao2_find(c1, &tmp_obj, OBJ_POINTER))) {
182 ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_POINTER flag failed.\n", i);
184 /* a correct match will only take place when the custom cmp function is used */
185 if (use_cmp && obj->i != i) {
186 ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
189 ao2_t_ref(obj, -1, "test");
193 /* Testing ao2_find with OBJ_KEY */
196 int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
197 if (!(obj = ao2_find(c1, &i, OBJ_KEY))) {
199 ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_KEY flag failed.\n", i);
201 /* a correct match will only take place when the custom cmp function is used */
202 if (use_cmp && obj->i != i) {
203 ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
206 ao2_t_ref(obj, -1, "test");
210 /* Testing ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE.
211 * In this test items are unlinked from c1 and placed in c2. Then
212 * unlinked from c2 and placed back into c1.
214 * For this module and set of custom hash/cmp functions, an object
215 * should only be found if the astobj2 default cmp function is used.
216 * This test is designed to mimic the chan_iax.c call number use case. */
217 num = lim < 25 ? lim : 25;
219 if (!(obj = ao2_find(c1, NULL, OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE))) {
221 ast_test_status_update(test, "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with default hash function.\n");
226 ast_test_status_update(test, "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with custom hash function.\n");
230 ao2_t_ref(obj, -1, "test");
233 it = ao2_iterator_init(c2, 0);
234 while ((obj = ao2_t_iterator_next(&it, "test"))) {
235 ao2_t_unlink(c2, obj, "test");
236 ao2_t_link(c1, obj, "test");
237 ao2_t_ref(obj, -1, "test");
239 ao2_iterator_destroy(&it);
241 /* Test Callback with no flags. */
243 ao2_t_callback(c1, 0, increment_cb, &increment, "test callback");
244 if (increment != lim) {
245 ast_test_status_update(test, "callback with no flags failed. Increment is %d\n", increment);
249 /* Test Callback with OBJ_NODATA. This should do nothing different than with no flags here. */
251 ao2_t_callback(c1, OBJ_NODATA, increment_cb, &increment, "test callback");
252 if (increment != lim) {
253 ast_test_status_update(test, "callback with OBJ_NODATA failed. Increment is %d\n", increment);
257 /* Test OBJ_MULTIPLE with OBJ_UNLINK*/
258 num = lim < 25 ? lim : 25;
259 if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE | OBJ_UNLINK, multiple_cb, &num, "test multiple"))) {
260 ast_test_status_update(test, "OBJ_MULTIPLE iwth OBJ_UNLINK test failed.\n");
263 /* make sure num items unlinked is as expected */
264 if ((lim - ao2_container_count(c1)) != num) {
265 ast_test_status_update(test, "OBJ_MULTIPLE | OBJ_UNLINK test failed, did not unlink correct number of objects.\n");
269 /* link what was unlinked back into c1 */
270 while ((obj = ao2_t_iterator_next(mult_it, "test"))) {
271 ao2_t_link(c1, obj, "test");
272 ao2_t_ref(obj, -1, "test"); /* remove ref from iterator */
274 ao2_iterator_destroy(mult_it);
277 /* Test OBJ_MULTIPLE without unlink, add items back afterwards */
278 num = lim < 25 ? lim : 25;
279 if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE, multiple_cb, &num, "test multiple"))) {
280 ast_test_status_update(test, "OBJ_MULTIPLE without OBJ_UNLINK test failed.\n");
283 while ((obj = ao2_t_iterator_next(mult_it, "test"))) {
284 ao2_t_ref(obj, -1, "test"); /* remove ref from iterator */
286 ao2_iterator_destroy(mult_it);
289 /* Test OBJ_MULTIPLE without unlink and no iterating */
290 num = lim < 5 ? lim : 5;
291 if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE, multiple_cb, &num, "test multiple"))) {
292 ast_test_status_update(test, "OBJ_MULTIPLE with no OBJ_UNLINK and no iterating failed.\n");
295 ao2_iterator_destroy(mult_it);
298 /* Is the container count what we expect after all the finds and unlinks?*/
299 if (ao2_container_count(c1) != lim) {
300 ast_test_status_update(test, "container count does not match what is expected after ao2_find tests.\n");
304 /* Testing iterator. Unlink a single object and break. do not add item back */
305 it = ao2_iterator_init(c1, 0);
307 while ((obj = ao2_t_iterator_next(&it, "test"))) {
309 ao2_t_ref(obj, -1, "test");
310 ao2_t_unlink(c1, obj, "test");
313 ao2_t_ref(obj, -1, "test");
315 ao2_iterator_destroy(&it);
317 /* Is the container count what we expect after removing a single item? */
318 if (ao2_container_count(c1) != (lim - 1)) {
319 ast_test_status_update(test, "unlink during iterator failed. Number %d was not removed.\n", num);
323 /* Test unlink all with OBJ_MULTIPLE, leave a single object for the container to destroy */
324 ao2_t_callback(c1, OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA, all_but_one_cb, NULL, "test multiple");
325 /* check to make sure all test_obj destructors were called except for 1 */
326 if (destructor_count != 1) {
327 ast_test_status_update(test, "OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA failed. destructor count %d\n", destructor_count);
332 /* destroy containers */
334 ao2_t_ref(c1, -1, "bye c1");
337 ao2_t_ref(c2, -1, "bye c2");
340 if (destructor_count > 0) {
341 ast_test_status_update(test, "all destructors were not called, destructor count is %d\n", destructor_count);
343 } else if (destructor_count < 0) {
344 ast_test_status_update(test, "Destructor was called too many times, destructor count is %d\n", destructor_count);
351 AST_TEST_DEFINE(astobj2_test_1)
353 int res = AST_TEST_PASS;
357 info->name = "astobj2_test1";
358 info->category = "/main/astobj2/";
359 info->summary = "astobj2 test using ao2 objects, containers, callbacks, and iterators";
361 "Builds ao2_containers with various item numbers, bucket sizes, cmp and hash "
362 "functions. Runs a series of tests to manipulate the container using callbacks "
363 "and iterators. Verifies expected behavior.";
364 return AST_TEST_NOT_RUN;
370 /* Test 1, 500 items with custom hash and cmp functions */
371 ast_test_status_update(test, "Test 1, astobj2 test with 500 items.\n");
372 if ((res = astobj2_test_helper(1, 1, 500, test)) == AST_TEST_FAIL) {
376 /* Test 2, 1000 items with custom hash and default cmp functions */
377 ast_test_status_update(test, "Test 2, astobj2 test with 1000 items.\n");
378 if ((res = astobj2_test_helper(1, 0, 1000, test)) == AST_TEST_FAIL) {
382 /* Test 3, 10000 items with default hash and custom cmp functions */
383 ast_test_status_update(test, "Test 3, astobj2 test with 10000 items.\n");
384 if ((res = astobj2_test_helper(0, 1, 10000, test)) == AST_TEST_FAIL) {
388 /* Test 4, 100000 items with default hash and cmp functions */
389 ast_test_status_update(test, "Test 4, astobj2 test with 100000 items.\n");
390 if ((res = astobj2_test_helper(0, 0, 100000, test)) == AST_TEST_FAIL) {
397 AST_TEST_DEFINE(astobj2_test_2)
399 int res = AST_TEST_PASS;
400 struct ao2_container *c;
401 struct ao2_iterator i;
402 struct test_obj *obj;
404 static const int NUM_OBJS = 5;
405 int destructor_count = NUM_OBJS;
406 struct test_obj tmp_obj = { 0, };
410 info->name = "astobj2_test2";
411 info->category = "/main/astobj2/";
412 info->summary = "Test a certain scenario using ao2 iterators";
414 "This test is aimed at testing for a specific regression that occurred. "
415 "Add some objects into a container. Mix finds and iteration and make "
416 "sure that the iterator still sees all objects.";
417 return AST_TEST_NOT_RUN;
422 c = ao2_container_alloc(1, NULL, test_cmp_cb);
424 ast_test_status_update(test, "ao2_container_alloc failed.\n");
429 for (num = 1; num <= NUM_OBJS; num++) {
430 if (!(obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor))) {
431 ast_test_status_update(test, "ao2_alloc failed.\n");
435 obj->destructor_count = &destructor_count;
439 if (ao2_container_count(c) != num) {
440 ast_test_status_update(test, "container did not link correctly\n");
446 * Iteration take 1. Just make sure we see all NUM_OBJS objects.
449 i = ao2_iterator_init(c, 0);
450 while ((obj = ao2_iterator_next(&i))) {
454 ao2_iterator_destroy(&i);
456 if (num != NUM_OBJS) {
457 ast_test_status_update(test, "iterate take 1, expected '%d', only saw '%d' objects\n",
463 * Iteration take 2. Do a find for the last object, then iterate and make
464 * sure we find all NUM_OBJS objects.
466 tmp_obj.i = NUM_OBJS;
467 obj = ao2_find(c, &tmp_obj, OBJ_POINTER);
469 ast_test_status_update(test, "ao2_find() failed.\n");
476 i = ao2_iterator_init(c, 0);
477 while ((obj = ao2_iterator_next(&i))) {
481 ao2_iterator_destroy(&i);
483 if (num != NUM_OBJS) {
484 ast_test_status_update(test, "iterate take 2, expected '%d', only saw '%d' objects\n",
490 * Iteration take 3. Do a find for an object while in the middle
494 i = ao2_iterator_init(c, 0);
495 while ((obj = ao2_iterator_next(&i))) {
497 struct test_obj *obj2;
498 tmp_obj.i = NUM_OBJS - 1;
499 obj2 = ao2_find(c, &tmp_obj, OBJ_POINTER);
501 ast_test_status_update(test, "ao2_find() failed.\n");
510 ao2_iterator_destroy(&i);
512 if (num != NUM_OBJS) {
513 ast_test_status_update(test, "iterate take 3, expected '%d', only saw '%d' objects\n",
527 static int unload_module(void)
529 AST_TEST_UNREGISTER(astobj2_test_1);
530 AST_TEST_UNREGISTER(astobj2_test_2);
534 static int load_module(void)
536 AST_TEST_REGISTER(astobj2_test_1);
537 AST_TEST_REGISTER(astobj2_test_2);
538 return AST_MODULE_LOAD_SUCCESS;
541 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ASTOBJ2 Unit Tests");