unique.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  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 <cstring>
  42. #include <libgearman/gearman.h>
  43. #include <tests/unique.h>
  44. #include <tests/start_worker.h>
  45. #include "libgearman/client.hpp"
  46. #include "libgearman/worker.hpp"
  47. using namespace org::gearmand;
  48. #include "tests/libgearman-1.0/client_test.h"
  49. #include "tests/workers/v1/unique.h"
  50. #include "tests/workers/v2/sleep_return_random.h"
  51. #include "tests/workers/v2/echo_or_react.h"
  52. #ifndef __INTEL_COMPILER
  53. #pragma GCC diagnostic ignored "-Wold-style-cast"
  54. #endif
  55. #define WORKER_UNIQUE_FUNCTION_NAME "unique_test"
  56. test_return_t unique_SETUP(void *object)
  57. {
  58. client_test_st *test= (client_test_st *)object;
  59. test->set_worker_name(WORKER_UNIQUE_FUNCTION_NAME);
  60. gearman_function_t unique_worker_arg= gearman_function_create_v1(unique_worker);
  61. test->push(test_worker_start(libtest::default_port(), NULL,
  62. test->worker_name(),
  63. unique_worker_arg, NULL, GEARMAN_WORKER_GRAB_UNIQ));
  64. return TEST_SUCCESS;
  65. }
  66. test_return_t coalescence_TEST(void *object)
  67. {
  68. gearman_client_st *client_one= (gearman_client_st *)object;
  69. ASSERT_TRUE(client_one);
  70. libgearman::Client client_two(client_one);
  71. const char* unique_handle= "local_handle";
  72. gearman_function_t sleep_return_random_worker_FN= gearman_function_create(sleep_return_random_worker);
  73. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  74. NULL,
  75. __func__,
  76. sleep_return_random_worker_FN,
  77. NULL,
  78. gearman_worker_options_t(),
  79. 0)); // timeout
  80. // First task
  81. gearman_return_t ret;
  82. gearman_task_st *first_task= gearman_client_add_task(client_one,
  83. NULL, // preallocated task
  84. NULL, // context
  85. __func__, // function
  86. unique_handle, // unique
  87. NULL, 0, // workload
  88. &ret);
  89. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  90. ASSERT_TRUE(first_task);
  91. ASSERT_TRUE(gearman_task_unique(first_task));
  92. ASSERT_EQ(strlen(unique_handle), strlen(gearman_task_unique(first_task)));
  93. // Second task
  94. gearman_task_st *second_task= gearman_client_add_task(&client_two,
  95. NULL, // preallocated task
  96. NULL, // context
  97. __func__, // function
  98. unique_handle, // unique
  99. NULL, 0, // workload
  100. &ret);
  101. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  102. ASSERT_TRUE(second_task);
  103. ASSERT_TRUE(gearman_task_unique(second_task));
  104. ASSERT_EQ(strlen(unique_handle), strlen(gearman_task_unique(second_task)));
  105. test_strcmp(gearman_task_unique(first_task), gearman_task_unique(second_task));
  106. do {
  107. ret= gearman_client_run_tasks(client_one);
  108. gearman_client_run_tasks(&client_two);
  109. } while (gearman_continue(ret));
  110. do {
  111. ret= gearman_client_run_tasks(&client_two);
  112. } while (gearman_continue(ret));
  113. gearman_result_st* first_result= gearman_task_result(first_task);
  114. gearman_result_st* second_result= gearman_task_result(second_task);
  115. ASSERT_EQ(GEARMAN_SUCCESS, gearman_task_return(first_task));
  116. ASSERT_EQ(GEARMAN_SUCCESS, gearman_task_return(second_task));
  117. ASSERT_EQ(gearman_result_value(first_result), gearman_result_value(second_result));
  118. gearman_task_free(first_task);
  119. gearman_task_free(second_task);
  120. return TEST_SUCCESS;
  121. }
  122. test_return_t coalescence_by_data_hash_TEST(void *object)
  123. {
  124. gearman_client_st *client_one= (gearman_client_st *)object;
  125. ASSERT_TRUE(client_one);
  126. libgearman::Client client_two(client_one);
  127. const char* unique_handle= "#";
  128. gearman_function_t sleep_return_random_worker_FN= gearman_function_create(sleep_return_random_worker);
  129. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  130. NULL,
  131. __func__,
  132. sleep_return_random_worker_FN,
  133. NULL,
  134. gearman_worker_options_t(),
  135. 0)); // timeout
  136. // First task
  137. gearman_return_t ret;
  138. gearman_task_st *first_task= gearman_client_add_task(client_one,
  139. NULL, // preallocated task
  140. NULL, // context
  141. __func__, // function
  142. unique_handle, // unique
  143. test_literal_param(__func__), // workload
  144. &ret);
  145. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  146. ASSERT_TRUE(first_task);
  147. ASSERT_TRUE(gearman_task_unique(first_task));
  148. ASSERT_EQ(strlen("2285535048"), strlen(gearman_task_unique(first_task)));
  149. // Second task
  150. gearman_task_st *second_task= gearman_client_add_task(&client_two,
  151. NULL, // preallocated task
  152. NULL, // context
  153. __func__, // function
  154. unique_handle, // unique
  155. test_literal_param(__func__), // workload
  156. &ret);
  157. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  158. ASSERT_TRUE(second_task);
  159. ASSERT_TRUE(gearman_task_unique(second_task));
  160. ASSERT_EQ(strlen("2285535048"), strlen(gearman_task_unique(second_task)));
  161. test_strcmp(gearman_task_unique(first_task), gearman_task_unique(second_task));
  162. test_strcmp("2285535048", gearman_task_unique(second_task));
  163. do {
  164. ret= gearman_client_run_tasks(client_one);
  165. gearman_client_run_tasks(&client_two);
  166. } while (gearman_continue(ret));
  167. do {
  168. ret= gearman_client_run_tasks(&client_two);
  169. } while (gearman_continue(ret));
  170. gearman_result_st* first_result= gearman_task_result(first_task);
  171. gearman_result_st* second_result= gearman_task_result(second_task);
  172. ASSERT_EQ(GEARMAN_SUCCESS, gearman_task_return(first_task));
  173. ASSERT_EQ(GEARMAN_SUCCESS, gearman_task_return(second_task));
  174. ASSERT_EQ(gearman_result_value(first_result), gearman_result_value(second_result));
  175. gearman_task_free(first_task);
  176. gearman_task_free(second_task);
  177. return TEST_SUCCESS;
  178. }
  179. test_return_t coalescence_by_data_TEST(void *object)
  180. {
  181. gearman_client_st *client_one= (gearman_client_st *)object;
  182. ASSERT_TRUE(client_one);
  183. libgearman::Client client_two(client_one);
  184. const char* unique_handle= "-";
  185. gearman_function_t sleep_return_random_worker_FN= gearman_function_create(sleep_return_random_worker);
  186. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  187. NULL,
  188. __func__,
  189. sleep_return_random_worker_FN,
  190. NULL,
  191. gearman_worker_options_t(),
  192. 0)); // timeout
  193. // First task
  194. gearman_return_t ret;
  195. gearman_task_st *first_task= gearman_client_add_task(client_one,
  196. NULL, // preallocated task
  197. NULL, // context
  198. __func__, // function
  199. unique_handle, // unique
  200. NULL, 0, // workload
  201. &ret);
  202. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  203. ASSERT_TRUE(first_task);
  204. ASSERT_TRUE(gearman_task_unique(first_task));
  205. ASSERT_EQ(strlen(unique_handle), strlen(gearman_task_unique(first_task)));
  206. // Second task
  207. gearman_task_st *second_task= gearman_client_add_task(&client_two,
  208. NULL, // preallocated task
  209. NULL, // context
  210. __func__, // function
  211. unique_handle, // unique
  212. NULL, 0, // workload
  213. &ret);
  214. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  215. ASSERT_TRUE(second_task);
  216. ASSERT_TRUE(gearman_task_unique(second_task));
  217. ASSERT_EQ(strlen(unique_handle), strlen(gearman_task_unique(second_task)));
  218. test_strcmp(gearman_task_unique(first_task), gearman_task_unique(second_task));
  219. do {
  220. ret= gearman_client_run_tasks(client_one);
  221. gearman_client_run_tasks(&client_two);
  222. } while (gearman_continue(ret));
  223. do {
  224. ret= gearman_client_run_tasks(&client_two);
  225. } while (gearman_continue(ret));
  226. gearman_result_st* first_result= gearman_task_result(first_task);
  227. gearman_result_st* second_result= gearman_task_result(second_task);
  228. ASSERT_EQ(GEARMAN_SUCCESS, gearman_task_return(first_task));
  229. ASSERT_EQ(GEARMAN_SUCCESS, gearman_task_return(second_task));
  230. ASSERT_EQ(gearman_result_value(first_result), gearman_result_value(second_result));
  231. gearman_task_free(first_task);
  232. gearman_task_free(second_task);
  233. return TEST_SUCCESS;
  234. }
  235. test_return_t coalescence_by_data_FAIL_TEST(void *object)
  236. {
  237. gearman_client_st *client_one= (gearman_client_st *)object;
  238. ASSERT_TRUE(client_one);
  239. libgearman::Client client_two(client_one);
  240. const char* unique_handle= "-";
  241. gearman_function_t sleep_return_random_worker_FN= gearman_function_create(sleep_return_random_worker);
  242. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  243. NULL,
  244. __func__,
  245. sleep_return_random_worker_FN,
  246. NULL,
  247. gearman_worker_options_t(),
  248. 0)); // timeout
  249. // First task
  250. gearman_return_t ret;
  251. gearman_task_st *first_task= gearman_client_add_task(client_one,
  252. NULL, // preallocated task
  253. NULL, // context
  254. __func__, // function
  255. unique_handle, // unique
  256. NULL, 0, // workload
  257. &ret);
  258. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  259. ASSERT_TRUE(first_task);
  260. ASSERT_TRUE(gearman_task_unique(first_task));
  261. ASSERT_EQ(strlen(unique_handle), strlen(gearman_task_unique(first_task)));
  262. // Second task
  263. gearman_task_st *second_task= gearman_client_add_task(&client_two,
  264. NULL, // preallocated task
  265. NULL, // context
  266. __func__, // function
  267. unique_handle, // unique
  268. test_literal_param("mine"), // workload
  269. &ret);
  270. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  271. ASSERT_TRUE(second_task);
  272. ASSERT_TRUE(gearman_task_unique(second_task));
  273. ASSERT_EQ(strlen(unique_handle), strlen(gearman_task_unique(second_task)));
  274. test_strcmp(gearman_task_unique(first_task), gearman_task_unique(second_task));
  275. do {
  276. ret= gearman_client_run_tasks(client_one);
  277. gearman_client_run_tasks(&client_two);
  278. } while (gearman_continue(ret));
  279. do {
  280. ret= gearman_client_run_tasks(&client_two);
  281. } while (gearman_continue(ret));
  282. gearman_result_st* first_result= gearman_task_result(first_task);
  283. gearman_result_st* second_result= gearman_task_result(second_task);
  284. ASSERT_EQ(GEARMAN_SUCCESS, gearman_task_return(first_task));
  285. ASSERT_EQ(GEARMAN_SUCCESS, gearman_task_return(second_task));
  286. ASSERT_EQ(gearman_result_value(first_result), gearman_result_value(second_result));
  287. gearman_task_free(first_task);
  288. gearman_task_free(second_task);
  289. return TEST_SUCCESS;
  290. }
  291. test_return_t unique_compare_test(void *object)
  292. {
  293. gearman_return_t rc;
  294. gearman_client_st *client= (gearman_client_st *)object;
  295. const char *worker_function= (const char *)gearman_client_context(client);
  296. size_t job_length;
  297. gearman_string_t unique= { test_literal_param("my little unique") };
  298. void *job_result= gearman_client_do(client,
  299. worker_function, // function
  300. gearman_c_str(unique), // unique
  301. gearman_string_param(unique), //workload
  302. &job_length, // result size
  303. &rc);
  304. ASSERT_EQ(rc, GEARMAN_SUCCESS);
  305. ASSERT_EQ(gearman_size(unique), job_length);
  306. test_memcmp(gearman_c_str(unique), job_result, job_length);
  307. free(job_result);
  308. return TEST_SUCCESS;
  309. }
  310. test_return_t gearman_client_unique_status_TEST(void *object)
  311. {
  312. gearman_client_st *original_client= (gearman_client_st *)object;
  313. libgearman::Client status_client(original_client);
  314. libgearman::Client client_one(original_client);
  315. libgearman::Client client_two(original_client);
  316. libgearman::Client client_three(original_client);
  317. const char* unique_handle= "local_handle4";
  318. gearman_return_t ret;
  319. // First task
  320. gearman_task_st *first_task= gearman_client_add_task_background(&client_one,
  321. NULL, // preallocated task
  322. NULL, // context
  323. __func__, // function
  324. unique_handle, // unique
  325. test_literal_param("first_task"), // workload
  326. &ret);
  327. ASSERT_TRUE(first_task);
  328. gearman_task_st *second_task= gearman_client_add_task_background(&client_two,
  329. NULL, // preallocated task
  330. NULL, // context
  331. __func__, // function
  332. unique_handle, // unique
  333. test_literal_param("second_task"), // workload
  334. &ret);
  335. ASSERT_TRUE(second_task);
  336. gearman_task_st *third_task= gearman_client_add_task_background(&client_three,
  337. NULL, // preallocated task
  338. NULL, // context
  339. __func__, // function
  340. unique_handle, // unique
  341. test_literal_param("third_task"), // workload
  342. &ret);
  343. ASSERT_TRUE(third_task);
  344. {
  345. ASSERT_EQ(gearman_client_run_tasks(&client_one), GEARMAN_SUCCESS);
  346. ASSERT_EQ(gearman_client_run_tasks(&client_two), GEARMAN_SUCCESS);
  347. ASSERT_EQ(gearman_client_run_tasks(&client_three), GEARMAN_SUCCESS);
  348. }
  349. ASSERT_EQ(gearman_client_job_status(&status_client,
  350. gearman_task_job_handle(third_task), // job handle
  351. NULL, // is_known
  352. NULL, // is_running
  353. NULL, // numerator
  354. NULL // denominator
  355. ), GEARMAN_JOB_EXISTS);
  356. {
  357. libgearman::Client unique_client(original_client);
  358. gearman_status_t status= gearman_client_unique_status(&unique_client,
  359. unique_handle, strlen(unique_handle));
  360. ASSERT_EQ(GEARMAN_SUCCESS, gearman_status_return(status));
  361. }
  362. {
  363. gearman_status_t status= gearman_client_unique_status(&client_one,
  364. unique_handle, strlen(unique_handle));
  365. ASSERT_EQ(GEARMAN_SUCCESS, gearman_status_return(status));
  366. ASSERT_EQ(true, gearman_status_is_known(status));
  367. ASSERT_EQ(false, gearman_status_is_running(status));
  368. test_zero(gearman_status_numerator(status));
  369. test_zero(gearman_status_denominator(status));
  370. }
  371. gearman_function_t func= gearman_function_create_v2(echo_or_react_worker_v2);
  372. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(), NULL,
  373. __func__,
  374. func, NULL, gearman_worker_options_t()));
  375. {
  376. ASSERT_EQ(gearman_client_run_tasks(&client_one), GEARMAN_SUCCESS);
  377. ASSERT_EQ(gearman_client_run_tasks(&client_two), GEARMAN_SUCCESS);
  378. ASSERT_EQ(gearman_client_run_tasks(&client_three), GEARMAN_SUCCESS);
  379. }
  380. gearman_task_free(first_task);
  381. gearman_task_free(second_task);
  382. gearman_task_free(third_task);
  383. return TEST_SUCCESS;
  384. }
  385. test_return_t gearman_client_unique_status_NOT_FOUND_TEST(void *object)
  386. {
  387. gearman_client_st *original_client= (gearman_client_st *)object;
  388. libgearman::Client status_client(original_client);
  389. const char* unique_handle= YATL_UNIQUE;
  390. gearman_function_t func= gearman_function_create_v2(echo_or_react_worker_v2);
  391. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(), NULL,
  392. __func__,
  393. func, NULL, gearman_worker_options_t()));
  394. {
  395. gearman_status_t status= gearman_client_unique_status(&status_client,
  396. unique_handle, strlen(unique_handle));
  397. ASSERT_EQ(GEARMAN_SUCCESS, gearman_status_return(status));
  398. ASSERT_EQ(false, gearman_status_is_known(status));
  399. ASSERT_EQ(false, gearman_status_is_running(status));
  400. test_zero(gearman_status_numerator(status));
  401. test_zero(gearman_status_denominator(status));
  402. }
  403. return TEST_SUCCESS;
  404. }