cycle.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Cycle the Gearmand server
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. /*
  37. Test that we are cycling the servers we are creating during testing.
  38. */
  39. #include "gear_config.h"
  40. #include <libtest/test.hpp>
  41. using namespace libtest;
  42. #include <libgearman/gearman.h>
  43. #include "tests/start_worker.h"
  44. struct cycle_context_st
  45. {
  46. cycle_context_st(libtest::server_startup_st& arg) :
  47. servers(arg),
  48. port(0)
  49. {
  50. reset();
  51. }
  52. void reset()
  53. {
  54. servers.clear();
  55. port= get_free_port();
  56. }
  57. server_startup_st& servers;
  58. in_port_t port;
  59. };
  60. static gearman_return_t success_fn(gearman_job_st*, void* /* context */)
  61. {
  62. return GEARMAN_SUCCESS;
  63. }
  64. static test_return_t single_cycle(void* object)
  65. {
  66. cycle_context_st *context= (cycle_context_st*)object;
  67. gearman_function_t success_function= gearman_function_create(success_fn);
  68. worker_handle_st *worker= test_worker_start(context->port, NULL, "success", success_function, NULL, gearman_worker_options_t());
  69. ASSERT_TRUE(worker);
  70. ASSERT_TRUE(worker->shutdown());
  71. delete worker;
  72. return TEST_SUCCESS;
  73. }
  74. static test_return_t kill_test(void *)
  75. {
  76. libtest::dream(2, 0);
  77. return TEST_SUCCESS;
  78. }
  79. static test_return_t __server_startup_TEST(cycle_context_st* context, const int count)
  80. {
  81. for (int x= 0; x < count; ++x)
  82. {
  83. test_skip(true, server_startup(context->servers, "gearmand", libtest::get_free_port(), NULL));
  84. }
  85. ASSERT_EQ(true, context->servers.shutdown());
  86. return TEST_SUCCESS;
  87. }
  88. static test_return_t server_startup_single_TEST(void *obj)
  89. {
  90. ASSERT_EQ(__server_startup_TEST((cycle_context_st*)obj, 1), TEST_SUCCESS);
  91. return TEST_SUCCESS;
  92. }
  93. static test_return_t server_startup_multiple_TEST(void *obj)
  94. {
  95. ASSERT_EQ(__server_startup_TEST((cycle_context_st*)obj, 20), TEST_SUCCESS);
  96. return TEST_SUCCESS;
  97. }
  98. // We can't really test for this just yet, because we don't know if the server
  99. // we attach to is really the one we expect to attach too.
  100. static test_return_t server_startup_conflict_TEST(void*)
  101. {
  102. #if 0
  103. cycle_context_st *context= (cycle_context_st*)object;
  104. in_port_t bind_port= libtest::get_free_port();
  105. ASSERT_EQ(true, server_startup(context->servers, "gearmand", bind_port, NULL, false));
  106. ASSERT_EQ(false, server_startup(context->servers, "gearmand", bind_port, NULL, false));
  107. #endif
  108. return TEST_SUCCESS;
  109. }
  110. static test_return_t shutdown_and_remove_TEST(void *obj)
  111. {
  112. cycle_context_st *context= (cycle_context_st*)obj;
  113. context->servers.clear();
  114. return TEST_SUCCESS;
  115. }
  116. test_st server_startup_TESTS[] ={
  117. {"server_startup(1)", false, (test_callback_fn*)server_startup_single_TEST },
  118. {"server_startup(many)", false, (test_callback_fn*)server_startup_multiple_TEST },
  119. {"shutdown_and_remove()", false, (test_callback_fn*)shutdown_and_remove_TEST },
  120. {"server_startup(many)", false, (test_callback_fn*)server_startup_multiple_TEST },
  121. {"server_startup() with bind() conflict", false, (test_callback_fn*)server_startup_conflict_TEST },
  122. {0, 0, 0}
  123. };
  124. test_st kill_tests[] ={
  125. {"kill", true, (test_callback_fn*)kill_test },
  126. {0, 0, 0}
  127. };
  128. test_st worker_tests[] ={
  129. {"single startup/shutdown", true, (test_callback_fn*)single_cycle },
  130. {0, 0, 0}
  131. };
  132. static test_return_t collection_INIT(void *object)
  133. {
  134. cycle_context_st *context= (cycle_context_st*)object;
  135. test_zero(context->servers.count());
  136. context->reset();
  137. test_skip(true, server_startup(context->servers, "gearmand", context->port, NULL));
  138. return TEST_SUCCESS;
  139. }
  140. static test_return_t validate_sanity_INIT(void *object)
  141. {
  142. cycle_context_st *context= (cycle_context_st*)object;
  143. test_zero(context->servers.count());
  144. context->reset();
  145. return TEST_SUCCESS;
  146. }
  147. static test_return_t collection_FINAL(void *object)
  148. {
  149. cycle_context_st *context= (cycle_context_st*)object;
  150. context->reset();
  151. return TEST_SUCCESS;
  152. }
  153. collection_st collection[] ={
  154. {"kill", validate_sanity_INIT, collection_FINAL, kill_tests },
  155. {"worker", collection_INIT, collection_FINAL, worker_tests },
  156. {"server_startup()", validate_sanity_INIT, collection_FINAL, server_startup_TESTS },
  157. {0, 0, 0, 0}
  158. };
  159. static void *world_create(server_startup_st& servers, test_return_t& error)
  160. {
  161. if (jenkins_is_caller())
  162. {
  163. error= TEST_SKIPPED;
  164. return NULL;
  165. }
  166. return new cycle_context_st(servers);
  167. }
  168. static bool world_destroy(void *object)
  169. {
  170. cycle_context_st *context= (cycle_context_st*)object;
  171. delete context;
  172. return TEST_SUCCESS;
  173. }
  174. void get_world(libtest::Framework *world)
  175. {
  176. world->collections(collection);
  177. world->create(world_create);
  178. world->destroy(world_destroy);
  179. }