test_res_stasis.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 Stasis Application API.
  20. * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
  21. *
  22. * \ingroup tests
  23. */
  24. /*** MODULEINFO
  25. <depend>TEST_FRAMEWORK</depend>
  26. <depend>res_stasis</depend>
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/module.h"
  32. #include "asterisk/test.h"
  33. #include "asterisk/stasis_app.h"
  34. static const char *test_category = "/stasis/res/";
  35. AST_TEST_DEFINE(app_invoke_dne)
  36. {
  37. int res;
  38. switch (cmd) {
  39. case TEST_INIT:
  40. info->name = __func__;
  41. info->category = test_category;
  42. info->summary = "Test stasis app invocation.";
  43. info->description = "Test stasis app invocation.";
  44. return AST_TEST_NOT_RUN;
  45. case TEST_EXECUTE:
  46. break;
  47. }
  48. res = stasis_app_send("i-am-not-an-app", ast_json_null());
  49. ast_test_validate(test, -1 == res);
  50. return AST_TEST_PASS;
  51. }
  52. struct app_data {
  53. int invocations;
  54. struct ast_json *messages;
  55. };
  56. static void app_data_dtor(void *obj)
  57. {
  58. struct app_data *actual = obj;
  59. ast_json_unref(actual->messages);
  60. actual->messages = NULL;
  61. }
  62. static struct app_data *app_data_create(void)
  63. {
  64. struct app_data *res = ao2_alloc(sizeof(struct app_data), app_data_dtor);
  65. if (!res) {
  66. return NULL;
  67. }
  68. res->messages = ast_json_array_create();
  69. return res;
  70. }
  71. static void test_handler(void *data, const char *app_name, struct ast_json *message)
  72. {
  73. struct app_data *actual = data;
  74. int res;
  75. ++(actual->invocations);
  76. res = ast_json_array_append(actual->messages, ast_json_copy(message));
  77. ast_assert(res == 0);
  78. }
  79. AST_TEST_DEFINE(app_invoke_one)
  80. {
  81. RAII_VAR(struct app_data *, app_data, NULL, ao2_cleanup);
  82. RAII_VAR(char *, app_name, NULL, stasis_app_unregister);
  83. RAII_VAR(struct ast_json *, expected_message, NULL, ast_json_unref);
  84. RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
  85. int res;
  86. switch (cmd) {
  87. case TEST_INIT:
  88. info->name = __func__;
  89. info->category = test_category;
  90. info->summary = "Test stasis app invocation.";
  91. info->description = "Test stasis app invocation.";
  92. return AST_TEST_NOT_RUN;
  93. case TEST_EXECUTE:
  94. break;
  95. }
  96. app_name = "test-handler";
  97. app_data = app_data_create();
  98. stasis_app_register(app_name, test_handler, app_data);
  99. message = ast_json_pack("{ s: o }", "test-message", ast_json_null());
  100. expected_message = ast_json_pack("[o]", ast_json_ref(message));
  101. res = stasis_app_send(app_name, message);
  102. ast_test_validate(test, 0 == res);
  103. ast_test_validate(test, 1 == app_data->invocations);
  104. ast_test_validate(test, ast_json_equal(expected_message, app_data->messages));
  105. return AST_TEST_PASS;
  106. }
  107. AST_TEST_DEFINE(app_replaced)
  108. {
  109. RAII_VAR(struct app_data *, app_data1, NULL, ao2_cleanup);
  110. RAII_VAR(struct app_data *, app_data2, NULL, ao2_cleanup);
  111. RAII_VAR(char *, app_name, NULL, stasis_app_unregister);
  112. RAII_VAR(struct ast_json *, expected_message1, NULL, ast_json_unref);
  113. RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
  114. RAII_VAR(struct ast_json *, expected_message2, NULL, ast_json_unref);
  115. char eid[20];
  116. int res;
  117. switch (cmd) {
  118. case TEST_INIT:
  119. info->name = __func__;
  120. info->category = test_category;
  121. info->summary = "Test stasis app invocation.";
  122. info->description = "Test stasis app invocation.";
  123. return AST_TEST_NOT_RUN;
  124. case TEST_EXECUTE:
  125. break;
  126. }
  127. app_name = "test-handler";
  128. app_data1 = app_data_create();
  129. app_data2 = app_data_create();
  130. stasis_app_register(app_name, test_handler, app_data1);
  131. stasis_app_register(app_name, test_handler, app_data2);
  132. expected_message1 = ast_json_pack("[{s: s, s: s, s: s}]",
  133. "type", "ApplicationReplaced",
  134. "application", app_name,
  135. "asterisk_id", ast_eid_to_str(eid, sizeof(eid), &ast_eid_default));
  136. message = ast_json_pack("{ s: o }", "test-message", ast_json_null());
  137. expected_message2 = ast_json_pack("[o]", ast_json_ref(message));
  138. res = stasis_app_send(app_name, message);
  139. ast_test_validate(test, 0 == res);
  140. ast_test_validate(test, 1 == app_data1->invocations);
  141. ast_test_validate(test, ast_json_equal(expected_message1, app_data1->messages));
  142. ast_test_validate(test, 1 == app_data2->invocations);
  143. ast_test_validate(test, ast_json_equal(expected_message2, app_data2->messages));
  144. return AST_TEST_PASS;
  145. }
  146. static int unload_module(void)
  147. {
  148. AST_TEST_UNREGISTER(app_invoke_dne);
  149. AST_TEST_UNREGISTER(app_invoke_one);
  150. AST_TEST_UNREGISTER(app_replaced);
  151. stasis_app_unref();
  152. return 0;
  153. }
  154. static int load_module(void)
  155. {
  156. stasis_app_ref();
  157. AST_TEST_REGISTER(app_replaced);
  158. AST_TEST_REGISTER(app_invoke_one);
  159. AST_TEST_REGISTER(app_invoke_dne);
  160. return AST_MODULE_LOAD_SUCCESS;
  161. }
  162. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Stasis Core testing",
  163. .support_level = AST_MODULE_SUPPORT_CORE,
  164. .load = load_module,
  165. .unload = unload_module,
  166. );