test_astobj2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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>core</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. int i;
  36. int *destructor_count;
  37. };
  38. static void test_obj_destructor(void *obj)
  39. {
  40. struct test_obj *test_obj = (struct test_obj *) obj;
  41. *test_obj->destructor_count = *test_obj->destructor_count - 1;
  42. }
  43. static int increment_cb(void *obj, void *arg, int flag)
  44. {
  45. int *i = (int *) arg;
  46. *i = *i + 1;
  47. return 0;
  48. }
  49. static int all_but_one_cb(void *obj, void *arg, int flag)
  50. {
  51. struct test_obj *test_obj = (struct test_obj *) obj;
  52. return (test_obj->i > 1) ? CMP_MATCH : 0;
  53. }
  54. static int multiple_cb(void *obj, void *arg, int flag)
  55. {
  56. int *i = (int *) arg;
  57. struct test_obj *test_obj = (struct test_obj *) obj;
  58. return (test_obj->i <= *i) ? CMP_MATCH : 0;
  59. }
  60. static int test_cmp_cb(void *obj, void *arg, int flags)
  61. {
  62. struct test_obj *cmp_obj = (struct test_obj *) obj;
  63. if (!arg) {
  64. return 0;
  65. }
  66. if (flags & OBJ_KEY) {
  67. int *i = (int *) arg;
  68. return (cmp_obj->i == *i) ? CMP_MATCH | CMP_STOP : 0;
  69. } else {
  70. struct test_obj *test_obj = (struct test_obj *) arg;
  71. return (cmp_obj->i == test_obj->i) ? CMP_MATCH | CMP_STOP : 0;
  72. }
  73. }
  74. static int test_hash_cb(const void *obj, const int flags)
  75. {
  76. if (!obj) {
  77. return 0;
  78. }
  79. if (flags & OBJ_KEY) {
  80. const int *i = obj;
  81. return *i;
  82. } else {
  83. const struct test_obj *test_obj = obj;
  84. return test_obj->i;
  85. }
  86. }
  87. static int astobj2_test_helper(int use_hash, int use_cmp, unsigned int lim, struct ast_test *test)
  88. {
  89. struct ao2_container *c1;
  90. struct ao2_container *c2;
  91. struct ao2_container *c3 = NULL;
  92. struct ao2_iterator it;
  93. struct ao2_iterator *mult_it;
  94. struct test_obj *obj;
  95. struct test_obj *obj2;
  96. struct test_obj tmp_obj;
  97. int bucket_size;
  98. int increment = 0;
  99. int destructor_count = 0;
  100. int num;
  101. int res = AST_TEST_PASS;
  102. /* This test needs at least 5 objects */
  103. if (lim < 5) {
  104. lim = 5;
  105. }
  106. bucket_size = (ast_random() % ((lim / 4) + 1)) + 1;
  107. c1 = ao2_t_container_alloc(bucket_size, use_hash ? test_hash_cb : NULL, use_cmp ? test_cmp_cb : NULL, "test");
  108. c2 = ao2_t_container_alloc(bucket_size, test_hash_cb, test_cmp_cb, "test");
  109. if (!c1 || !c2) {
  110. ast_test_status_update(test, "ao2_container_alloc failed.\n");
  111. res = AST_TEST_FAIL;
  112. goto cleanup;
  113. }
  114. /* Create objects and link into container */
  115. destructor_count = lim;
  116. for (num = 1; num <= lim; num++) {
  117. if (!(obj = ao2_t_alloc(sizeof(struct test_obj), test_obj_destructor, "making zombies"))) {
  118. ast_test_status_update(test, "ao2_alloc failed.\n");
  119. res = AST_TEST_FAIL;
  120. goto cleanup;
  121. }
  122. obj->destructor_count = &destructor_count;
  123. obj->i = num;
  124. ao2_link(c1, obj);
  125. ao2_t_ref(obj, -1, "test");
  126. if (ao2_container_count(c1) != num) {
  127. ast_test_status_update(test, "container did not link correctly\n");
  128. res = AST_TEST_FAIL;
  129. }
  130. }
  131. ast_test_status_update(test, "Container created: random bucket size %d: number of items: %u\n", bucket_size, lim);
  132. /* Testing ao2_container_clone */
  133. c3 = ao2_container_clone(c1, 0);
  134. if (!c3) {
  135. ast_test_status_update(test, "ao2_container_clone failed.\n");
  136. res = AST_TEST_FAIL;
  137. goto cleanup;
  138. }
  139. if (ao2_container_count(c1) != ao2_container_count(c3)) {
  140. ast_test_status_update(test, "Cloned container does not have the same number of objects.\n");
  141. res = AST_TEST_FAIL;
  142. } else {
  143. it = ao2_iterator_init(c1, 0);
  144. for (; (obj = ao2_t_iterator_next(&it, "test orig")); ao2_t_ref(obj, -1, "test orig")) {
  145. /*
  146. * Unlink the matching object from the cloned container to make
  147. * the next search faster. This is a big speed optimization!
  148. * It reduces the container with 100000 objects test time from
  149. * 18 seconds to 200 ms.
  150. */
  151. obj2 = ao2_t_callback(c3, OBJ_POINTER | OBJ_UNLINK, ao2_match_by_addr, obj,
  152. "test clone");
  153. if (obj2) {
  154. ao2_t_ref(obj2, -1, "test clone");
  155. continue;
  156. }
  157. ast_test_status_update(test,
  158. "Orig container has an object %p not in the clone container.\n", obj);
  159. res = AST_TEST_FAIL;
  160. }
  161. ao2_iterator_destroy(&it);
  162. if (ao2_container_count(c3)) {
  163. ast_test_status_update(test, "Cloned container still has objects.\n");
  164. res = AST_TEST_FAIL;
  165. }
  166. }
  167. ao2_t_ref(c3, -1, "bye c3");
  168. c3 = NULL;
  169. /* Testing ao2_find with no flags */
  170. num = 100;
  171. for (; num; num--) {
  172. int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
  173. tmp_obj.i = i;
  174. if (!(obj = ao2_find(c1, &tmp_obj, 0))) {
  175. res = AST_TEST_FAIL;
  176. ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with no flags failed.\n", i);
  177. } else {
  178. /* a correct match will only take place when the custom cmp function is used */
  179. if (use_cmp && obj->i != i) {
  180. ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
  181. res = AST_TEST_FAIL;
  182. }
  183. ao2_t_ref(obj, -1, "test");
  184. }
  185. }
  186. /* Testing ao2_find with OBJ_POINTER */
  187. num = 75;
  188. for (; num; num--) {
  189. int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
  190. tmp_obj.i = i;
  191. if (!(obj = ao2_find(c1, &tmp_obj, OBJ_POINTER))) {
  192. res = AST_TEST_FAIL;
  193. ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_POINTER flag failed.\n", i);
  194. } else {
  195. /* a correct match will only take place when the custom cmp function is used */
  196. if (use_cmp && obj->i != i) {
  197. ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
  198. res = AST_TEST_FAIL;
  199. }
  200. ao2_t_ref(obj, -1, "test");
  201. }
  202. }
  203. /* Testing ao2_find with OBJ_KEY */
  204. num = 75;
  205. for (; num; num--) {
  206. int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
  207. if (!(obj = ao2_find(c1, &i, OBJ_KEY))) {
  208. res = AST_TEST_FAIL;
  209. ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_KEY flag failed.\n", i);
  210. } else {
  211. /* a correct match will only take place when the custom cmp function is used */
  212. if (use_cmp && obj->i != i) {
  213. ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
  214. res = AST_TEST_FAIL;
  215. }
  216. ao2_t_ref(obj, -1, "test");
  217. }
  218. }
  219. /* Testing ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE.
  220. * In this test items are unlinked from c1 and placed in c2. Then
  221. * unlinked from c2 and placed back into c1.
  222. *
  223. * For this module and set of custom hash/cmp functions, an object
  224. * should only be found if the astobj2 default cmp function is used.
  225. * This test is designed to mimic the chan_iax.c call number use case. */
  226. num = lim < 25 ? lim : 25;
  227. for (; num; num--) {
  228. if (!(obj = ao2_find(c1, NULL, OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE))) {
  229. if (!use_cmp) {
  230. ast_test_status_update(test, "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with default hash function.\n");
  231. res = AST_TEST_FAIL;
  232. }
  233. } else {
  234. if (use_cmp) {
  235. ast_test_status_update(test, "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with custom hash function.\n");
  236. res = AST_TEST_FAIL;
  237. }
  238. ao2_link(c2, obj);
  239. ao2_t_ref(obj, -1, "test");
  240. }
  241. }
  242. it = ao2_iterator_init(c2, 0);
  243. while ((obj = ao2_t_iterator_next(&it, "test"))) {
  244. ao2_t_unlink(c2, obj, "test");
  245. ao2_t_link(c1, obj, "test");
  246. ao2_t_ref(obj, -1, "test");
  247. }
  248. ao2_iterator_destroy(&it);
  249. /* Test Callback with no flags. */
  250. increment = 0;
  251. ao2_t_callback(c1, 0, increment_cb, &increment, "test callback");
  252. if (increment != lim) {
  253. ast_test_status_update(test, "callback with no flags failed. Increment is %d\n", increment);
  254. res = AST_TEST_FAIL;
  255. }
  256. /* Test Callback with OBJ_NODATA. This should do nothing different than with no flags here. */
  257. increment = 0;
  258. ao2_t_callback(c1, OBJ_NODATA, increment_cb, &increment, "test callback");
  259. if (increment != lim) {
  260. ast_test_status_update(test, "callback with OBJ_NODATA failed. Increment is %d\n", increment);
  261. res = AST_TEST_FAIL;
  262. }
  263. /* Test OBJ_MULTIPLE with OBJ_UNLINK*/
  264. num = lim < 25 ? lim : 25;
  265. if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE | OBJ_UNLINK, multiple_cb, &num, "test multiple"))) {
  266. ast_test_status_update(test, "OBJ_MULTIPLE iwth OBJ_UNLINK test failed.\n");
  267. res = AST_TEST_FAIL;
  268. } else {
  269. /* make sure num items unlinked is as expected */
  270. if ((lim - ao2_container_count(c1)) != num) {
  271. ast_test_status_update(test, "OBJ_MULTIPLE | OBJ_UNLINK test failed, did not unlink correct number of objects.\n");
  272. res = AST_TEST_FAIL;
  273. }
  274. /* link what was unlinked back into c1 */
  275. while ((obj = ao2_t_iterator_next(mult_it, "test"))) {
  276. ao2_t_link(c1, obj, "test");
  277. ao2_t_ref(obj, -1, "test"); /* remove ref from iterator */
  278. }
  279. ao2_iterator_destroy(mult_it);
  280. }
  281. /* Test OBJ_MULTIPLE without unlink, add items back afterwards */
  282. num = lim < 25 ? lim : 25;
  283. if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE, multiple_cb, &num, "test multiple"))) {
  284. ast_test_status_update(test, "OBJ_MULTIPLE without OBJ_UNLINK test failed.\n");
  285. res = AST_TEST_FAIL;
  286. } else {
  287. while ((obj = ao2_t_iterator_next(mult_it, "test"))) {
  288. ao2_t_ref(obj, -1, "test"); /* remove ref from iterator */
  289. }
  290. ao2_iterator_destroy(mult_it);
  291. }
  292. /* Test OBJ_MULTIPLE without unlink and no iterating */
  293. num = lim < 5 ? lim : 5;
  294. if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE, multiple_cb, &num, "test multiple"))) {
  295. ast_test_status_update(test, "OBJ_MULTIPLE with no OBJ_UNLINK and no iterating failed.\n");
  296. res = AST_TEST_FAIL;
  297. } else {
  298. ao2_iterator_destroy(mult_it);
  299. }
  300. /* Is the container count what we expect after all the finds and unlinks? */
  301. if (ao2_container_count(c1) != lim) {
  302. ast_test_status_update(test, "container count does not match what is expected after ao2_find tests.\n");
  303. res = AST_TEST_FAIL;
  304. }
  305. /* Testing iterator. Unlink a single object and break. do not add item back */
  306. it = ao2_iterator_init(c1, 0);
  307. num = (lim / 4) + 1;
  308. while ((obj = ao2_t_iterator_next(&it, "test"))) {
  309. if (obj->i == num) {
  310. ao2_t_ref(obj, -1, "test");
  311. ao2_t_unlink(c1, obj, "test");
  312. break;
  313. }
  314. ao2_t_ref(obj, -1, "test");
  315. }
  316. 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);
  320. res = AST_TEST_FAIL;
  321. }
  322. /* Test unlink all with OBJ_MULTIPLE, leave a single object for the container to destroy */
  323. ao2_t_callback(c1, OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA, all_but_one_cb, NULL, "test multiple");
  324. /* check to make sure all test_obj destructors were called except for 1 */
  325. if (destructor_count != 1) {
  326. ast_test_status_update(test, "OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA failed. destructor count %d\n", destructor_count);
  327. res = AST_TEST_FAIL;
  328. }
  329. cleanup:
  330. /* destroy containers */
  331. if (c1) {
  332. ao2_t_ref(c1, -1, "bye c1");
  333. }
  334. if (c2) {
  335. ao2_t_ref(c2, -1, "bye c2");
  336. }
  337. if (c3) {
  338. ao2_t_ref(c3, -1, "bye c3");
  339. }
  340. if (destructor_count > 0) {
  341. ast_test_status_update(test, "all destructors were not called, destructor count is %d\n", destructor_count);
  342. res = AST_TEST_FAIL;
  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);
  345. res = AST_TEST_FAIL;
  346. }
  347. return res;
  348. }
  349. AST_TEST_DEFINE(astobj2_test_1)
  350. {
  351. int res = AST_TEST_PASS;
  352. switch (cmd) {
  353. case TEST_INIT:
  354. info->name = "astobj2_test1";
  355. info->category = "/main/astobj2/";
  356. info->summary = "astobj2 test using ao2 objects, containers, callbacks, and iterators";
  357. info->description =
  358. "Builds ao2_containers with various item numbers, bucket sizes, cmp and hash "
  359. "functions. Runs a series of tests to manipulate the container using callbacks "
  360. "and iterators. Verifies expected behavior.";
  361. return AST_TEST_NOT_RUN;
  362. case TEST_EXECUTE:
  363. break;
  364. }
  365. /* Test 1, 500 items with custom hash and cmp functions */
  366. ast_test_status_update(test, "Test 1, astobj2 test with 500 items.\n");
  367. if ((res = astobj2_test_helper(1, 1, 500, test)) == AST_TEST_FAIL) {
  368. return res;
  369. }
  370. /* Test 2, 1000 items with custom hash and default cmp functions */
  371. ast_test_status_update(test, "Test 2, astobj2 test with 1000 items.\n");
  372. if ((res = astobj2_test_helper(1, 0, 1000, test)) == AST_TEST_FAIL) {
  373. return res;
  374. }
  375. /* Test 3, 10000 items with default hash and custom cmp functions */
  376. ast_test_status_update(test, "Test 3, astobj2 test with 10000 items.\n");
  377. if ((res = astobj2_test_helper(0, 1, 10000, test)) == AST_TEST_FAIL) {
  378. return res;
  379. }
  380. /* Test 4, 100000 items with default hash and cmp functions */
  381. ast_test_status_update(test, "Test 4, astobj2 test with 100000 items.\n");
  382. if ((res = astobj2_test_helper(0, 0, 100000, test)) == AST_TEST_FAIL) {
  383. return res;
  384. }
  385. return res;
  386. }
  387. AST_TEST_DEFINE(astobj2_test_2)
  388. {
  389. int res = AST_TEST_PASS;
  390. struct ao2_container *c;
  391. struct ao2_iterator i;
  392. struct test_obj *obj;
  393. int num;
  394. static const int NUM_OBJS = 5;
  395. int destructor_count = NUM_OBJS;
  396. struct test_obj tmp_obj = { 0, };
  397. switch (cmd) {
  398. case TEST_INIT:
  399. info->name = "astobj2_test2";
  400. info->category = "/main/astobj2/";
  401. info->summary = "Test a certain scenario using ao2 iterators";
  402. info->description =
  403. "This test is aimed at testing for a specific regression that occurred. "
  404. "Add some objects into a container. Mix finds and iteration and make "
  405. "sure that the iterator still sees all objects.";
  406. return AST_TEST_NOT_RUN;
  407. case TEST_EXECUTE:
  408. break;
  409. }
  410. c = ao2_container_alloc(1, NULL, test_cmp_cb);
  411. if (!c) {
  412. ast_test_status_update(test, "ao2_container_alloc failed.\n");
  413. res = AST_TEST_FAIL;
  414. goto cleanup;
  415. }
  416. for (num = 1; num <= NUM_OBJS; num++) {
  417. if (!(obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor))) {
  418. ast_test_status_update(test, "ao2_alloc failed.\n");
  419. res = AST_TEST_FAIL;
  420. goto cleanup;
  421. }
  422. obj->destructor_count = &destructor_count;
  423. obj->i = num;
  424. ao2_link(c, obj);
  425. ao2_ref(obj, -1);
  426. if (ao2_container_count(c) != num) {
  427. ast_test_status_update(test, "container did not link correctly\n");
  428. res = AST_TEST_FAIL;
  429. }
  430. }
  431. /*
  432. * Iteration take 1. Just make sure we see all NUM_OBJS objects.
  433. */
  434. num = 0;
  435. i = ao2_iterator_init(c, 0);
  436. while ((obj = ao2_iterator_next(&i))) {
  437. num++;
  438. ao2_ref(obj, -1);
  439. }
  440. ao2_iterator_destroy(&i);
  441. if (num != NUM_OBJS) {
  442. ast_test_status_update(test, "iterate take 1, expected '%d', only saw '%d' objects\n",
  443. NUM_OBJS, num);
  444. res = AST_TEST_FAIL;
  445. }
  446. /*
  447. * Iteration take 2. Do a find for the last object, then iterate and make
  448. * sure we find all NUM_OBJS objects.
  449. */
  450. tmp_obj.i = NUM_OBJS;
  451. obj = ao2_find(c, &tmp_obj, OBJ_POINTER);
  452. if (!obj) {
  453. ast_test_status_update(test, "ao2_find() failed.\n");
  454. res = AST_TEST_FAIL;
  455. } else {
  456. ao2_ref(obj, -1);
  457. }
  458. num = 0;
  459. i = ao2_iterator_init(c, 0);
  460. while ((obj = ao2_iterator_next(&i))) {
  461. num++;
  462. ao2_ref(obj, -1);
  463. }
  464. ao2_iterator_destroy(&i);
  465. if (num != NUM_OBJS) {
  466. ast_test_status_update(test, "iterate take 2, expected '%d', only saw '%d' objects\n",
  467. NUM_OBJS, num);
  468. res = AST_TEST_FAIL;
  469. }
  470. /*
  471. * Iteration take 3. Do a find for an object while in the middle
  472. * of iterating;
  473. */
  474. num = 0;
  475. i = ao2_iterator_init(c, 0);
  476. while ((obj = ao2_iterator_next(&i))) {
  477. if (num == 1) {
  478. struct test_obj *obj2;
  479. tmp_obj.i = NUM_OBJS - 1;
  480. obj2 = ao2_find(c, &tmp_obj, OBJ_POINTER);
  481. if (!obj2) {
  482. ast_test_status_update(test, "ao2_find() failed.\n");
  483. res = AST_TEST_FAIL;
  484. } else {
  485. ao2_ref(obj2, -1);
  486. }
  487. }
  488. num++;
  489. ao2_ref(obj, -1);
  490. }
  491. ao2_iterator_destroy(&i);
  492. if (num != NUM_OBJS) {
  493. ast_test_status_update(test, "iterate take 3, expected '%d', only saw '%d' objects\n",
  494. NUM_OBJS, num);
  495. res = AST_TEST_FAIL;
  496. }
  497. cleanup:
  498. if (c) {
  499. ao2_ref(c, -1);
  500. }
  501. return res;
  502. }
  503. static AO2_GLOBAL_OBJ_STATIC(astobj2_holder);
  504. AST_TEST_DEFINE(astobj2_test_3)
  505. {
  506. int res = AST_TEST_PASS;
  507. int destructor_count = 0;
  508. int num_objects = 0;
  509. struct test_obj *obj = NULL;
  510. struct test_obj *obj2 = NULL;
  511. struct test_obj *obj3 = NULL;
  512. switch (cmd) {
  513. case TEST_INIT:
  514. info->name = "astobj2_test3";
  515. info->category = "/main/astobj2/";
  516. info->summary = "Test global ao2 holder";
  517. info->description =
  518. "This test is to see if the global ao2 holder works as intended.";
  519. return AST_TEST_NOT_RUN;
  520. case TEST_EXECUTE:
  521. break;
  522. }
  523. /* Put an object in the holder */
  524. obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
  525. if (!obj) {
  526. ast_test_status_update(test, "ao2_alloc failed.\n");
  527. res = AST_TEST_FAIL;
  528. goto cleanup;
  529. }
  530. obj->destructor_count = &destructor_count;
  531. obj->i = ++num_objects;
  532. obj2 = ao2_t_global_obj_replace(astobj2_holder, obj, "Save object in the holder");
  533. if (obj2) {
  534. ast_test_status_update(test, "Returned object not expected.\n");
  535. res = AST_TEST_FAIL;
  536. goto cleanup;
  537. }
  538. /* Save object for next check. */
  539. obj3 = obj;
  540. /* Replace an object in the holder */
  541. obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
  542. if (!obj) {
  543. ast_test_status_update(test, "ao2_alloc failed.\n");
  544. res = AST_TEST_FAIL;
  545. goto cleanup;
  546. }
  547. obj->destructor_count = &destructor_count;
  548. obj->i = ++num_objects;
  549. obj2 = ao2_t_global_obj_replace(astobj2_holder, obj, "Replace object in the holder");
  550. if (!obj2) {
  551. ast_test_status_update(test, "Expected an object.\n");
  552. res = AST_TEST_FAIL;
  553. goto cleanup;
  554. }
  555. if (obj2 != obj3) {
  556. ast_test_status_update(test, "Replaced object not expected object.\n");
  557. res = AST_TEST_FAIL;
  558. goto cleanup;
  559. }
  560. ao2_ref(obj3, -1);
  561. obj3 = NULL;
  562. ao2_ref(obj2, -1);
  563. obj2 = NULL;
  564. ao2_ref(obj, -1);
  565. /* Replace with unref of an object in the holder */
  566. obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
  567. if (!obj) {
  568. ast_test_status_update(test, "ao2_alloc failed.\n");
  569. res = AST_TEST_FAIL;
  570. goto cleanup;
  571. }
  572. obj->destructor_count = &destructor_count;
  573. obj->i = ++num_objects;
  574. if (!ao2_t_global_obj_replace_unref(astobj2_holder, obj, "Replace w/ unref object in the holder")) {
  575. ast_test_status_update(test, "Expected an object to be replaced.\n");
  576. res = AST_TEST_FAIL;
  577. goto cleanup;
  578. }
  579. /* Save object for next check. */
  580. obj3 = obj;
  581. /* Get reference to held object. */
  582. obj = ao2_t_global_obj_ref(astobj2_holder, "Get a held object reference");
  583. if (!obj) {
  584. ast_test_status_update(test, "Expected an object.\n");
  585. res = AST_TEST_FAIL;
  586. goto cleanup;
  587. }
  588. if (obj != obj3) {
  589. ast_test_status_update(test, "Referenced object not expected object.\n");
  590. res = AST_TEST_FAIL;
  591. goto cleanup;
  592. }
  593. ao2_ref(obj3, -1);
  594. obj3 = NULL;
  595. ao2_ref(obj, -1);
  596. obj = NULL;
  597. /* Release the object in the global holder. */
  598. ao2_t_global_obj_release(astobj2_holder, "Check release all objects");
  599. destructor_count += num_objects;
  600. if (0 < destructor_count) {
  601. ast_test_status_update(test,
  602. "all destructors were not called, destructor count is %d\n",
  603. destructor_count);
  604. res = AST_TEST_FAIL;
  605. } else if (destructor_count < 0) {
  606. ast_test_status_update(test,
  607. "Destructor was called too many times, destructor count is %d\n",
  608. destructor_count);
  609. res = AST_TEST_FAIL;
  610. }
  611. cleanup:
  612. if (obj) {
  613. ao2_t_ref(obj, -1, "Test cleanup external object 1");
  614. }
  615. if (obj2) {
  616. ao2_t_ref(obj2, -1, "Test cleanup external object 2");
  617. }
  618. if (obj3) {
  619. ao2_t_ref(obj3, -1, "Test cleanup external object 3");
  620. }
  621. ao2_t_global_obj_release(astobj2_holder, "Test cleanup holder");
  622. return res;
  623. }
  624. static int unload_module(void)
  625. {
  626. AST_TEST_UNREGISTER(astobj2_test_1);
  627. AST_TEST_UNREGISTER(astobj2_test_2);
  628. AST_TEST_UNREGISTER(astobj2_test_3);
  629. return 0;
  630. }
  631. static int load_module(void)
  632. {
  633. AST_TEST_REGISTER(astobj2_test_1);
  634. AST_TEST_REGISTER(astobj2_test_2);
  635. AST_TEST_REGISTER(astobj2_test_3);
  636. return AST_MODULE_LOAD_SUCCESS;
  637. }
  638. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ASTOBJ2 Unit Tests");