test_astobj2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. * \file
  20. * \brief astobj2 test module
  21. *
  22. * \author David Vossel <dvossel@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <depend>TEST_FRAMEWORK</depend>
  26. <support_level>extended</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/utils.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/test.h"
  33. #include "asterisk/astobj2.h"
  34. struct test_obj {
  35. char c[20];
  36. int i;
  37. int *destructor_count;
  38. };
  39. static void test_obj_destructor(void *obj)
  40. {
  41. struct test_obj *test_obj = (struct test_obj *) obj;
  42. *test_obj->destructor_count = *test_obj->destructor_count - 1;
  43. }
  44. static int increment_cb(void *obj, void *arg, int flag)
  45. {
  46. int *i = (int *) arg;
  47. *i = *i + 1;
  48. return 0;
  49. }
  50. static int all_but_one_cb(void *obj, void *arg, int flag)
  51. {
  52. struct test_obj *test_obj = (struct test_obj *) obj;
  53. return (test_obj->i > 1) ? CMP_MATCH : 0;
  54. }
  55. static int multiple_cb(void *obj, void *arg, int flag)
  56. {
  57. int *i = (int *) arg;
  58. struct test_obj *test_obj = (struct test_obj *) obj;
  59. return (test_obj->i <= *i) ? CMP_MATCH : 0;
  60. }
  61. static int test_cmp_cb(void *obj, void *arg, int flags)
  62. {
  63. struct test_obj *cmp_obj = (struct test_obj *) obj;
  64. struct test_obj *test_obj = (struct test_obj *) arg;
  65. if (!arg) {
  66. return 0;
  67. }
  68. return (cmp_obj->i == test_obj->i) ? CMP_MATCH | CMP_STOP : 0;
  69. }
  70. static int test_hash_cb(const void *obj, const int flags)
  71. {
  72. struct test_obj *test_obj = (struct test_obj *) obj;
  73. if (!test_obj || ast_strlen_zero(test_obj->c)) {
  74. return 0;
  75. }
  76. return ast_str_hash(test_obj->c);
  77. }
  78. static int astobj2_test_helper(int use_hash, int use_cmp, unsigned int lim, struct ast_test *test)
  79. {
  80. struct ao2_container *c1;
  81. struct ao2_container *c2;
  82. struct ao2_iterator it;
  83. struct ao2_iterator *mult_it;
  84. struct test_obj *obj;
  85. struct test_obj tmp_obj;
  86. int bucket_size;
  87. int increment = 0;
  88. int destructor_count = 0;
  89. int num;
  90. int res = AST_TEST_PASS;
  91. /* This test needs at least 5 objects */
  92. if (lim < 5) {
  93. lim = 5;
  94. }
  95. bucket_size = (ast_random() % ((lim / 4) + 1)) + 1;
  96. c1 = ao2_t_container_alloc(bucket_size, use_hash ? test_hash_cb : NULL, use_cmp ? test_cmp_cb : NULL, "test");
  97. c2 = ao2_t_container_alloc(bucket_size, test_hash_cb, test_cmp_cb, "test");
  98. if (!c1 || !c2) {
  99. ast_test_status_update(test, "ao2_container_alloc failed.\n");
  100. res = AST_TEST_FAIL;
  101. goto cleanup;
  102. }
  103. /* Create objects and link into container */
  104. destructor_count = lim;
  105. for (num = 1; num <= lim; num++) {
  106. if (!(obj = ao2_t_alloc(sizeof(struct test_obj), test_obj_destructor, "making zombies"))) {
  107. ast_test_status_update(test, "ao2_alloc failed.\n");
  108. res = AST_TEST_FAIL;
  109. goto cleanup;
  110. }
  111. snprintf(obj->c, sizeof(obj->c), "zombie #%d", num);
  112. obj->destructor_count = &destructor_count;
  113. obj->i = num;
  114. ao2_link(c1, obj);
  115. ao2_t_ref(obj, -1, "test");
  116. if (ao2_container_count(c1) != num) {
  117. ast_test_status_update(test, "container did not link correctly\n");
  118. res = AST_TEST_FAIL;
  119. }
  120. }
  121. ast_test_status_update(test, "Container created: random bucket size %d: number of items: %d\n", bucket_size, lim);
  122. /* Testing ao2_find with no flags */
  123. num = 100;
  124. for (; num; num--) {
  125. int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
  126. tmp_obj.i = i;
  127. if (!(obj = ao2_find(c1, &tmp_obj, 0))) {
  128. res = AST_TEST_FAIL;
  129. ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with no flags failed.\n", i);
  130. } else {
  131. /* a correct match will only take place when the custom cmp function is used */
  132. if (use_cmp && obj->i != i) {
  133. ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
  134. res = AST_TEST_FAIL;
  135. }
  136. ao2_t_ref(obj, -1, "test");
  137. }
  138. }
  139. /* Testing ao2_find with OBJ_POINTER */
  140. num = 75;
  141. for (; num; num--) {
  142. int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
  143. snprintf(tmp_obj.c, sizeof(tmp_obj.c), "zombie #%d", i);
  144. tmp_obj.i = i;
  145. if (!(obj = ao2_find(c1, &tmp_obj, OBJ_POINTER))) {
  146. res = AST_TEST_FAIL;
  147. ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_POINTER flag failed.\n", i);
  148. } else {
  149. /* a correct match will only take place when the custom cmp function is used */
  150. if (use_cmp && obj->i != i) {
  151. ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
  152. res = AST_TEST_FAIL;
  153. }
  154. ao2_t_ref(obj, -1, "test");
  155. }
  156. }
  157. /* Testing ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE.
  158. * In this test items are unlinked from c1 and placed in c2. Then
  159. * unlinked from c2 and placed back into c1.
  160. *
  161. * For this module and set of custom hash/cmp functions, an object
  162. * should only be found if the astobj2 default cmp function is used.
  163. * This test is designed to mimic the chan_iax.c call number use case. */
  164. num = lim < 25 ? lim : 25;
  165. for (; num; num--) {
  166. if (!(obj = ao2_find(c1, NULL, OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE))) {
  167. if (!use_cmp) {
  168. ast_test_status_update(test, "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with default hash function.\n");
  169. res = AST_TEST_FAIL;
  170. }
  171. } else {
  172. if (use_cmp) {
  173. ast_test_status_update(test, "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with custom hash function.\n");
  174. res = AST_TEST_FAIL;
  175. }
  176. ao2_link(c2, obj);
  177. ao2_t_ref(obj, -1, "test");
  178. }
  179. }
  180. it = ao2_iterator_init(c2, 0);
  181. while ((obj = ao2_t_iterator_next(&it, "test"))) {
  182. ao2_t_unlink(c2, obj, "test");
  183. ao2_t_link(c1, obj, "test");
  184. ao2_t_ref(obj, -1, "test");
  185. }
  186. ao2_iterator_destroy(&it);
  187. /* Test Callback with no flags. */
  188. increment = 0;
  189. ao2_t_callback(c1, 0, increment_cb, &increment, "test callback");
  190. if (increment != lim) {
  191. ast_test_status_update(test, "callback with no flags failed. Increment is %d\n", increment);
  192. res = AST_TEST_FAIL;
  193. }
  194. /* Test Callback with OBJ_NODATA. This should do nothing different than with no flags here. */
  195. increment = 0;
  196. ao2_t_callback(c1, OBJ_NODATA, increment_cb, &increment, "test callback");
  197. if (increment != lim) {
  198. ast_test_status_update(test, "callback with OBJ_NODATA failed. Increment is %d\n", increment);
  199. res = AST_TEST_FAIL;
  200. }
  201. /* Test OBJ_MULTIPLE with OBJ_UNLINK*/
  202. num = lim < 25 ? lim : 25;
  203. if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE | OBJ_UNLINK, multiple_cb, &num, "test multiple"))) {
  204. ast_test_status_update(test, "OBJ_MULTIPLE iwth OBJ_UNLINK test failed.\n");
  205. res = AST_TEST_FAIL;
  206. } else {
  207. /* make sure num items unlinked is as expected */
  208. if ((lim - ao2_container_count(c1)) != num) {
  209. ast_test_status_update(test, "OBJ_MULTIPLE | OBJ_UNLINK test failed, did not unlink correct number of objects.\n");
  210. res = AST_TEST_FAIL;
  211. }
  212. /* link what was unlinked back into c1 */
  213. while ((obj = ao2_t_iterator_next(mult_it, "test"))) {
  214. ao2_t_link(c1, obj, "test");
  215. ao2_t_ref(obj, -1, "test"); /* remove ref from iterator */
  216. }
  217. ao2_iterator_destroy(mult_it);
  218. }
  219. /* Test OBJ_MULTIPLE without unlink, add items back afterwards */
  220. num = lim < 25 ? lim : 25;
  221. if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE, multiple_cb, &num, "test multiple"))) {
  222. ast_test_status_update(test, "OBJ_MULTIPLE without OBJ_UNLINK test failed.\n");
  223. res = AST_TEST_FAIL;
  224. } else {
  225. while ((obj = ao2_t_iterator_next(mult_it, "test"))) {
  226. ao2_t_ref(obj, -1, "test"); /* remove ref from iterator */
  227. }
  228. ao2_iterator_destroy(mult_it);
  229. }
  230. /* Test OBJ_MULTIPLE without unlink and no iterating */
  231. num = lim < 5 ? lim : 5;
  232. if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE, multiple_cb, &num, "test multiple"))) {
  233. ast_test_status_update(test, "OBJ_MULTIPLE with no OBJ_UNLINK and no iterating failed.\n");
  234. res = AST_TEST_FAIL;
  235. } else {
  236. ao2_iterator_destroy(mult_it);
  237. }
  238. /* Is the container count what we expect after all the finds and unlinks?*/
  239. if (ao2_container_count(c1) != lim) {
  240. ast_test_status_update(test, "container count does not match what is expected after ao2_find tests.\n");
  241. res = AST_TEST_FAIL;
  242. }
  243. /* Testing iterator. Unlink a single object and break. do not add item back */
  244. it = ao2_iterator_init(c1, 0);
  245. num = (lim / 4) + 1;
  246. while ((obj = ao2_t_iterator_next(&it, "test"))) {
  247. if (obj->i == num) {
  248. ao2_t_ref(obj, -1, "test");
  249. ao2_t_unlink(c1, obj, "test");
  250. break;
  251. }
  252. ao2_t_ref(obj, -1, "test");
  253. }
  254. ao2_iterator_destroy(&it);
  255. /* Is the container count what we expect after removing a single item? */
  256. if (ao2_container_count(c1) != (lim - 1)) {
  257. ast_test_status_update(test, "unlink during iterator failed. Number %d was not removed.\n", num);
  258. res = AST_TEST_FAIL;
  259. }
  260. /* Test unlink all with OBJ_MULTIPLE, leave a single object for the container to destroy */
  261. ao2_t_callback(c1, OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA, all_but_one_cb, NULL, "test multiple");
  262. /* check to make sure all test_obj destructors were called except for 1 */
  263. if (destructor_count != 1) {
  264. ast_test_status_update(test, "OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA failed. destructor count %d\n", destructor_count);
  265. res = AST_TEST_FAIL;
  266. }
  267. cleanup:
  268. /* destroy containers */
  269. if (c1) {
  270. ao2_t_ref(c1, -1, "bye c1");
  271. }
  272. if (c2) {
  273. ao2_t_ref(c2, -1, "bye c2");
  274. }
  275. if (destructor_count > 0) {
  276. ast_test_status_update(test, "all destructors were not called, destructor count is %d\n", destructor_count);
  277. res = AST_TEST_FAIL;
  278. } else if (destructor_count < 0) {
  279. ast_test_status_update(test, "Destructor was called too many times, destructor count is %d\n", destructor_count);
  280. res = AST_TEST_FAIL;
  281. }
  282. return res;
  283. }
  284. AST_TEST_DEFINE(astobj2_test_1)
  285. {
  286. int res = AST_TEST_PASS;
  287. switch (cmd) {
  288. case TEST_INIT:
  289. info->name = "astobj2_test1";
  290. info->category = "/main/astobj2/";
  291. info->summary = "astobj2 test using ao2 objects, containers, callbacks, and iterators";
  292. info->description =
  293. "Builds ao2_containers with various item numbers, bucket sizes, cmp and hash "
  294. "functions. Runs a series of tests to manipulate the container using callbacks "
  295. "and iterators. Verifies expected behavior.";
  296. return AST_TEST_NOT_RUN;
  297. case TEST_EXECUTE:
  298. break;
  299. }
  300. /* Test 1, 500 items with custom hash and cmp functions */
  301. ast_test_status_update(test, "Test 1, astobj2 test with 500 items.\n");
  302. if ((res = astobj2_test_helper(1, 1, 500, test)) == AST_TEST_FAIL) {
  303. return res;
  304. }
  305. /* Test 2, 1000 items with custom hash and default cmp functions */
  306. ast_test_status_update(test, "Test 2, astobj2 test with 1000 items.\n");
  307. if ((res = astobj2_test_helper(1, 0, 1000, test)) == AST_TEST_FAIL) {
  308. return res;
  309. }
  310. /* Test 3, 10000 items with default hash and custom cmp functions */
  311. ast_test_status_update(test, "Test 3, astobj2 test with 10000 items.\n");
  312. if ((res = astobj2_test_helper(0, 1, 10000, test)) == AST_TEST_FAIL) {
  313. return res;
  314. }
  315. /* Test 4, 100000 items with default hash and cmp functions */
  316. ast_test_status_update(test, "Test 4, astobj2 test with 100000 items.\n");
  317. if ((res = astobj2_test_helper(0, 0, 100000, test)) == AST_TEST_FAIL) {
  318. return res;
  319. }
  320. return res;
  321. }
  322. AST_TEST_DEFINE(astobj2_test_2)
  323. {
  324. int res = AST_TEST_PASS;
  325. struct ao2_container *c;
  326. struct ao2_iterator i;
  327. struct test_obj *obj;
  328. int num;
  329. static const int NUM_OBJS = 5;
  330. int destructor_count = NUM_OBJS;
  331. struct test_obj tmp_obj = { "", };
  332. switch (cmd) {
  333. case TEST_INIT:
  334. info->name = "astobj2_test2";
  335. info->category = "/main/astobj2/";
  336. info->summary = "Test a certain scenario using ao2 iterators";
  337. info->description =
  338. "This test is aimed at testing for a specific regression that occurred. "
  339. "Add some objects into a container. Mix finds and iteration and make "
  340. "sure that the iterator still sees all objects.";
  341. return AST_TEST_NOT_RUN;
  342. case TEST_EXECUTE:
  343. break;
  344. }
  345. c = ao2_container_alloc(1, NULL, test_cmp_cb);
  346. if (!c) {
  347. ast_test_status_update(test, "ao2_container_alloc failed.\n");
  348. res = AST_TEST_FAIL;
  349. goto cleanup;
  350. }
  351. for (num = 1; num <= NUM_OBJS; num++) {
  352. if (!(obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor))) {
  353. ast_test_status_update(test, "ao2_alloc failed.\n");
  354. res = AST_TEST_FAIL;
  355. goto cleanup;
  356. }
  357. obj->destructor_count = &destructor_count;
  358. obj->i = num;
  359. ao2_link(c, obj);
  360. ao2_ref(obj, -1);
  361. if (ao2_container_count(c) != num) {
  362. ast_test_status_update(test, "container did not link correctly\n");
  363. res = AST_TEST_FAIL;
  364. }
  365. }
  366. /*
  367. * Iteration take 1. Just make sure we see all NUM_OBJS objects.
  368. */
  369. num = 0;
  370. i = ao2_iterator_init(c, 0);
  371. while ((obj = ao2_iterator_next(&i))) {
  372. num++;
  373. ao2_ref(obj, -1);
  374. }
  375. ao2_iterator_destroy(&i);
  376. if (num != NUM_OBJS) {
  377. ast_test_status_update(test, "iterate take 1, expected '%d', only saw '%d' objects\n",
  378. NUM_OBJS, num);
  379. res = AST_TEST_FAIL;
  380. }
  381. /*
  382. * Iteration take 2. Do a find for the last object, then iterate and make
  383. * sure we find all NUM_OBJS objects.
  384. */
  385. tmp_obj.i = NUM_OBJS;
  386. obj = ao2_find(c, &tmp_obj, OBJ_POINTER);
  387. if (!obj) {
  388. ast_test_status_update(test, "ao2_find() failed.\n");
  389. res = AST_TEST_FAIL;
  390. } else {
  391. ao2_ref(obj, -1);
  392. }
  393. num = 0;
  394. i = ao2_iterator_init(c, 0);
  395. while ((obj = ao2_iterator_next(&i))) {
  396. num++;
  397. ao2_ref(obj, -1);
  398. }
  399. ao2_iterator_destroy(&i);
  400. if (num != NUM_OBJS) {
  401. ast_test_status_update(test, "iterate take 2, expected '%d', only saw '%d' objects\n",
  402. NUM_OBJS, num);
  403. res = AST_TEST_FAIL;
  404. }
  405. /*
  406. * Iteration take 3. Do a find for an object while in the middle
  407. * of iterating;
  408. */
  409. num = 0;
  410. i = ao2_iterator_init(c, 0);
  411. while ((obj = ao2_iterator_next(&i))) {
  412. if (num == 1) {
  413. struct test_obj *obj2;
  414. tmp_obj.i = NUM_OBJS - 1;
  415. obj2 = ao2_find(c, &tmp_obj, OBJ_POINTER);
  416. if (!obj2) {
  417. ast_test_status_update(test, "ao2_find() failed.\n");
  418. res = AST_TEST_FAIL;
  419. } else {
  420. ao2_ref(obj2, -1);
  421. }
  422. }
  423. num++;
  424. ao2_ref(obj, -1);
  425. }
  426. ao2_iterator_destroy(&i);
  427. if (num != NUM_OBJS) {
  428. ast_test_status_update(test, "iterate take 3, expected '%d', only saw '%d' objects\n",
  429. NUM_OBJS, num);
  430. res = AST_TEST_FAIL;
  431. }
  432. cleanup:
  433. if (c) {
  434. ao2_ref(c, -1);
  435. }
  436. return res;
  437. }
  438. static int unload_module(void)
  439. {
  440. AST_TEST_UNREGISTER(astobj2_test_1);
  441. AST_TEST_UNREGISTER(astobj2_test_2);
  442. return 0;
  443. }
  444. static int load_module(void)
  445. {
  446. AST_TEST_REGISTER(astobj2_test_1);
  447. AST_TEST_REGISTER(astobj2_test_2);
  448. return AST_MODULE_LOAD_SUCCESS;
  449. }
  450. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ASTOBJ2 Unit Tests");