test_stasis_endpoints.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@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 \brief Test endpoints.
  20. *
  21. * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
  22. *
  23. * \ingroup tests
  24. */
  25. /*** MODULEINFO
  26. <depend>TEST_FRAMEWORK</depend>
  27. <depend>res_stasis_test</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/astobj2.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/endpoints.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/stasis_channels.h"
  37. #include "asterisk/stasis_endpoints.h"
  38. #include "asterisk/stasis_test.h"
  39. #include "asterisk/test.h"
  40. static const char *test_category = "/stasis/endpoints/";
  41. /*! \brief Message matcher looking for cache update messages */
  42. static int cache_update(struct stasis_message *msg, const void *data) {
  43. struct stasis_cache_update *update;
  44. struct ast_endpoint_snapshot *snapshot;
  45. const char *name = data;
  46. if (stasis_cache_update_type() != stasis_message_type(msg)) {
  47. return 0;
  48. }
  49. update = stasis_message_data(msg);
  50. if (ast_endpoint_snapshot_type() != update->type) {
  51. return 0;
  52. }
  53. snapshot = stasis_message_data(update->old_snapshot);
  54. if (!snapshot) {
  55. snapshot = stasis_message_data(update->new_snapshot);
  56. }
  57. return 0 == strcmp(name, snapshot->resource);
  58. }
  59. AST_TEST_DEFINE(state_changes)
  60. {
  61. RAII_VAR(struct ast_endpoint *, uut, NULL, ast_endpoint_shutdown);
  62. RAII_VAR(struct ast_channel *, chan, NULL, ast_hangup);
  63. RAII_VAR(struct stasis_message_sink *, sink, NULL, ao2_cleanup);
  64. RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe);
  65. struct stasis_message *msg;
  66. struct stasis_message_type *type;
  67. struct ast_endpoint_snapshot *actual_snapshot;
  68. int actual_count;
  69. switch (cmd) {
  70. case TEST_INIT:
  71. info->name = __func__;
  72. info->category = test_category;
  73. info->summary = "Test endpoint updates as its state changes";
  74. info->description =
  75. "Test endpoint updates as its state changes";
  76. return AST_TEST_NOT_RUN;
  77. case TEST_EXECUTE:
  78. break;
  79. }
  80. uut = ast_endpoint_create("TEST", __func__);
  81. ast_test_validate(test, NULL != uut);
  82. sink = stasis_message_sink_create();
  83. ast_test_validate(test, NULL != sink);
  84. sub = stasis_subscribe(ast_endpoint_topic(uut),
  85. stasis_message_sink_cb(), sink);
  86. ast_test_validate(test, NULL != sub);
  87. ast_endpoint_set_state(uut, AST_ENDPOINT_OFFLINE);
  88. actual_count = stasis_message_sink_wait_for_count(sink, 1,
  89. STASIS_SINK_DEFAULT_WAIT);
  90. ast_test_validate(test, 1 == actual_count);
  91. msg = sink->messages[0];
  92. type = stasis_message_type(msg);
  93. ast_test_validate(test, ast_endpoint_snapshot_type() == type);
  94. actual_snapshot = stasis_message_data(msg);
  95. ast_test_validate(test, AST_ENDPOINT_OFFLINE == actual_snapshot->state);
  96. ast_endpoint_set_max_channels(uut, 8675309);
  97. actual_count = stasis_message_sink_wait_for_count(sink, 2,
  98. STASIS_SINK_DEFAULT_WAIT);
  99. ast_test_validate(test, 2 == actual_count);
  100. msg = sink->messages[1];
  101. type = stasis_message_type(msg);
  102. ast_test_validate(test, ast_endpoint_snapshot_type() == type);
  103. actual_snapshot = stasis_message_data(msg);
  104. ast_test_validate(test, 8675309 == actual_snapshot->max_channels);
  105. return AST_TEST_PASS;
  106. }
  107. AST_TEST_DEFINE(cache_clear)
  108. {
  109. RAII_VAR(struct ast_endpoint *, uut, NULL, ast_endpoint_shutdown);
  110. RAII_VAR(struct ast_channel *, chan, NULL, ast_hangup);
  111. RAII_VAR(struct stasis_message_sink *, sink, NULL, ao2_cleanup);
  112. RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe);
  113. struct stasis_message *msg;
  114. struct stasis_message_type *type;
  115. struct ast_endpoint_snapshot *actual_snapshot;
  116. struct stasis_cache_update *update;
  117. int message_index;
  118. switch (cmd) {
  119. case TEST_INIT:
  120. info->name = __func__;
  121. info->category = test_category;
  122. info->summary = "Test endpoint state change messages";
  123. info->description = "Test endpoint state change messages";
  124. return AST_TEST_NOT_RUN;
  125. case TEST_EXECUTE:
  126. break;
  127. }
  128. /* Subscribe to the cache topic */
  129. sink = stasis_message_sink_create();
  130. ast_test_validate(test, NULL != sink);
  131. sub = stasis_subscribe(
  132. ast_endpoint_topic_all_cached(),
  133. stasis_message_sink_cb(), sink);
  134. ast_test_validate(test, NULL != sub);
  135. uut = ast_endpoint_create("TEST", __func__);
  136. ast_test_validate(test, NULL != uut);
  137. /* Since the cache topic is a singleton (ew), it may have messages from
  138. * elsewheres that it's processing, or maybe even some final messages
  139. * from the prior test. We've got to wait_for our specific message,
  140. * instead of wait_for_count.
  141. */
  142. message_index = stasis_message_sink_wait_for(sink, 0,
  143. cache_update, __func__, STASIS_SINK_DEFAULT_WAIT);
  144. ast_test_validate(test, 0 <= message_index);
  145. /* First message should be a cache creation entry for our endpoint */
  146. msg = sink->messages[message_index];
  147. type = stasis_message_type(msg);
  148. ast_test_validate(test, stasis_cache_update_type() == type);
  149. update = stasis_message_data(msg);
  150. ast_test_validate(test, ast_endpoint_snapshot_type() == update->type);
  151. ast_test_validate(test, NULL == update->old_snapshot);
  152. actual_snapshot = stasis_message_data(update->new_snapshot);
  153. ast_test_validate(test, 0 == strcmp("TEST", actual_snapshot->tech));
  154. ast_test_validate(test,
  155. 0 == strcmp(__func__, actual_snapshot->resource));
  156. ast_endpoint_shutdown(uut);
  157. uut = NULL;
  158. /* Note: there's a few messages between the creation and the clear.
  159. * Wait for all of them... */
  160. message_index = stasis_message_sink_wait_for(sink, message_index + 2,
  161. cache_update, __func__, STASIS_SINK_DEFAULT_WAIT);
  162. ast_test_validate(test, 0 <= message_index);
  163. /* Now we should have a cache removal entry */
  164. msg = sink->messages[message_index];
  165. type = stasis_message_type(msg);
  166. ast_test_validate(test, stasis_cache_update_type() == type);
  167. update = stasis_message_data(msg);
  168. ast_test_validate(test, ast_endpoint_snapshot_type() == update->type);
  169. actual_snapshot = stasis_message_data(update->old_snapshot);
  170. ast_test_validate(test, 0 == strcmp("TEST", actual_snapshot->tech));
  171. ast_test_validate(test,
  172. 0 == strcmp(__func__, actual_snapshot->resource));
  173. ast_test_validate(test, NULL == update->new_snapshot);
  174. return AST_TEST_PASS;
  175. }
  176. AST_TEST_DEFINE(channel_messages)
  177. {
  178. RAII_VAR(struct ast_endpoint *, uut, NULL, ast_endpoint_shutdown);
  179. RAII_VAR(struct ast_channel *, chan, NULL, ast_hangup);
  180. RAII_VAR(struct stasis_message_sink *, sink, NULL, ao2_cleanup);
  181. RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe);
  182. struct stasis_message *msg;
  183. struct stasis_message_type *type;
  184. struct ast_endpoint_snapshot *actual_snapshot;
  185. int actual_count;
  186. switch (cmd) {
  187. case TEST_INIT:
  188. info->name = __func__;
  189. info->category = test_category;
  190. info->summary = "Test channel messages on an endpoint topic";
  191. info->description =
  192. "Test channel messages on an endpoint topic";
  193. return AST_TEST_NOT_RUN;
  194. case TEST_EXECUTE:
  195. break;
  196. }
  197. uut = ast_endpoint_create("TEST", __func__);
  198. ast_test_validate(test, NULL != uut);
  199. sink = stasis_message_sink_create();
  200. ast_test_validate(test, NULL != sink);
  201. sub = stasis_subscribe(ast_endpoint_topic(uut),
  202. stasis_message_sink_cb(), sink);
  203. ast_test_validate(test, NULL != sub);
  204. chan = ast_channel_alloc(0, AST_STATE_DOWN, "100", __func__, "100",
  205. "100", "default", NULL, NULL, 0, "TEST/test_res");
  206. ast_test_validate(test, NULL != chan);
  207. ast_endpoint_add_channel(uut, chan);
  208. actual_count = stasis_message_sink_wait_for_count(sink, 1,
  209. STASIS_SINK_DEFAULT_WAIT);
  210. ast_test_validate(test, 1 == actual_count);
  211. msg = sink->messages[0];
  212. type = stasis_message_type(msg);
  213. ast_test_validate(test, ast_endpoint_snapshot_type() == type);
  214. actual_snapshot = stasis_message_data(msg);
  215. ast_test_validate(test, 1 == actual_snapshot->num_channels);
  216. ast_hangup(chan);
  217. chan = NULL;
  218. actual_count = stasis_message_sink_wait_for_count(sink, 6,
  219. STASIS_SINK_DEFAULT_WAIT);
  220. ast_test_validate(test, 6 == actual_count);
  221. msg = sink->messages[1];
  222. type = stasis_message_type(msg);
  223. ast_test_validate(test, stasis_cache_update_type() == type);
  224. msg = sink->messages[2];
  225. type = stasis_message_type(msg);
  226. ast_test_validate(test, ast_channel_snapshot_type() == type);
  227. msg = sink->messages[3];
  228. type = stasis_message_type(msg);
  229. ast_test_validate(test, stasis_cache_update_type() == type);
  230. /* The ordering of the cache clear and endpoint snapshot are
  231. * unspecified */
  232. msg = sink->messages[4];
  233. if (stasis_message_type(msg) == stasis_cache_clear_type()) {
  234. /* Okay; the next message should be the endpoint snapshot */
  235. msg = sink->messages[5];
  236. }
  237. type = stasis_message_type(msg);
  238. ast_test_validate(test, ast_endpoint_snapshot_type() == type);
  239. actual_snapshot = stasis_message_data(msg);
  240. ast_test_validate(test, 0 == actual_snapshot->num_channels);
  241. return AST_TEST_PASS;
  242. }
  243. static int unload_module(void)
  244. {
  245. AST_TEST_UNREGISTER(state_changes);
  246. AST_TEST_UNREGISTER(cache_clear);
  247. AST_TEST_UNREGISTER(channel_messages);
  248. return 0;
  249. }
  250. static int load_module(void)
  251. {
  252. AST_TEST_REGISTER(state_changes);
  253. AST_TEST_REGISTER(cache_clear);
  254. AST_TEST_REGISTER(channel_messages);
  255. return AST_MODULE_LOAD_SUCCESS;
  256. }
  257. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Endpoint stasis-related testing",
  258. .support_level = AST_MODULE_SUPPORT_CORE,
  259. .load = load_module,
  260. .unload = unload_module,
  261. );