cli.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 "libgearman/client.hpp"
  41. #include "libgearman/worker.hpp"
  42. using namespace org::gearmand;
  43. #include <libtest/test.hpp>
  44. using namespace libtest;
  45. #include <libgearman/gearman.h>
  46. #ifndef __INTEL_COMPILER
  47. #pragma GCC diagnostic ignored "-Wstrict-aliasing"
  48. #endif
  49. #include <tests/start_worker.h>
  50. #include "tests/workers/v2/echo_or_react.h"
  51. #define WORKER_FUNCTION_NAME "echo_function"
  52. namespace cli {
  53. struct Context
  54. {
  55. server_startup_st& servers;
  56. Context(server_startup_st& servers_arg) :
  57. servers(servers_arg)
  58. { }
  59. void push(worker_handle_st *worker_arg)
  60. {
  61. _workers.push_back(worker_arg);
  62. }
  63. void shutdown_workers()
  64. {
  65. for (std::vector<worker_handle_st *>::iterator iter= _workers.begin(); iter != _workers.end(); ++iter)
  66. {
  67. delete *iter;
  68. }
  69. _workers.clear();
  70. }
  71. void clear()
  72. {
  73. shutdown_workers();
  74. servers.clear();
  75. }
  76. void port(in_port_t port_)
  77. {
  78. _port= port_;
  79. }
  80. in_port_t port() const
  81. {
  82. return _port;
  83. }
  84. ~Context()
  85. {
  86. clear();
  87. }
  88. private:
  89. std::vector<worker_handle_st *>_workers;
  90. in_port_t _port;
  91. };
  92. }
  93. static test_return_t gearman_help_test(void *)
  94. {
  95. const char *args[]= { "-H", 0 };
  96. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearman", args, true));
  97. return TEST_SUCCESS;
  98. }
  99. static test_return_t gearman_verbose_TEST(void *)
  100. {
  101. const char *args[]= { "-H", "-v", 0 };
  102. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearman", args, true));
  103. return TEST_SUCCESS;
  104. }
  105. static test_return_t gearman_unknown_test(void* object)
  106. {
  107. cli::Context *context= (cli::Context*)object;
  108. char buffer[1024];
  109. snprintf(buffer, sizeof(buffer), "-p %d", int(context->port()));
  110. const char *args[]= { buffer, "--unknown", 0 };
  111. // The argument doesn't exist, so we should see an error
  112. ASSERT_EQ(EXIT_FAILURE, exec_cmdline("bin/gearman", args, true));
  113. return TEST_SUCCESS;
  114. }
  115. static test_return_t gearman_client_background_test(void* object)
  116. {
  117. cli::Context *context= (cli::Context*)object;
  118. char buffer[1024];
  119. snprintf(buffer, sizeof(buffer), "-p %d", int(context->port()));
  120. const char *args[]= { buffer, "-f", WORKER_FUNCTION_NAME, "-b", "payload", 0 };
  121. // The argument doesn't exist, so we should see an error
  122. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearman", args, true));
  123. return TEST_SUCCESS;
  124. }
  125. #define REGRESSION_FUNCTION_833394 "55_char_function_name_________________________________"
  126. static test_return_t regression_833394_test(void* object)
  127. {
  128. cli::Context *context= (cli::Context*)object;
  129. char buffer[1024];
  130. snprintf(buffer, sizeof(buffer), "-p %d", int(context->port()));
  131. const char *args[]= { buffer, "-f", REGRESSION_FUNCTION_833394, "-b", "payload", 0 };
  132. // The argument doesn't exist, so we should see an error
  133. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearman", args, true));
  134. return TEST_SUCCESS;
  135. }
  136. static test_return_t gearadmin_help_test(void* object)
  137. {
  138. cli::Context *context= (cli::Context*)object;
  139. char buffer[1024];
  140. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  141. const char *args[]= { buffer, "--help", 0 };
  142. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearadmin", args, true));
  143. return TEST_SUCCESS;
  144. }
  145. static test_return_t gearadmin_shutdown_test(void* object)
  146. {
  147. cli::Context *context= (cli::Context*)object;
  148. char buffer[1024];
  149. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  150. const char *args[]= { buffer, "--shutdown", 0 };
  151. ASSERT_EQ(EXIT_FAILURE, exec_cmdline("bin/gearadmin", args, true));
  152. return TEST_SUCCESS;
  153. }
  154. static test_return_t gearadmin_version_test(void* object)
  155. {
  156. cli::Context *context= (cli::Context*)object;
  157. char buffer[1024];
  158. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  159. const char *args[]= { buffer, "--server-version", 0 };
  160. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearadmin", args, true));
  161. return TEST_SUCCESS;
  162. }
  163. static test_return_t gearadmin_verbose_test(void* object)
  164. {
  165. cli::Context *context= (cli::Context*)object;
  166. char buffer[1024];
  167. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  168. const char *args[]= { buffer, "--server-verbose", 0 };
  169. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearadmin", args, true));
  170. return TEST_SUCCESS;
  171. }
  172. static test_return_t gearadmin_status_TEST(void* object)
  173. {
  174. cli::Context *context= (cli::Context*)object;
  175. char buffer[1024];
  176. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  177. const char *args[]= { buffer, "--status", 0 };
  178. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearadmin", args, true));
  179. return TEST_SUCCESS;
  180. }
  181. static test_return_t gearadmin_status_with_jobs_TEST(void* object)
  182. {
  183. cli::Context *context= (cli::Context*)object;
  184. {
  185. libgearman::Client client(context->port());
  186. for (size_t x= 0; x < 10000; ++x)
  187. {
  188. gearman_job_handle_t job_handle;
  189. ASSERT_EQ(GEARMAN_SUCCESS, gearman_client_do_background(&client,
  190. __func__,
  191. NULL, // unique
  192. NULL, 0, // workload
  193. job_handle));
  194. }
  195. }
  196. char buffer[1024];
  197. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  198. const char *args[]= { buffer, "--status", 0 };
  199. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearadmin", args, true));
  200. return TEST_SUCCESS;
  201. }
  202. static test_return_t gearadmin_priority_status_TEST(void* object)
  203. {
  204. cli::Context *context= (cli::Context*)object;
  205. char buffer[1024];
  206. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  207. const char *args[]= { buffer, "--priority-status", 0 };
  208. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearadmin", args, true));
  209. return TEST_SUCCESS;
  210. }
  211. static test_return_t gearadmin_show_unique_jobs_TEST(void* object)
  212. {
  213. cli::Context *context= (cli::Context*)object;
  214. char buffer[1024];
  215. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  216. const char *args[]= { buffer, "--show-unique-jobs", 0 };
  217. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearadmin", args, true));
  218. return TEST_SUCCESS;
  219. }
  220. static test_return_t gearadmin_cancel_job_TEST(void* object)
  221. {
  222. cli::Context *context= (cli::Context*)object;
  223. char buffer[1024];
  224. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  225. const char *args[]= { buffer, "--cancel-job=test", 0 };
  226. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearadmin", args, true));
  227. return TEST_SUCCESS;
  228. }
  229. static test_return_t gearadmin_show_jobs_TEST(void* object)
  230. {
  231. cli::Context *context= (cli::Context*)object;
  232. char buffer[1024];
  233. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  234. const char *args[]= { buffer, "--show-jobs", 0 };
  235. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearadmin", args, true));
  236. return TEST_SUCCESS;
  237. }
  238. static test_return_t gearadmin_workers_test(void* object)
  239. {
  240. cli::Context *context= (cli::Context*)object;
  241. libgearman::Worker worker(context->port());
  242. for (int x= 0; x < 100; ++x)
  243. {
  244. char function_name[1024];
  245. snprintf(function_name, sizeof(function_name), "function_%u", x);
  246. ASSERT_EQ(GEARMAN_SUCCESS, gearman_worker_register(&worker, function_name, 0));
  247. }
  248. char buffer[1024];
  249. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  250. const char *args[]= { buffer, "--workers", 0 };
  251. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearadmin", args, true));
  252. return TEST_SUCCESS;
  253. }
  254. static test_return_t gearadmin_create_drop_test(void* object)
  255. {
  256. cli::Context *context= (cli::Context*)object;
  257. char buffer[1024];
  258. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  259. const char *create_args[]= { buffer, "--create-function=test_function", 0 };
  260. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearadmin", create_args, true));
  261. const char *drop_args[]= { buffer, "--drop-function=test_function", 0 };
  262. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearadmin", drop_args, true));
  263. return TEST_SUCCESS;
  264. }
  265. static test_return_t gearadmin_getpid_test(void* object)
  266. {
  267. cli::Context *context= (cli::Context*)object;
  268. {
  269. libgearman::Client client(context->port());
  270. for (size_t x= 0; x < 4; x++)
  271. {
  272. gearman_job_handle_t job_handle;
  273. ASSERT_EQ(GEARMAN_SUCCESS,
  274. gearman_client_do_background(&client,
  275. __func__, // function
  276. NULL, // unique
  277. NULL, 0, // workload
  278. job_handle));
  279. }
  280. }
  281. char buffer[1024];
  282. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  283. const char *args[]= { buffer, "--getpid", 0 };
  284. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("bin/gearadmin", args, true));
  285. return TEST_SUCCESS;
  286. }
  287. static test_return_t gearadmin_unknown_test(void* object)
  288. {
  289. cli::Context *context= (cli::Context*)object;
  290. char buffer[1024];
  291. snprintf(buffer, sizeof(buffer), "--port=%d", int(context->port()));
  292. const char *args[]= { buffer, "--unknown", 0 };
  293. // The argument doesn't exist, so we should see an error
  294. ASSERT_EQ(EXIT_FAILURE, exec_cmdline("bin/gearadmin", args, true));
  295. return TEST_SUCCESS;
  296. }
  297. static test_return_t init_SETUP(void* object)
  298. {
  299. cli::Context *context= (cli::Context*)object;
  300. context->port(libtest::get_free_port());
  301. ASSERT_TRUE(server_startup(context->servers, "gearmand", context->port(), NULL));
  302. // Echo function
  303. gearman_function_t echo_react_fn_v2= gearman_function_create(echo_or_react_worker_v2);
  304. context->push(test_worker_start(context->port(), NULL, WORKER_FUNCTION_NAME, echo_react_fn_v2, NULL, gearman_worker_options_t()));
  305. context->push(test_worker_start(context->port(), NULL, REGRESSION_FUNCTION_833394, echo_react_fn_v2, NULL, gearman_worker_options_t()));
  306. return TEST_SUCCESS;
  307. }
  308. static test_return_t init_TEARDOWN(void* object)
  309. {
  310. cli::Context *context= (cli::Context*)object;
  311. context->clear();
  312. return TEST_SUCCESS;
  313. }
  314. static test_return_t server_SETUP(void *object)
  315. {
  316. cli::Context *context= (cli::Context*)object;
  317. in_port_t new_port= libtest::get_free_port();
  318. ASSERT_TRUE(server_startup(context->servers, "gearmand", new_port, NULL));
  319. return TEST_SUCCESS;
  320. }
  321. test_st gearman_tests[] ={
  322. { "--help", 0, gearman_help_test },
  323. { "-H", 0, gearman_help_test },
  324. { "-v", 0, gearman_verbose_TEST },
  325. { "--unknown", 0, gearman_unknown_test },
  326. { "-f echo -b payload", 0, gearman_client_background_test },
  327. { "lp:833394", 0, regression_833394_test },
  328. { 0, 0, 0 }
  329. };
  330. test_st gearadmin_tests[] ={
  331. {"--help", 0, gearadmin_help_test},
  332. {"--server-version", 0, gearadmin_version_test},
  333. {"--server-verbose", 0, gearadmin_verbose_test},
  334. {"--cancel-job", 0, gearadmin_cancel_job_TEST},
  335. {"--show-jobs", 0, gearadmin_show_jobs_TEST},
  336. {"--show-unique-jobs", 0, gearadmin_show_unique_jobs_TEST},
  337. {"--status", 0, gearadmin_status_TEST},
  338. {"--priority-status", 0, gearadmin_priority_status_TEST},
  339. {"gearman_client_do_background(100) --status", 0, gearadmin_status_with_jobs_TEST},
  340. {"--getpid", 0, gearadmin_getpid_test},
  341. {"--workers", 0, gearadmin_workers_test},
  342. {"--create-function and --drop-function", 0, gearadmin_create_drop_test},
  343. {"--unknown", 0, gearadmin_unknown_test},
  344. {0, 0, 0}
  345. };
  346. test_st gearadmin_shutdown_tests[] ={
  347. {"--shutdown", 0, gearadmin_shutdown_test}, // shutdown test is a relict. It doesn't shut down the server anymore
  348. {0, 0, 0}
  349. };
  350. collection_st collection[] ={
  351. {"gearman", init_SETUP, init_TEARDOWN, gearman_tests},
  352. {"gearadmin", init_SETUP, init_TEARDOWN, gearadmin_tests},
  353. {"gearadmin --shutdown", server_SETUP, init_TEARDOWN, gearadmin_shutdown_tests},
  354. {0, 0, 0, 0}
  355. };
  356. static void *world_create(server_startup_st& servers, test_return_t& error)
  357. {
  358. #if defined(HAVE_WOLFSSL) && HAVE_WOLFSSL
  359. SKIP_IF(HAVE_WOLFSSL);
  360. #endif
  361. cli::Context *context= new cli::Context(servers);
  362. if (context == NULL)
  363. {
  364. error= TEST_FAILURE;
  365. return NULL;
  366. }
  367. return context;
  368. }
  369. static bool world_destroy(void *object)
  370. {
  371. cli::Context *context= (cli::Context*)object;
  372. delete context;
  373. return TEST_SUCCESS;
  374. }
  375. void get_world(libtest::Framework *world)
  376. {
  377. world->collections(collection);
  378. world->create(world_create);
  379. world->destroy(world_destroy);
  380. }