test_named_lock.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2016, Fairview 5 Engineering, LLC
  5. *
  6. * George Joseph <george.joseph@fairview5.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 Named Lock unit tests
  21. *
  22. * \author George Joseph <george.joseph@fairview5.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>TEST_FRAMEWORK</depend>
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. #include <signal.h>
  31. #include "asterisk/test.h"
  32. #include "asterisk/utils.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/lock.h"
  35. #include "asterisk/named_locks.h"
  36. static void *lock_thread(void *data)
  37. {
  38. struct ast_named_lock *lock = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "lock_test", data);
  39. if (!lock) {
  40. return NULL;
  41. }
  42. ao2_lock(lock);
  43. usleep(3000000);
  44. ao2_unlock(lock);
  45. ast_named_lock_put(lock);
  46. return NULL;
  47. }
  48. AST_TEST_DEFINE(named_lock_test)
  49. {
  50. enum ast_test_result_state res = AST_TEST_FAIL;
  51. struct ast_named_lock *lock1 = NULL;
  52. struct ast_named_lock *lock2 = NULL;
  53. pthread_t thread1;
  54. pthread_t thread2;
  55. struct timeval start_time;
  56. int64_t duration;
  57. switch(cmd) {
  58. case TEST_INIT:
  59. info->name = "named_lock_test";
  60. info->category = "/main/lock/";
  61. info->summary = "Named Lock test";
  62. info->description =
  63. "Tests that named locks operate as expected";
  64. return AST_TEST_NOT_RUN;
  65. case TEST_EXECUTE:
  66. break;
  67. }
  68. ast_test_status_update(test, "This test should take about 3 seconds\n");
  69. /* 2 locks/threads to make sure they're independent */
  70. ast_pthread_create(&thread1, NULL, lock_thread, "lock_1");
  71. ast_pthread_create(&thread2, NULL, lock_thread, "lock_2");
  72. lock1 = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "lock_test", "lock_1");
  73. ast_test_validate_cleanup(test, lock1 != NULL, res, fail);
  74. lock2 = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "lock_test", "lock_2");
  75. ast_test_validate_cleanup(test, lock2 != NULL, res, fail);
  76. usleep(1000000);
  77. /* These should both fail */
  78. if (!ao2_trylock(lock1)) {
  79. ast_test_status_update(test, "ao2_trylock on lock1 succeeded when it should have failed\n");
  80. ao2_unlock(lock1);
  81. goto fail;
  82. }
  83. if (!ao2_trylock(lock2)) {
  84. ast_test_status_update(test, "ao2_trylock on lock2 succeeded when it should have failed\n");
  85. ao2_unlock(lock2);
  86. goto fail;
  87. }
  88. start_time = ast_tvnow();
  89. /* These should both succeed eventually */
  90. if (ao2_lock(lock1)) {
  91. ast_test_status_update(test, "ao2_lock on lock1 failed\n");
  92. goto fail;
  93. }
  94. ao2_unlock(lock1);
  95. if (ao2_lock(lock2)) {
  96. ast_test_status_update(test, "ao2_lock on lock2 failed\n");
  97. goto fail;
  98. }
  99. ao2_unlock(lock2);
  100. duration = ast_tvdiff_ms(ast_tvnow(), start_time);
  101. ast_test_validate_cleanup(test, duration > 1500 && duration < 3500, res, fail);
  102. res = AST_TEST_PASS;
  103. fail:
  104. ast_named_lock_put(lock1);
  105. ast_named_lock_put(lock2);
  106. pthread_join(thread1, NULL);
  107. pthread_join(thread2, NULL);
  108. return res;
  109. }
  110. static int unload_module(void)
  111. {
  112. AST_TEST_UNREGISTER(named_lock_test);
  113. return 0;
  114. }
  115. static int load_module(void)
  116. {
  117. AST_TEST_REGISTER(named_lock_test);
  118. return AST_MODULE_LOAD_SUCCESS;
  119. }
  120. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Named Lock test module");