tokyocabinet_test.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. #include "gear_config.h"
  39. #include <libtest/test.hpp>
  40. using namespace libtest;
  41. #include <cassert>
  42. #include <cstdio>
  43. #include <cstdlib>
  44. #include <cstring>
  45. #include <unistd.h>
  46. #include <libgearman/gearman.h>
  47. #include "tests/basic.h"
  48. #include "tests/context.h"
  49. #include "libgearman/client.hpp"
  50. #include "libgearman/worker.hpp"
  51. using namespace org::gearmand;
  52. #include "tests/workers/v2/called.h"
  53. #ifndef __INTEL_COMPILER
  54. #pragma GCC diagnostic ignored "-Wold-style-cast"
  55. #endif
  56. static test_return_t gearmand_basic_option_test(void *)
  57. {
  58. const char *args[]= { "--check-args",
  59. "--queue-type=libtokyocabinet",
  60. "--libtokyocabinet-file=var/tmp/gearman_basic.tcb",
  61. "--libtokyocabinet-optimize",
  62. 0 };
  63. unlink("var/tmp/gearman.tcb");
  64. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline(gearmand_binary(), args, true));
  65. return TEST_SUCCESS;
  66. }
  67. static test_return_t collection_init(void *object)
  68. {
  69. const char *argv[]= {
  70. "--libtokyocabinet-file=var/tmp/gearman.tcb",
  71. "--queue-type=libtokyocabinet",
  72. 0 };
  73. unlink("var/tmp/gearman.tcb");
  74. Context *test= (Context *)object;
  75. assert(test);
  76. ASSERT_TRUE(test->initialize(argv));
  77. return TEST_SUCCESS;
  78. }
  79. static test_return_t lp_1054377_TEST(void *object)
  80. {
  81. Context *test= (Context *)object;
  82. ASSERT_TRUE(test);
  83. server_startup_st &servers= test->_servers;
  84. unlink("var/tmp/gearman.tcb");
  85. const char *argv[]= {
  86. "--libtokyocabinet-file=var/tmp/gearman.tcb",
  87. "--queue-type=libtokyocabinet",
  88. 0 };
  89. const int32_t inserted_jobs= 8;
  90. {
  91. in_port_t first_port= libtest::get_free_port();
  92. ASSERT_TRUE(server_startup(servers, "gearmand", first_port, argv));
  93. {
  94. libgearman::Worker worker(first_port);
  95. ASSERT_EQ(gearman_worker_register(&worker, __func__, 0), GEARMAN_SUCCESS);
  96. }
  97. {
  98. libgearman::Client client(first_port);
  99. ASSERT_EQ(gearman_client_echo(&client, test_literal_param("This is my echo test")), GEARMAN_SUCCESS);
  100. gearman_job_handle_t job_handle;
  101. for (int32_t x= 0; x < inserted_jobs; ++x)
  102. {
  103. ASSERT_EQ(gearman_client_do_background(&client,
  104. __func__, // func
  105. NULL, // unique
  106. test_literal_param("foo"),
  107. job_handle), GEARMAN_SUCCESS);
  108. }
  109. }
  110. servers.clear();
  111. }
  112. {
  113. in_port_t first_port= libtest::get_free_port();
  114. ASSERT_TRUE(server_startup(servers, "gearmand", first_port, argv));
  115. {
  116. libgearman::Worker worker(first_port);
  117. Called called;
  118. gearman_function_t counter_function= gearman_function_create(called_worker);
  119. ASSERT_EQ(gearman_worker_define_function(&worker,
  120. test_literal_param(__func__),
  121. counter_function,
  122. 3000, &called), GEARMAN_SUCCESS);
  123. const int32_t max_timeout= 4;
  124. int32_t max_timeout_value= max_timeout;
  125. int32_t job_count= 0;
  126. gearman_return_t ret;
  127. do
  128. {
  129. ret= gearman_worker_work(&worker);
  130. if (gearman_success(ret))
  131. {
  132. job_count++;
  133. max_timeout_value= max_timeout;
  134. if (job_count == inserted_jobs)
  135. {
  136. break;
  137. }
  138. }
  139. else if (ret == GEARMAN_TIMEOUT)
  140. {
  141. if ((--max_timeout_value) < 0)
  142. {
  143. break;
  144. }
  145. }
  146. } while (ret == GEARMAN_TIMEOUT or ret == GEARMAN_SUCCESS);
  147. ASSERT_EQ(called.count(), inserted_jobs);
  148. }
  149. }
  150. unlink("var/tmp/gearman.tcb");
  151. return TEST_SUCCESS;
  152. }
  153. static test_return_t collection_cleanup(void *object)
  154. {
  155. Context *test= (Context *)object;
  156. test->reset();
  157. unlink("var/tmp/gearman.tcb");
  158. return TEST_SUCCESS;
  159. }
  160. static void *world_create(server_startup_st& servers, test_return_t&)
  161. {
  162. SKIP_IF(HAVE_UUID_UUID_H != 1);
  163. SKIP_IF(HAVE_TOKYOCABINET != 1);
  164. unlink("var/tmp/gearman.tcb");
  165. return new Context(servers);
  166. }
  167. static bool world_destroy(void *object)
  168. {
  169. Context *test= (Context *)object;
  170. unlink("var/tmp/gearman.tcb");
  171. delete test;
  172. return TEST_SUCCESS;
  173. }
  174. test_st gearmand_basic_option_tests[] ={
  175. {"--libtokyocabinet-file=var/tmp/gearman_basic.tcb --libtokyocabinet-optimize", 0, gearmand_basic_option_test },
  176. {0, 0, 0}
  177. };
  178. test_st tests[] ={
  179. {"gearman_client_echo()", 0, client_echo_test },
  180. {"gearman_client_echo() fail", 0, client_echo_fail_test },
  181. {"gearman_worker_echo()", 0, worker_echo_test },
  182. {"clean", 0, queue_clean },
  183. {"add", 0, queue_add },
  184. {"worker", 0, queue_worker },
  185. {0, 0, 0}
  186. };
  187. test_st queue_restart_TESTS[] ={
  188. {"lp:1054377", 0, lp_1054377_TEST },
  189. {0, 0, 0}
  190. };
  191. collection_st collection[] ={
  192. {"gearmand options", 0, 0, gearmand_basic_option_tests},
  193. {"tokyocabinet queue", collection_init, collection_cleanup, tests},
  194. {"queue restart", 0, 0, queue_restart_TESTS},
  195. {0, 0, 0, 0}
  196. };
  197. void get_world(libtest::Framework *world)
  198. {
  199. world->collections(collection);
  200. world->create(world_create);
  201. world->destroy(world_destroy);
  202. }