memcached_test.cc 8.7 KB

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