Add ability to clone ao2 containers.
[asterisk/asterisk.git] / tests / test_astobj2.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * David Vossel <dvossel@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*!
20  * \file
21  * \brief astobj2 test module
22  *
23  * \author David Vossel <dvossel@digium.com>
24  */
25
26 /*** MODULEINFO
27         <depend>TEST_FRAMEWORK</depend>
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/utils.h"
36 #include "asterisk/module.h"
37 #include "asterisk/test.h"
38 #include "asterisk/astobj2.h"
39
40 struct test_obj {
41         int i;
42         int *destructor_count;
43 };
44
45 static void test_obj_destructor(void *obj)
46 {
47         struct test_obj *test_obj = (struct test_obj *) obj;
48         *test_obj->destructor_count = *test_obj->destructor_count - 1;
49 }
50
51 static int increment_cb(void *obj, void *arg, int flag)
52 {
53         int *i = (int *) arg;
54
55         *i = *i + 1;
56         return 0;
57 }
58
59 static int all_but_one_cb(void *obj, void *arg, int flag)
60 {
61         struct test_obj *test_obj = (struct test_obj *) obj;
62
63         return (test_obj->i > 1) ? CMP_MATCH : 0;
64 }
65
66 static int multiple_cb(void *obj, void *arg, int flag)
67 {
68         int *i = (int *) arg;
69         struct test_obj *test_obj = (struct test_obj *) obj;
70
71         return (test_obj->i <= *i) ? CMP_MATCH : 0;
72 }
73
74 static int test_cmp_cb(void *obj, void *arg, int flags)
75 {
76         struct test_obj *cmp_obj = (struct test_obj *) obj;
77
78         if (!arg) {
79                 return 0;
80         }
81
82         if (flags & OBJ_KEY) {
83                 int *i = (int *) arg;
84                 return (cmp_obj->i == *i) ? CMP_MATCH | CMP_STOP : 0;
85         } else {
86                 struct test_obj *test_obj = (struct test_obj *) arg;
87                 return (cmp_obj->i == test_obj->i) ? CMP_MATCH | CMP_STOP : 0;
88         }
89 }
90
91 static int test_hash_cb(const void *obj, const int flags)
92 {
93         if (!obj) {
94                 return 0;
95         }
96
97         if (flags & OBJ_KEY) {
98                 const int *i = obj;
99
100                 return *i;
101         } else {
102                 const struct test_obj *test_obj = obj;
103
104                 return test_obj->i;
105         }
106 }
107
108 static int astobj2_test_helper(int use_hash, int use_cmp, unsigned int lim, struct ast_test *test)
109 {
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;
118         int bucket_size;
119         int increment = 0;
120         int destructor_count = 0;
121         int num;
122         int  res = AST_TEST_PASS;
123
124         /* This test needs at least 5 objects */
125         if (lim < 5) {
126                 lim = 5;
127         }
128
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");
132
133         if (!c1 || !c2) {
134                 ast_test_status_update(test, "ao2_container_alloc failed.\n");
135                 res = AST_TEST_FAIL;
136                 goto cleanup;
137         }
138
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");
144                         res = AST_TEST_FAIL;
145                         goto cleanup;
146                 }
147                 obj->destructor_count = &destructor_count;
148                 obj->i = num;
149                 ao2_link(c1, obj);
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");
153                         res = AST_TEST_FAIL;
154                 }
155         }
156
157         ast_test_status_update(test, "Container created: random bucket size %d: number of items: %d\n", bucket_size, lim);
158
159         /* Testing ao2_container_clone */
160         c3 = ao2_container_clone(c1, 0);
161         if (!c3) {
162                 ast_test_status_update(test, "ao2_container_clone failed.\n");
163                 res = AST_TEST_FAIL;
164                 goto cleanup;
165         }
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");
168                 res = AST_TEST_FAIL;
169         } else {
170                 it = ao2_iterator_init(c1, 0);
171                 for (; (obj = ao2_t_iterator_next(&it, "test orig")); ao2_t_ref(obj, -1, "test orig")) {
172                         /*
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.
177                          */
178                         obj2 = ao2_t_callback(c3, OBJ_POINTER | OBJ_UNLINK, ao2_match_by_addr, obj,
179                                 "test clone");
180                         if (obj2) {
181                                 ao2_t_ref(obj2, -1, "test clone");
182                                 continue;
183                         }
184                         ast_test_status_update(test,
185                                 "Orig container has an object %p not in the clone container.\n", obj);
186                         res = AST_TEST_FAIL;
187                 }
188                 ao2_iterator_destroy(&it);
189                 if (ao2_container_count(c3)) {
190                         ast_test_status_update(test, "Cloned container still has objects.\n");
191                         res = AST_TEST_FAIL;
192                 }
193         }
194         ao2_t_ref(c3, -1, "bye c3");
195         c3 = NULL;
196
197         /* Testing ao2_find with no flags */
198         num = 100;
199         for (; num; num--) {
200                 int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
201                 tmp_obj.i = i;
202                 if (!(obj = ao2_find(c1, &tmp_obj, 0))) {
203                         res = AST_TEST_FAIL;
204                         ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with no flags failed.\n", i);
205                 } else {
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);
209                                 res = AST_TEST_FAIL;
210                         }
211                         ao2_t_ref(obj, -1, "test");
212                 }
213         }
214
215         /* Testing ao2_find with OBJ_POINTER */
216         num = 75;
217         for (; num; num--) {
218                 int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
219                 tmp_obj.i = i;
220                 if (!(obj = ao2_find(c1, &tmp_obj, OBJ_POINTER))) {
221                         res = AST_TEST_FAIL;
222                         ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_POINTER flag failed.\n", i);
223                 } else {
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);
227                                 res = AST_TEST_FAIL;
228                         }
229                         ao2_t_ref(obj, -1, "test");
230                 }
231         }
232
233         /* Testing ao2_find with OBJ_KEY */
234         num = 75;
235         for (; num; num--) {
236                 int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
237                 if (!(obj = ao2_find(c1, &i, OBJ_KEY))) {
238                         res = AST_TEST_FAIL;
239                         ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_KEY flag failed.\n", i);
240                 } else {
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);
244                                 res = AST_TEST_FAIL;
245                         }
246                         ao2_t_ref(obj, -1, "test");
247                 }
248         }
249
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.
253          *
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;
258         for (; num; num--) {
259                 if (!(obj = ao2_find(c1, NULL, OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE))) {
260                         if (!use_cmp) {
261                                 ast_test_status_update(test, "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with default hash function.\n");
262                                 res = AST_TEST_FAIL;
263                         }
264                 } else {
265                         if (use_cmp) {
266                                 ast_test_status_update(test, "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with custom hash function.\n");
267                                 res = AST_TEST_FAIL;
268                         }
269                         ao2_link(c2, obj);
270                         ao2_t_ref(obj, -1, "test");
271                 }
272         }
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");
278         }
279         ao2_iterator_destroy(&it);
280
281         /* Test Callback with no flags. */
282         increment = 0;
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);
286                 res = AST_TEST_FAIL;
287         }
288
289         /* Test Callback with OBJ_NODATA. This should do nothing different than with no flags here. */
290         increment = 0;
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);
294                 res = AST_TEST_FAIL;
295         }
296
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");
301                 res = AST_TEST_FAIL;
302         } else {
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");
306                         res = AST_TEST_FAIL;
307                 }
308
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 */
313                 }
314                 ao2_iterator_destroy(mult_it);
315         }
316
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");
321                 res = AST_TEST_FAIL;
322         } else {
323                 while ((obj = ao2_t_iterator_next(mult_it, "test"))) {
324                         ao2_t_ref(obj, -1, "test"); /* remove ref from iterator */
325                 }
326                 ao2_iterator_destroy(mult_it);
327         }
328
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");
333                 res = AST_TEST_FAIL;
334         } else {
335                 ao2_iterator_destroy(mult_it);
336         }
337
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");
341                 res = AST_TEST_FAIL;
342         }
343
344         /* Testing iterator.  Unlink a single object and break. do not add item back */
345         it = ao2_iterator_init(c1, 0);
346         num = (lim / 4) + 1;
347         while ((obj = ao2_t_iterator_next(&it, "test"))) {
348                 if (obj->i == num) {
349                         ao2_t_ref(obj, -1, "test");
350                         ao2_t_unlink(c1, obj, "test");
351                         break;
352                 }
353                 ao2_t_ref(obj, -1, "test");
354         }
355         ao2_iterator_destroy(&it);
356
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);
360                 res = AST_TEST_FAIL;
361         }
362
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);
368                 res = AST_TEST_FAIL;
369         }
370
371 cleanup:
372         /* destroy containers */
373         if (c1) {
374                 ao2_t_ref(c1, -1, "bye c1");
375         }
376         if (c2) {
377                 ao2_t_ref(c2, -1, "bye c2");
378         }
379         if (c3) {
380                 ao2_t_ref(c3, -1, "bye c3");
381         }
382
383         if (destructor_count > 0) {
384                 ast_test_status_update(test, "all destructors were not called, destructor count is %d\n", destructor_count);
385                 res = AST_TEST_FAIL;
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);
388                 res = AST_TEST_FAIL;
389         }
390
391         return res;
392 }
393
394 AST_TEST_DEFINE(astobj2_test_1)
395 {
396         int res = AST_TEST_PASS;
397
398         switch (cmd) {
399         case TEST_INIT:
400                 info->name = "astobj2_test1";
401                 info->category = "/main/astobj2/";
402                 info->summary = "astobj2 test using ao2 objects, containers, callbacks, and iterators";
403                 info->description =
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;
408         case TEST_EXECUTE:
409                 break;
410         }
411
412
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) {
416                 return res;
417         }
418
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) {
422                 return res;
423         }
424
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) {
428                 return res;
429         }
430
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) {
434                 return res;
435         }
436
437         return res;
438 }
439
440 AST_TEST_DEFINE(astobj2_test_2)
441 {
442         int res = AST_TEST_PASS;
443         struct ao2_container *c;
444         struct ao2_iterator i;
445         struct test_obj *obj;
446         int num;
447         static const int NUM_OBJS = 5;
448         int destructor_count = NUM_OBJS;
449         struct test_obj tmp_obj = { 0, };
450
451         switch (cmd) {
452         case TEST_INIT:
453                 info->name = "astobj2_test2";
454                 info->category = "/main/astobj2/";
455                 info->summary = "Test a certain scenario using ao2 iterators";
456                 info->description =
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;
461         case TEST_EXECUTE:
462                 break;
463         }
464
465         c = ao2_container_alloc(1, NULL, test_cmp_cb);
466         if (!c) {
467                 ast_test_status_update(test, "ao2_container_alloc failed.\n");
468                 res = AST_TEST_FAIL;
469                 goto cleanup;
470         }
471
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");
475                         res = AST_TEST_FAIL;
476                         goto cleanup;
477                 }
478                 obj->destructor_count = &destructor_count;
479                 obj->i = num;
480                 ao2_link(c, obj);
481                 ao2_ref(obj, -1);
482                 if (ao2_container_count(c) != num) {
483                         ast_test_status_update(test, "container did not link correctly\n");
484                         res = AST_TEST_FAIL;
485                 }
486         }
487
488         /*
489          * Iteration take 1.  Just make sure we see all NUM_OBJS objects.
490          */
491         num = 0;
492         i = ao2_iterator_init(c, 0);
493         while ((obj = ao2_iterator_next(&i))) {
494                 num++;
495                 ao2_ref(obj, -1);
496         }
497         ao2_iterator_destroy(&i);
498
499         if (num != NUM_OBJS) {
500                 ast_test_status_update(test, "iterate take 1, expected '%d', only saw '%d' objects\n",
501                                 NUM_OBJS, num);
502                 res = AST_TEST_FAIL;
503         }
504
505         /*
506          * Iteration take 2.  Do a find for the last object, then iterate and make
507          * sure we find all NUM_OBJS objects.
508          */
509         tmp_obj.i = NUM_OBJS;
510         obj = ao2_find(c, &tmp_obj, OBJ_POINTER);
511         if (!obj) {
512                 ast_test_status_update(test, "ao2_find() failed.\n");
513                 res = AST_TEST_FAIL;
514         } else {
515                 ao2_ref(obj, -1);
516         }
517
518         num = 0;
519         i = ao2_iterator_init(c, 0);
520         while ((obj = ao2_iterator_next(&i))) {
521                 num++;
522                 ao2_ref(obj, -1);
523         }
524         ao2_iterator_destroy(&i);
525
526         if (num != NUM_OBJS) {
527                 ast_test_status_update(test, "iterate take 2, expected '%d', only saw '%d' objects\n",
528                                 NUM_OBJS, num);
529                 res = AST_TEST_FAIL;
530         }
531
532         /*
533          * Iteration take 3.  Do a find for an object while in the middle
534          * of iterating;
535          */
536         num = 0;
537         i = ao2_iterator_init(c, 0);
538         while ((obj = ao2_iterator_next(&i))) {
539                 if (num == 1) {
540                         struct test_obj *obj2;
541                         tmp_obj.i = NUM_OBJS - 1;
542                         obj2 = ao2_find(c, &tmp_obj, OBJ_POINTER);
543                         if (!obj2) {
544                                 ast_test_status_update(test, "ao2_find() failed.\n");
545                                 res = AST_TEST_FAIL;
546                         } else {
547                                 ao2_ref(obj2, -1);
548                         }
549                 }
550                 num++;
551                 ao2_ref(obj, -1);
552         }
553         ao2_iterator_destroy(&i);
554
555         if (num != NUM_OBJS) {
556                 ast_test_status_update(test, "iterate take 3, expected '%d', only saw '%d' objects\n",
557                                 NUM_OBJS, num);
558                 res = AST_TEST_FAIL;
559         }
560
561
562 cleanup:
563         if (c) {
564                 ao2_ref(c, -1);
565         }
566
567         return res;
568 }
569
570 static int unload_module(void)
571 {
572         AST_TEST_UNREGISTER(astobj2_test_1);
573         AST_TEST_UNREGISTER(astobj2_test_2);
574         return 0;
575 }
576
577 static int load_module(void)
578 {
579         AST_TEST_REGISTER(astobj2_test_1);
580         AST_TEST_REGISTER(astobj2_test_2);
581         return AST_MODULE_LOAD_SUCCESS;
582 }
583
584 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ASTOBJ2 Unit Tests");