2
0

hmanager.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2018 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. #define now() (ks_time_ms(ks_time_now()))
  24. /* Define our context */
  25. typedef struct test_ctx_s {
  26. swclt_handle_base_t base;
  27. ks_time_t create_time;
  28. ks_time_t service_time;
  29. ks_time_t retry_time;
  30. ks_time_t retry_try_1_time;
  31. ks_cond_t *cond;
  32. } test_ctx_t;
  33. static ks_status_t __context_state_transition(test_ctx_t *ctx, SWCLT_HSTATE new_state)
  34. {
  35. REQUIRE(new_state == SWCLT_HSTATE_ONLINE);
  36. if (ctx->service_time == 0) {
  37. /* Lets give it a max of 500ms for the assert */
  38. REQUIRE((now() - ctx->create_time) <= 500);
  39. ctx->service_time = now();
  40. /* Now fail the transition, should retry in ~1 seconds */
  41. return KS_STATUS_FAIL;
  42. } else if (ctx->retry_time == 0) {
  43. /* Should've servied us within 1.5 seconds */
  44. REQUIRE((now() - ctx->service_time) <= 1500);
  45. ctx->retry_time = now();
  46. /* Fail it again */
  47. return KS_STATUS_FAIL;
  48. } else if (ctx->retry_try_1_time == 0) {
  49. /* Same check, should've servied us within 1.5 seconds */
  50. REQUIRE((now() - ctx->retry_time) <= 1500);
  51. ctx->retry_try_1_time = now();
  52. /* Ok test complete, signal */
  53. ks_cond_lock(ctx->cond);
  54. ks_cond_broadcast(ctx->cond);
  55. ks_cond_unlock(ctx->cond);
  56. return KS_STATUS_SUCCESS;
  57. }
  58. FAIL("Should not have gotten here");
  59. }
  60. static ks_status_t __context_init(test_ctx_t *ctx, ks_cond_t *cond)
  61. {
  62. ctx->cond = cond;
  63. REQUIRE(ctx->base.state == SWCLT_HSTATE_NORMAL);
  64. REQUIRE(ctx->base.last_state == SWCLT_HSTATE_NORMAL);
  65. ctx->create_time = now();
  66. /* Alright requet service right away */
  67. swclt_hstate_initiate_change_now(
  68. &ctx->base,
  69. SWCLT_HSTATE_ONLINE,
  70. __context_state_transition,
  71. 1000); /* retry duration */
  72. return KS_STATUS_SUCCESS;
  73. }
  74. static void __context_describe(test_ctx_t *ctx, char *buffer, ks_size_t buffer_len)
  75. {
  76. snprintf(buffer, buffer_len, "Unit test yo");
  77. }
  78. static void __context_deinit(test_ctx_t *ctx)
  79. {
  80. }
  81. static ks_status_t allocate(ks_handle_t *handle, ks_cond_t *cond)
  82. {
  83. SWCLT_HANDLE_ALLOC_TEMPLATE_M(
  84. NULL,
  85. SWCLT_HTYPE_TEST,
  86. handle,
  87. test_ctx_t,
  88. SWCLT_HSTATE_NORMAL,
  89. __context_describe,
  90. __context_deinit,
  91. __context_init,
  92. cond);
  93. }
  94. void test_hmanager(ks_pool_t *pool)
  95. {
  96. ks_cond_t *cond;
  97. REQUIRE(!ks_cond_create(&cond, NULL));
  98. REQUIRE(!ks_cond_lock(cond));
  99. /* Create our own handle and test the signalwire client manager's */
  100. ks_handle_t handle;
  101. REQUIRE(!allocate(&handle, cond));
  102. /* Now wait for the test to end, should not take more hten 5 seconds, if so fail */
  103. if (ks_cond_timedwait(cond, 5000) == KS_STATUS_TIMEOUT)
  104. ks_abort("Test took too long");
  105. /* Success destroy it */
  106. ks_cond_unlock(cond);
  107. ks_handle_destroy(&handle);
  108. ks_cond_destroy(&cond);
  109. }