task.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 <config.h>
  38. #include <libtest/test.hpp>
  39. using namespace libtest;
  40. #include <cassert>
  41. #include <libgearman/gearman.h>
  42. #include <libgearman/actions.hpp>
  43. #include <tests/task.h>
  44. #include <iostream>
  45. #ifndef __INTEL_COMPILER
  46. #pragma GCC diagnostic ignored "-Wold-style-cast"
  47. #endif
  48. test_return_t gearman_client_add_task_test(void *object)
  49. {
  50. gearman_client_st *client= (gearman_client_st *)object;
  51. const char *worker_function= (const char *)gearman_client_context(client);
  52. assert(worker_function);
  53. gearman_return_t ret;
  54. gearman_task_st *task= gearman_client_add_task(client, NULL, NULL,
  55. worker_function, NULL, "dog", 3,
  56. &ret);
  57. test_true_got(gearman_success(ret), gearman_strerror(ret));
  58. test_truth(task);
  59. do
  60. {
  61. // just for async IO
  62. do {
  63. ret= gearman_client_run_tasks(client);
  64. } while (gearman_continue(ret));
  65. test_compare_got(GEARMAN_SUCCESS, ret, gearman_client_error(client));
  66. // If the task has been built to be freed, we won't have it to test
  67. if (gearman_client_has_option(client, GEARMAN_CLIENT_FREE_TASKS))
  68. {
  69. return TEST_SUCCESS;
  70. }
  71. } while (gearman_task_is_running(task));
  72. gearman_task_free(task);
  73. return TEST_SUCCESS;
  74. }
  75. test_return_t gearman_client_add_task_test_fail(void *object)
  76. {
  77. gearman_client_st *client= (gearman_client_st *)object;
  78. const char *worker_function= (const char *)gearman_client_context(client);
  79. assert(worker_function);
  80. gearman_return_t ret;
  81. gearman_task_st *task= gearman_client_add_task(client, NULL, NULL,
  82. worker_function, NULL,
  83. test_literal_param("fail"),
  84. &ret);
  85. test_compare(GEARMAN_SUCCESS,ret);
  86. test_truth(task);
  87. test_truth(task->client);
  88. do {
  89. ret= gearman_client_run_tasks(client);
  90. } while (gearman_continue(ret));
  91. test_compare_got(GEARMAN_SUCCESS, ret, gearman_client_error(client));
  92. // If the task has been free() then we can't check anything about it
  93. if (gearman_client_has_option(client, GEARMAN_CLIENT_FREE_TASKS))
  94. {
  95. return TEST_SUCCESS;
  96. }
  97. test_truth(task->client);
  98. test_compare_got(GEARMAN_WORK_FAIL, gearman_task_return(task), gearman_task_error(task));
  99. test_truth(task->client);
  100. gearman_task_free(task);
  101. return TEST_SUCCESS;
  102. }
  103. test_return_t gearman_client_add_task_test_bad_workload(void *object)
  104. {
  105. gearman_client_st *client= (gearman_client_st *)object;
  106. const char *worker_function= (const char *)gearman_client_context(client);
  107. assert(worker_function);
  108. gearman_return_t ret;
  109. // We test for pointer with zero size
  110. gearman_task_st *task= gearman_client_add_task(client, NULL, NULL,
  111. worker_function, NULL, "fail", 0,
  112. &ret);
  113. test_true_got(ret == GEARMAN_INVALID_ARGUMENT, gearman_strerror(ret));
  114. test_false(task);
  115. // We test for NULL with size
  116. task= gearman_client_add_task(client, NULL, NULL,
  117. worker_function, NULL, NULL, 5,
  118. &ret);
  119. test_true_got(ret == GEARMAN_INVALID_ARGUMENT, gearman_strerror(ret));
  120. test_false(task);
  121. return TEST_SUCCESS;
  122. }
  123. static gearman_return_t gearman_exception_test_function(gearman_task_st *task)
  124. {
  125. bool *success= (bool *)gearman_task_context(task);
  126. if (not success)
  127. return GEARMAN_WORK_FAIL;
  128. *success= true;
  129. return GEARMAN_SUCCESS;
  130. }
  131. test_return_t gearman_client_add_task_exception(void *object)
  132. {
  133. gearman_client_st *client= (gearman_client_st *)object;
  134. const char *worker_function= (const char *)gearman_client_context(client);
  135. assert(worker_function);
  136. if (gearman_client_has_option(client, GEARMAN_CLIENT_FREE_TASKS))
  137. {
  138. return TEST_SKIPPED;
  139. }
  140. gearman_return_t ret;
  141. test_truth(gearman_client_set_server_option(client, test_literal_param("exceptions")));
  142. gearman_client_set_exception_fn(client, gearman_exception_test_function);
  143. bool exception_success= false;
  144. gearman_task_st *task= gearman_client_add_task(client, NULL, &exception_success,
  145. worker_function, NULL,
  146. test_literal_param("exception"),
  147. &ret);
  148. test_compare_got(GEARMAN_SUCCESS, ret, gearman_strerror(ret));
  149. test_truth(task);
  150. do {
  151. ret= gearman_client_run_tasks(client);
  152. } while (gearman_continue(ret));
  153. test_compare_got(GEARMAN_SUCCESS, ret, gearman_client_error(client));
  154. test_truth(exception_success);
  155. gearman_client_set_exception_fn(client, NULL);
  156. // If the task has been free() then we can't check anything about it
  157. if (gearman_client_has_option(client, GEARMAN_CLIENT_FREE_TASKS))
  158. {
  159. return TEST_SUCCESS;
  160. }
  161. assert(client->task_list);
  162. gearman_task_free(task);
  163. return TEST_SUCCESS;
  164. }
  165. test_return_t gearman_client_add_task_background_test(void *object)
  166. {
  167. gearman_client_st *client= (gearman_client_st *)object;
  168. const char *worker_function= (const char *)gearman_client_context(client);
  169. assert(worker_function);
  170. gearman_return_t ret;
  171. gearman_task_st *task= gearman_client_add_task_background(client, NULL, NULL,
  172. worker_function, NULL, "dog", 3,
  173. &ret);
  174. test_compare_got(GEARMAN_SUCCESS, ret, gearman_strerror(ret));
  175. test_truth(task);
  176. do
  177. {
  178. // just for async IO
  179. do {
  180. ret= gearman_client_run_tasks(client);
  181. } while (gearman_continue(ret));
  182. test_compare_got(GEARMAN_SUCCESS, ret, gearman_client_error(client));
  183. // If the task has been built to be freed, we won't have it to test
  184. if (gearman_client_has_option(client, GEARMAN_CLIENT_FREE_TASKS))
  185. {
  186. return TEST_SUCCESS;
  187. }
  188. } while (gearman_task_is_running(task));
  189. assert(client->task_list);
  190. gearman_task_free(task);
  191. return TEST_SUCCESS;
  192. }
  193. test_return_t gearman_client_add_task_high_background_test(void *object)
  194. {
  195. gearman_client_st *client= (gearman_client_st *)object;
  196. const char *worker_function= (const char *)gearman_client_context(client);
  197. assert(worker_function);
  198. gearman_return_t ret;
  199. gearman_task_st *task= gearman_client_add_task_high_background(client, NULL, NULL,
  200. worker_function, NULL, "dog", 3,
  201. &ret);
  202. test_compare_got(GEARMAN_SUCCESS, ret, gearman_strerror(ret));
  203. test_truth(task);
  204. do
  205. {
  206. // just for async IO
  207. do {
  208. ret= gearman_client_run_tasks(client);
  209. } while (gearman_continue(ret));
  210. test_compare_got(GEARMAN_SUCCESS, ret, gearman_client_error(client));
  211. // If the task has been built to be freed, we won't have it to test
  212. if (gearman_client_has_option(client, GEARMAN_CLIENT_FREE_TASKS))
  213. {
  214. return TEST_SUCCESS;
  215. }
  216. } while (gearman_task_is_running(task));
  217. gearman_task_free(task);
  218. return TEST_SUCCESS;
  219. }
  220. test_return_t gearman_client_add_task_low_background_test(void *object)
  221. {
  222. gearman_client_st *client= (gearman_client_st *)object;
  223. const char *worker_function= (const char *)gearman_client_context(client);
  224. assert(worker_function);
  225. gearman_return_t ret;
  226. gearman_task_st *task= gearman_client_add_task_high_background(client, NULL, NULL,
  227. worker_function, NULL, "dog", 3,
  228. &ret);
  229. test_compare_got(GEARMAN_SUCCESS, ret, gearman_strerror(ret));
  230. test_truth(task);
  231. do
  232. {
  233. // just for async IO
  234. do {
  235. ret= gearman_client_run_tasks(client);
  236. } while (gearman_continue(ret));
  237. test_compare_got(GEARMAN_SUCCESS, ret, gearman_client_error(client));
  238. // If the task has been built to be freed, we won't have it to test
  239. if (gearman_client_has_option(client, GEARMAN_CLIENT_FREE_TASKS))
  240. {
  241. return TEST_SUCCESS;
  242. }
  243. } while (gearman_task_is_running(task));
  244. if (not gearman_client_has_option(client, GEARMAN_CLIENT_FREE_TASKS))
  245. gearman_task_free(task);
  246. return TEST_SUCCESS;
  247. }
  248. static gearman_return_t gearman_warning_test_function(gearman_task_st *task)
  249. {
  250. bool *success= (bool *)gearman_task_context(task);
  251. if (not success)
  252. return GEARMAN_WORK_FAIL;
  253. *success= true;
  254. return GEARMAN_SUCCESS;
  255. }
  256. test_return_t gearman_client_add_task_warning(void *object)
  257. {
  258. gearman_client_st *client= (gearman_client_st *)object;
  259. const char *worker_function= (const char *)gearman_client_context(client);
  260. assert(worker_function);
  261. gearman_return_t ret;
  262. gearman_client_set_warning_fn(client, gearman_warning_test_function);
  263. bool warning_success= false;
  264. gearman_task_st *task= gearman_client_add_task(client, NULL, &warning_success,
  265. worker_function, NULL,
  266. test_literal_param("warning"),
  267. &ret);
  268. test_compare_got(GEARMAN_SUCCESS, ret, gearman_strerror(ret));
  269. test_truth(task);
  270. test_compare(GEARMAN_SUCCESS,
  271. gearman_client_run_tasks(client));
  272. test_truth(warning_success);
  273. gearman_client_set_warning_fn(client, NULL);
  274. if (not gearman_client_has_option(client, GEARMAN_CLIENT_FREE_TASKS))
  275. gearman_task_free(task);
  276. return TEST_SUCCESS;
  277. }
  278. test_return_t gearman_client_add_task_no_servers(void *)
  279. {
  280. gearman_client_st *client= gearman_client_create(NULL);
  281. test_truth(client);
  282. gearman_return_t ret;
  283. gearman_task_st *task= gearman_client_add_task(client, NULL, NULL,
  284. "does not exist", NULL,
  285. test_literal_param("dog"),
  286. &ret);
  287. test_compare_got(GEARMAN_SUCCESS, ret, gearman_strerror(ret));
  288. test_truth(task);
  289. test_compare(GEARMAN_NO_SERVERS,
  290. gearman_client_run_tasks(client));
  291. test_compare(GEARMAN_NO_SERVERS,
  292. gearman_client_run_tasks(client));
  293. gearman_client_free(client);
  294. return TEST_SUCCESS;
  295. }
  296. test_return_t gearman_client_add_task_pause_test(void *object)
  297. {
  298. gearman_client_st *client= (gearman_client_st *)object;
  299. assert(client);
  300. const char *worker_function= (const char *)gearman_client_context(client);
  301. assert(worker_function);
  302. // Don't do this.
  303. gearman_actions_t pause_actions= gearman_actions_pause();
  304. client->actions= pause_actions;
  305. gearman_return_t ret;
  306. gearman_task_st *task= gearman_client_add_task(client, NULL, NULL,
  307. worker_function, NULL, "dog", 3,
  308. &ret);
  309. test_compare(client->actions.data_fn, pause_actions.data_fn);
  310. test_compare_got(GEARMAN_SUCCESS, ret, gearman_strerror(ret));
  311. test_truth(task);
  312. do
  313. {
  314. // just for async IO
  315. uint32_t count= 0;
  316. do {
  317. count++;
  318. test_compare(client->actions.data_fn, pause_actions.data_fn);
  319. ret= gearman_client_run_tasks(client);
  320. test_compare(client->actions.data_fn, pause_actions.data_fn);
  321. } while (gearman_continue(ret));
  322. test_compare_got(GEARMAN_SUCCESS, ret, gearman_client_error(client));
  323. test_true(count > 1);
  324. // If the task has been built to be freed, we won't have it to test
  325. if (gearman_client_has_option(client, GEARMAN_CLIENT_FREE_TASKS))
  326. {
  327. return TEST_SUCCESS;
  328. }
  329. } while (gearman_task_is_running(task));
  330. gearman_task_free(task);
  331. return TEST_SUCCESS;
  332. }
  333. struct _task_free_st {
  334. int64_t count;
  335. _task_free_st() :
  336. count(0)
  337. { }
  338. void add()
  339. {
  340. count++;
  341. }
  342. bool test()
  343. {
  344. return true;
  345. }
  346. void reset()
  347. {
  348. count= 0;
  349. }
  350. bool success()
  351. {
  352. if (count)
  353. return true;
  354. return false;
  355. }
  356. };
  357. static void test_task_free_fn(gearman_task_st *task, void *context)
  358. {
  359. assert(task);
  360. _task_free_st *foo= (_task_free_st *)context;
  361. assert(foo->test());
  362. foo->add();
  363. }
  364. test_return_t gearman_client_set_task_context_free_fn_test(void *object)
  365. {
  366. gearman_client_st *client= (gearman_client_st *)object;
  367. assert(client);
  368. const char *worker_function= (const char *)gearman_client_context(client);
  369. assert(worker_function);
  370. struct _task_free_st task_free_foo;
  371. gearman_client_set_task_context_free_fn(client, test_task_free_fn);
  372. gearman_return_t ret;
  373. gearman_task_st *task= gearman_client_add_task(client, NULL, NULL,
  374. worker_function, NULL, "dog", 3,
  375. &ret);
  376. test_true_got(gearman_success(ret), gearman_strerror(ret));
  377. test_truth(task);
  378. gearman_task_set_context(task, &task_free_foo);
  379. do
  380. {
  381. // just for async IO
  382. do {
  383. ret= gearman_client_run_tasks(client);
  384. } while (gearman_continue(ret));
  385. test_compare_got(GEARMAN_SUCCESS, ret, gearman_client_error(client));
  386. // If the task has been built to be freed, we won't have it to test
  387. if (gearman_client_has_option(client, GEARMAN_CLIENT_FREE_TASKS))
  388. {
  389. test_true(task_free_foo.success());
  390. return TEST_SUCCESS;
  391. }
  392. } while (gearman_task_is_running(task));
  393. gearman_task_free(task);
  394. test_true(task_free_foo.success());
  395. return TEST_SUCCESS;
  396. }