test_res_pjsip_scheduler.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 res_pjsip scheduler tests
  21. *
  22. * \author George Joseph <george.joseph@fairview5.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>TEST_FRAMEWORK</depend>
  27. <depend>pjproject</depend>
  28. <depend>res_pjsip</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. #include <pjsip.h>
  33. #include "asterisk/test.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/taskprocessor.h"
  36. #include "asterisk/res_pjsip.h"
  37. #include "asterisk/utils.h"
  38. #define CATEGORY "/res/res_pjsip/scheduler/"
  39. struct test_data {
  40. ast_mutex_t lock;
  41. ast_cond_t cond;
  42. pthread_t tid;
  43. struct timeval test_start;
  44. struct timeval task_start;
  45. struct timeval task_end;
  46. int is_servant;
  47. int interval;
  48. int sleep;
  49. int done;
  50. int no_clear_done;
  51. struct ast_test *test;
  52. };
  53. #define S2U(x) (long int)(x * 1000 * 1000)
  54. #define M2U(x) (long int)(x * 1000)
  55. static int task_1(void *data)
  56. {
  57. struct test_data *test = data;
  58. if (!test->no_clear_done) {
  59. test->done = 0;
  60. }
  61. test->task_start = ast_tvnow();
  62. test->tid = pthread_self();
  63. test->is_servant = ast_sip_thread_is_servant();
  64. usleep(M2U(test->sleep));
  65. test->task_end = ast_tvnow();
  66. ast_mutex_lock(&test->lock);
  67. test->done++;
  68. ast_mutex_unlock(&test->lock);
  69. ast_cond_signal(&test->cond);
  70. return test->interval;
  71. }
  72. static void data_cleanup(void *data)
  73. {
  74. struct test_data *test_data = data;
  75. ast_mutex_destroy(&test_data->lock);
  76. ast_cond_destroy(&test_data->cond);
  77. }
  78. #define waitfor(x) \
  79. { \
  80. ast_mutex_lock(&(x)->lock); \
  81. while (!(x)->done) { \
  82. ast_cond_wait(&(x)->cond, &(x)->lock); \
  83. } \
  84. (x)->done = 0; \
  85. ast_mutex_unlock(&(x)->lock); \
  86. }
  87. static int scheduler(struct ast_test *test, int serialized)
  88. {
  89. RAII_VAR(struct ast_taskprocessor *, tp1, NULL, ast_taskprocessor_unreference);
  90. RAII_VAR(struct test_data *, test_data1, ao2_alloc(sizeof(*test_data1), data_cleanup), ao2_cleanup);
  91. RAII_VAR(struct test_data *, test_data2, ao2_alloc(sizeof(*test_data2), data_cleanup), ao2_cleanup);
  92. RAII_VAR(struct ast_sip_sched_task *, task1, NULL, ao2_cleanup);
  93. RAII_VAR(struct ast_sip_sched_task *, task2, NULL, ao2_cleanup);
  94. int duration;
  95. int delay;
  96. struct timeval task1_start;
  97. ast_test_validate(test, test_data1 != NULL);
  98. ast_test_validate(test, test_data2 != NULL);
  99. test_data1->test = test;
  100. test_data1->test_start = ast_tvnow();
  101. test_data1->interval = 2000;
  102. test_data1->sleep = 1000;
  103. ast_mutex_init(&test_data1->lock);
  104. ast_cond_init(&test_data1->cond, NULL);
  105. test_data2->test = test;
  106. test_data2->test_start = ast_tvnow();
  107. test_data2->interval = 2000;
  108. test_data2->sleep = 1000;
  109. ast_mutex_init(&test_data2->lock);
  110. ast_cond_init(&test_data2->cond, NULL);
  111. if (serialized) {
  112. ast_test_status_update(test, "This test will take about %3.1f seconds\n",
  113. (test_data1->interval + test_data1->sleep + (MAX(test_data1->interval - test_data2->interval, 0)) + test_data2->sleep) / 1000.0);
  114. tp1 = ast_sip_create_serializer("test-scheduler-serializer");
  115. ast_test_validate(test, (tp1 != NULL));
  116. } else {
  117. ast_test_status_update(test, "This test will take about %3.1f seconds\n",
  118. ((MAX(test_data1->interval, test_data2->interval) + MAX(test_data1->sleep, test_data2->sleep)) / 1000.0));
  119. }
  120. task1 = ast_sip_schedule_task(tp1, test_data1->interval, task_1, NULL, test_data1, AST_SIP_SCHED_TASK_FIXED);
  121. ast_test_validate(test, task1 != NULL);
  122. task2 = ast_sip_schedule_task(tp1, test_data2->interval, task_1, NULL, test_data2, AST_SIP_SCHED_TASK_FIXED);
  123. ast_test_validate(test, task2 != NULL);
  124. waitfor(test_data1);
  125. ast_sip_sched_task_cancel(task1);
  126. ast_test_validate(test, test_data1->is_servant);
  127. duration = ast_tvdiff_ms(test_data1->task_end, test_data1->test_start);
  128. ast_test_validate(test, (duration > ((test_data1->interval + test_data1->sleep) * 0.9))
  129. && (duration < ((test_data1->interval + test_data1->sleep) * 1.1)));
  130. ast_sip_sched_task_get_times(task1, NULL, &task1_start, NULL);
  131. delay = ast_tvdiff_ms(task1_start, test_data1->test_start);
  132. ast_test_validate(test, (delay > (test_data1->interval * 0.9)
  133. && (delay < (test_data1->interval * 1.1))));
  134. waitfor(test_data2);
  135. ast_sip_sched_task_cancel(task2);
  136. ast_test_validate(test, test_data2->is_servant);
  137. if (serialized) {
  138. ast_test_validate(test, test_data1->tid == test_data2->tid);
  139. ast_test_validate(test, ast_tvdiff_ms(test_data2->task_start, test_data1->task_end) >= 0);
  140. } else {
  141. ast_test_validate(test, test_data1->tid != test_data2->tid);
  142. }
  143. return AST_TEST_PASS;
  144. }
  145. AST_TEST_DEFINE(serialized_scheduler)
  146. {
  147. switch (cmd) {
  148. case TEST_INIT:
  149. info->name = __func__;
  150. info->category = CATEGORY;
  151. info->summary = "Test res_pjsip serialized scheduler";
  152. info->description = "Test res_pjsip serialized scheduler";
  153. return AST_TEST_NOT_RUN;
  154. case TEST_EXECUTE:
  155. break;
  156. }
  157. return scheduler(test, 1);
  158. }
  159. AST_TEST_DEFINE(unserialized_scheduler)
  160. {
  161. switch (cmd) {
  162. case TEST_INIT:
  163. info->name = __func__;
  164. info->category = CATEGORY;
  165. info->summary = "Test res_pjsip unserialized scheduler";
  166. info->description = "Test res_pjsip unserialized scheduler";
  167. return AST_TEST_NOT_RUN;
  168. case TEST_EXECUTE:
  169. break;
  170. }
  171. return scheduler(test, 0);
  172. }
  173. static int run_count;
  174. static int destruct_count;
  175. static int dummy_task(void *data)
  176. {
  177. int *sleep = data;
  178. usleep(M2U(*sleep));
  179. run_count++;
  180. return 0;
  181. }
  182. static void test_destructor(void *data)
  183. {
  184. destruct_count++;
  185. }
  186. AST_TEST_DEFINE(scheduler_cleanup)
  187. {
  188. RAII_VAR(int *, sleep, NULL, ao2_cleanup);
  189. RAII_VAR(struct ast_sip_sched_task *, task, NULL, ao2_cleanup);
  190. int interval;
  191. int when;
  192. switch (cmd) {
  193. case TEST_INIT:
  194. info->name = __func__;
  195. info->category = CATEGORY;
  196. info->summary = "Test res_pjsip scheduler cleanup";
  197. info->description = "Test res_pjsip scheduler cleanup";
  198. return AST_TEST_NOT_RUN;
  199. case TEST_EXECUTE:
  200. break;
  201. }
  202. destruct_count = 0;
  203. interval = 1000;
  204. sleep = ao2_alloc(sizeof(*sleep), test_destructor);
  205. ast_test_validate(test, sleep != NULL);
  206. *sleep = 500;
  207. ast_test_status_update(test, "This test will take about %3.1f seconds\n",
  208. ((interval * 1.1) + *sleep) / 1000.0);
  209. task = ast_sip_schedule_task(NULL, interval, dummy_task, "dummy", sleep,
  210. AST_SIP_SCHED_TASK_DATA_AO2 | AST_SIP_SCHED_TASK_DATA_FREE);
  211. ast_test_validate(test, task != NULL);
  212. usleep(M2U(interval * 0.5));
  213. when = ast_sip_sched_task_get_next_run(task);
  214. ast_test_validate(test, (when > (interval * 0.4) && when < (interval * 0.6)));
  215. usleep(M2U(interval * 0.6));
  216. ast_test_validate(test, ast_sip_sched_is_task_running(task));
  217. usleep(M2U(*sleep));
  218. ast_test_validate(test, (ast_sip_sched_is_task_running(task) == 0));
  219. when = ast_sip_sched_task_get_next_run(task);
  220. ast_test_validate(test, (when < 0), res, error);
  221. ast_test_validate(test, (ao2_ref(task, 0) == 1));
  222. ao2_ref(task, -1);
  223. task = NULL;
  224. ast_test_validate(test, (destruct_count == 1));
  225. sleep = NULL;
  226. return AST_TEST_PASS;
  227. }
  228. AST_TEST_DEFINE(scheduler_cancel)
  229. {
  230. RAII_VAR(int *, sleep, NULL, ao2_cleanup);
  231. RAII_VAR(struct ast_sip_sched_task *, task, NULL, ao2_cleanup);
  232. int interval;
  233. int when;
  234. switch (cmd) {
  235. case TEST_INIT:
  236. info->name = __func__;
  237. info->category = CATEGORY;
  238. info->summary = "Test res_pjsip scheduler cancel task";
  239. info->description = "Test res_pjsip scheduler cancel task";
  240. return AST_TEST_NOT_RUN;
  241. case TEST_EXECUTE:
  242. break;
  243. }
  244. destruct_count = 0;
  245. interval = 1000;
  246. sleep = ao2_alloc(sizeof(*sleep), test_destructor);
  247. ast_test_validate(test, sleep != NULL);
  248. *sleep = 500;
  249. ast_test_status_update(test, "This test will take about %3.1f seconds\n",
  250. (interval + *sleep) / 1000.0);
  251. task = ast_sip_schedule_task(NULL, interval, dummy_task, "dummy", sleep, AST_SIP_SCHED_TASK_DATA_NO_CLEANUP);
  252. ast_test_validate(test, task != NULL);
  253. usleep(M2U(interval * 0.5));
  254. when = ast_sip_sched_task_get_next_run_by_name("dummy");
  255. ast_test_validate(test, (when > (interval * 0.4) && when < (interval * 0.6)));
  256. ast_test_validate(test, !ast_sip_sched_is_task_running_by_name("dummy"));
  257. ast_test_validate(test, ao2_ref(task, 0) == 2);
  258. ast_sip_sched_task_cancel_by_name("dummy");
  259. when = ast_sip_sched_task_get_next_run(task);
  260. ast_test_validate(test, when < 0);
  261. usleep(M2U(interval));
  262. ast_test_validate(test, run_count == 0);
  263. ast_test_validate(test, destruct_count == 0);
  264. ast_test_validate(test, ao2_ref(task, 0) == 1);
  265. return AST_TEST_PASS;
  266. }
  267. AST_TEST_DEFINE(scheduler_policy)
  268. {
  269. RAII_VAR(struct test_data *, test_data1, ao2_alloc(sizeof(*test_data1), data_cleanup), ao2_cleanup);
  270. RAII_VAR(struct ast_sip_sched_task *, task, NULL, ao2_cleanup);
  271. int when;
  272. switch (cmd) {
  273. case TEST_INIT:
  274. info->name = __func__;
  275. info->category = CATEGORY;
  276. info->summary = "Test res_pjsip scheduler cancel task";
  277. info->description = "Test res_pjsip scheduler cancel task";
  278. return AST_TEST_NOT_RUN;
  279. case TEST_EXECUTE:
  280. break;
  281. }
  282. ast_test_validate(test, test_data1 != NULL);
  283. destruct_count = 0;
  284. run_count = 0;
  285. test_data1->test = test;
  286. test_data1->test_start = ast_tvnow();
  287. test_data1->interval = 1000;
  288. test_data1->sleep = 500;
  289. test_data1->no_clear_done = 1;
  290. ast_mutex_init(&test_data1->lock);
  291. ast_cond_init(&test_data1->cond, NULL);
  292. ast_test_status_update(test, "This test will take about %3.1f seconds\n",
  293. ((test_data1->interval * 4) + test_data1->sleep) / 1000.0);
  294. task = ast_sip_schedule_task(NULL, test_data1->interval, task_1, "test_1", test_data1,
  295. AST_SIP_SCHED_TASK_DATA_NO_CLEANUP | AST_SIP_SCHED_TASK_PERIODIC);
  296. ast_test_validate(test, task != NULL);
  297. waitfor(test_data1);
  298. when = ast_tvdiff_ms(test_data1->task_start, test_data1->test_start);
  299. ast_test_validate(test, when > test_data1->interval * 0.9 && when < test_data1->interval * 1.1);
  300. waitfor(test_data1);
  301. when = ast_tvdiff_ms(test_data1->task_start, test_data1->test_start);
  302. ast_test_validate(test, when > test_data1->interval * 2 * 0.9 && when < test_data1->interval * 2 * 1.1);
  303. waitfor(test_data1);
  304. when = ast_tvdiff_ms(test_data1->task_start, test_data1->test_start);
  305. ast_test_validate(test, when > test_data1->interval * 3 * 0.9 && when < test_data1->interval * 3 * 1.1);
  306. ast_sip_sched_task_cancel(task);
  307. /* Wait a full interval in case a 4th call to test_1 happened before the cancel */
  308. usleep(M2U(test_data1->interval));
  309. ast_mutex_lock(&test_data1->lock);
  310. if (test_data1->done) {
  311. int done = test_data1->done;
  312. test_data1->done = 0;
  313. ast_mutex_unlock(&test_data1->lock);
  314. ast_test_validate(test, done == 1);
  315. /* Wait two full intervals to be certain no further calls to test_1. */
  316. usleep(M2U(test_data1->interval * 2));
  317. ast_mutex_lock(&test_data1->lock);
  318. if (test_data1->done != 0) {
  319. ast_mutex_unlock(&test_data1->lock);
  320. /* The cancelation failed so we need to prevent cleanup of
  321. * test_data1 to prevent a crash from write-after-free. */
  322. test_data1 = NULL;
  323. ast_test_status_update(test, "Failed to cancel task");
  324. return AST_TEST_FAIL;
  325. }
  326. }
  327. ast_mutex_unlock(&test_data1->lock);
  328. return AST_TEST_PASS;
  329. }
  330. static int load_module(void)
  331. {
  332. AST_TEST_REGISTER(serialized_scheduler);
  333. AST_TEST_REGISTER(unserialized_scheduler);
  334. AST_TEST_REGISTER(scheduler_cleanup);
  335. AST_TEST_REGISTER(scheduler_cancel);
  336. AST_TEST_REGISTER(scheduler_policy);
  337. return AST_MODULE_LOAD_SUCCESS;
  338. }
  339. static int unload_module(void)
  340. {
  341. AST_TEST_UNREGISTER(scheduler_cancel);
  342. AST_TEST_UNREGISTER(scheduler_cleanup);
  343. AST_TEST_UNREGISTER(unserialized_scheduler);
  344. AST_TEST_UNREGISTER(serialized_scheduler);
  345. AST_TEST_UNREGISTER(scheduler_policy);
  346. return 0;
  347. }
  348. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "res_pjsip scheduler test module",
  349. .support_level = AST_MODULE_SUPPORT_CORE,
  350. .load = load_module,
  351. .unload = unload_module,
  352. .requires = "res_pjsip",
  353. );