connection.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2018-2020 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "swclt_test.h"
  23. #include "../../src/connection.c"
  24. static uint32_t g_protocol_response_cb_called;
  25. static ks_status_t __on_incoming_cmd(swclt_conn_t *conn, swclt_cmd_t *cmd, void *cb_data)
  26. {
  27. printf("ON INCOMING COMMAND\n");
  28. return KS_STATUS_SUCCESS;
  29. }
  30. static void __on_protocol_timeout_response(swclt_cmd_reply_t *reply, void *cb_data)
  31. {
  32. REQUIRE(reply);
  33. REQUIRE(reply->type == SWCLT_CMD_TYPE_FAILURE);
  34. REQUIRE(reply->failure_reason);
  35. REQUIRE(reply->failure_status == KS_STATUS_TIMEOUT);
  36. printf("Validated failure code, message: %s\n", reply->failure_reason);
  37. swclt_cmd_reply_destroy(&reply);
  38. g_protocol_response_cb_called++;
  39. }
  40. static void __on_protocol_result_response(swclt_cmd_reply_t *reply, void *cb_data)
  41. {
  42. REQUIRE(swclt_cmd_reply_ok(reply) == KS_STATUS_SUCCESS);
  43. swclt_cmd_reply_destroy(&reply);
  44. g_protocol_response_cb_called++;
  45. }
  46. void test_async(ks_pool_t *pool)
  47. {
  48. swclt_cmd_t *cmd;
  49. SSL_CTX *ssl = create_ssl_context();
  50. swclt_conn_t *conn;
  51. ks_json_t *channels;
  52. int i;
  53. REQUIRE(!swclt_conn_connect(&conn, __on_incoming_cmd, NULL, &g_target_ident, NULL, NULL, NULL, NULL, ssl));
  54. channels = ks_json_create_array();
  55. ks_json_add_item_to_array(channels, BLADE_CHANNEL_MARSHAL(&(blade_channel_t){"a_channel", 0, 0}));
  56. /* Create an async command (bogus command but will generate a reply at least) */
  57. REQUIRE(cmd = CREATE_BLADE_PROTOCOL_PROVIDER_ADD_CMD_ASYNC(
  58. __on_protocol_result_response,
  59. NULL,
  60. "a_protocol",
  61. 0,
  62. 0,
  63. 0,
  64. NULL,
  65. &channels,
  66. 1,
  67. NULL));
  68. /* And submit it */
  69. REQUIRE(!swclt_conn_submit_request(conn, &cmd, NULL));
  70. /* Wait for it to respond */
  71. for (i = 0; i < 5 && g_protocol_response_cb_called == 0; i++) {
  72. ks_sleep_ms(1000);
  73. }
  74. REQUIRE(g_protocol_response_cb_called == 1);
  75. swclt_cmd_destroy(&cmd);
  76. swclt_conn_destroy(&conn);
  77. swclt_ssl_destroy_context(&ssl);
  78. }
  79. void test_ttl(ks_pool_t *pool)
  80. {
  81. SSL_CTX *ssl = create_ssl_context();
  82. swclt_conn_t *conn;
  83. swclt_cmd_t *cmd;
  84. SWCLT_CMD_TYPE cmd_type;
  85. ks_json_t *channels;
  86. int i;
  87. g_protocol_response_cb_called = 0;
  88. REQUIRE(!swclt_conn_connect(&conn, __on_incoming_cmd, NULL, &g_target_ident, NULL, NULL, NULL, NULL, ssl));
  89. channels = ks_json_create_array();
  90. ks_json_add_item_to_array(channels, BLADE_CHANNEL_MARSHAL(&(blade_channel_t){"b_channel", 0, 0}));
  91. REQUIRE(cmd = CREATE_BLADE_PROTOCOL_PROVIDER_ADD_CMD_ASYNC(
  92. __on_protocol_timeout_response,
  93. NULL,
  94. "b_protocol",
  95. 0,
  96. 0,
  97. 0,
  98. NULL,
  99. &channels,
  100. 1,
  101. NULL));
  102. /* Lock the reader so we never get a response, forcing a timeout */
  103. REQUIRE(cmd->response_ttl_ms == BLADE_PROTOCOL_TTL_MS);
  104. REQUIRE(cmd->flags == BLADE_PROTOCOL_FLAGS);
  105. REQUIRE(!ks_mutex_lock(conn->wss->wss_mutex));
  106. /* And submit it */
  107. REQUIRE(!swclt_conn_submit_request(conn, &cmd, NULL));
  108. /* Wait for it to respond */
  109. for (i = 0; i < 7 && g_protocol_response_cb_called == 0; i++) {
  110. ks_sleep_ms(1000);
  111. }
  112. REQUIRE(g_protocol_response_cb_called == 1);
  113. /* Don't forget to unlock the poor websocket reader */
  114. REQUIRE(!ks_mutex_unlock(conn->wss->wss_mutex));
  115. swclt_cmd_destroy(&cmd);
  116. swclt_conn_destroy(&conn);
  117. swclt_ssl_destroy_context(&ssl);
  118. }
  119. void test_ttl_heap(ks_pool_t *pool)
  120. {
  121. swclt_ttl_tracker_t *ttl = NULL;
  122. ttl_tracker_create(pool, &ttl, NULL);
  123. if (ttl->thread) {
  124. ks_thread_request_stop(ttl->thread);
  125. ks_thread_join(ttl->thread);
  126. ks_thread_destroy(&ttl->thread); // don't want it
  127. }
  128. int i;
  129. int min = INT_MAX;
  130. for (i = 0; i < 100; i++) {
  131. ks_uuid_t uuid = { 0 };
  132. ks_uuid(&uuid);
  133. int expiry = rand();
  134. if (expiry < min) {
  135. min = expiry;
  136. }
  137. if (ttl_heap_insert(ttl, expiry, uuid) != KS_STATUS_SUCCESS) {
  138. printf("Failed to insert UUID = %s, TTL = %d, min = %d, root = %d, count = %d\n", ks_uuid_thr_str(&uuid), expiry, min, ttl->heap[TTL_HEAP_ROOT].expiry, ttl->count);
  139. } else {
  140. printf("Insert UUID = %s, TTL = %d, min = %d, root = %d, count = %d\n", ks_uuid_thr_str(&uuid), expiry, min, ttl->heap[TTL_HEAP_ROOT].expiry, ttl->count);
  141. }
  142. REQUIRE(ttl->heap[TTL_HEAP_ROOT].expiry == min);
  143. }
  144. min = 0;
  145. while (ttl->count) {
  146. REQUIRE(ttl->heap[TTL_HEAP_ROOT].expiry > 0);
  147. printf("Remove UUID = %s, TTL = %d, count = %d\n", ks_uuid_thr_str(&ttl->heap[TTL_HEAP_ROOT].id), ttl->heap[TTL_HEAP_ROOT].expiry, ttl->count);
  148. REQUIRE(ttl->heap[TTL_HEAP_ROOT].expiry >= min);
  149. min = ttl->heap[TTL_HEAP_ROOT].expiry;
  150. ttl_heap_remove(ttl);
  151. }
  152. ttl_tracker_destroy(&ttl);
  153. }
  154. void test_connection(ks_pool_t *pool)
  155. {
  156. test_async(pool);
  157. test_ttl(pool);
  158. test_ttl_heap(pool);
  159. }