task.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <libtest/test.h>
  38. #include <cassert>
  39. #include <libgearman/gearman.h>
  40. #include <tests/task.h>
  41. #include <iostream>
  42. #ifndef __INTEL_COMPILER
  43. #pragma GCC diagnostic ignored "-Wold-style-cast"
  44. #endif
  45. test_return_t gearman_client_add_task_test(void *object)
  46. {
  47. gearman_client_st *client= (gearman_client_st *)object;
  48. const char *worker_function= (const char *)gearman_client_context(client);
  49. assert(worker_function);
  50. gearman_return_t ret;
  51. gearman_task_st *task= gearman_client_add_task(client, NULL, NULL,
  52. worker_function, NULL, "dog", 3,
  53. &ret);
  54. test_true_got(gearman_success(ret), gearman_strerror(ret));
  55. test_truth(task);
  56. do
  57. {
  58. ret= gearman_client_run_tasks(client);
  59. test_true_got(ret == GEARMAN_SUCCESS, gearman_client_error(client));
  60. } while (gearman_task_is_running(task));
  61. gearman_task_free(task);
  62. return TEST_SUCCESS;
  63. }
  64. test_return_t gearman_client_add_task_test_fail(void *object)
  65. {
  66. gearman_client_st *client= (gearman_client_st *)object;
  67. const char *worker_function= (const char *)gearman_client_context(client);
  68. assert(worker_function);
  69. gearman_return_t ret;
  70. gearman_task_st *task= gearman_client_add_task(client, NULL, NULL,
  71. worker_function, NULL,
  72. gearman_literal_param("fail"),
  73. &ret);
  74. test_true_got(ret == GEARMAN_SUCCESS, gearman_strerror(ret));
  75. test_truth(task);
  76. test_truth(task->client);
  77. do
  78. {
  79. ret= gearman_client_run_tasks(client);
  80. test_true_got(ret == GEARMAN_SUCCESS, gearman_client_error(client));
  81. test_truth(task->client);
  82. } while (gearman_task_is_running(task));
  83. test_true_got(ret == GEARMAN_SUCCESS, gearman_client_error(client));
  84. test_true_got(gearman_task_error(task) == GEARMAN_WORK_FAIL, gearman_strerror(gearman_task_error(task)));
  85. test_truth(task->client);
  86. gearman_task_free(task);
  87. return TEST_SUCCESS;
  88. }
  89. test_return_t gearman_client_add_task_test_bad_workload(void *object)
  90. {
  91. gearman_client_st *client= (gearman_client_st *)object;
  92. const char *worker_function= (const char *)gearman_client_context(client);
  93. assert(worker_function);
  94. gearman_return_t ret;
  95. // We test for pointer with zero size
  96. gearman_task_st *task= gearman_client_add_task(client, NULL, NULL,
  97. worker_function, NULL, "fail", 0,
  98. &ret);
  99. test_true_got(ret == GEARMAN_INVALID_ARGUMENT, gearman_strerror(ret));
  100. test_false(task);
  101. // We test for NULL with size
  102. task= gearman_client_add_task(client, NULL, NULL,
  103. worker_function, NULL, NULL, 5,
  104. &ret);
  105. test_true_got(ret == GEARMAN_INVALID_ARGUMENT, gearman_strerror(ret));
  106. test_false(task);
  107. return TEST_SUCCESS;
  108. }
  109. static gearman_return_t gearman_exception_test_function(gearman_task_st *task)
  110. {
  111. bool *success= (bool *)gearman_task_context(task);
  112. if (not success)
  113. return GEARMAN_WORK_FAIL;
  114. *success= true;
  115. return GEARMAN_SUCCESS;
  116. }
  117. test_return_t gearman_client_add_task_exception(void *object)
  118. {
  119. gearman_client_st *client= (gearman_client_st *)object;
  120. const char *worker_function= (const char *)gearman_client_context(client);
  121. assert(worker_function);
  122. gearman_return_t ret;
  123. test_truth(gearman_client_set_server_option(client, gearman_literal_param("exceptions")));
  124. gearman_client_set_exception_fn(client, gearman_exception_test_function);
  125. bool exception_success= false;
  126. gearman_task_st *task= gearman_client_add_task(client, NULL, &exception_success,
  127. worker_function, NULL,
  128. gearman_literal_param("exception"),
  129. &ret);
  130. test_true_got(ret == GEARMAN_SUCCESS, gearman_strerror(ret));
  131. test_truth(task);
  132. ret= gearman_client_run_tasks(client);
  133. test_true_got(ret == GEARMAN_SUCCESS, gearman_strerror(ret));
  134. test_truth(exception_success);
  135. gearman_client_set_exception_fn(client, NULL);
  136. gearman_task_free(task);
  137. return TEST_SUCCESS;
  138. }
  139. test_return_t gearman_client_add_task_background_test(void *object)
  140. {
  141. gearman_client_st *client= (gearman_client_st *)object;
  142. const char *worker_function= (const char *)gearman_client_context(client);
  143. assert(worker_function);
  144. gearman_return_t ret;
  145. gearman_task_st *task= gearman_client_add_task_background(client, NULL, NULL,
  146. worker_function, NULL, "dog", 3,
  147. &ret);
  148. test_true_got(ret == GEARMAN_SUCCESS, gearman_strerror(ret));
  149. test_truth(task);
  150. do
  151. {
  152. ret= gearman_client_run_tasks(client);
  153. test_true_got(ret == GEARMAN_SUCCESS, gearman_client_error(client));
  154. } while (gearman_task_is_running(task));
  155. gearman_task_free(task);
  156. return TEST_SUCCESS;
  157. }
  158. test_return_t gearman_client_add_task_high_background_test(void *object)
  159. {
  160. gearman_client_st *client= (gearman_client_st *)object;
  161. const char *worker_function= (const char *)gearman_client_context(client);
  162. assert(worker_function);
  163. gearman_return_t ret;
  164. gearman_task_st *task= gearman_client_add_task_high_background(client, NULL, NULL,
  165. worker_function, NULL, "dog", 3,
  166. &ret);
  167. test_true_got(ret == GEARMAN_SUCCESS, gearman_strerror(ret));
  168. test_truth(task);
  169. do
  170. {
  171. ret= gearman_client_run_tasks(client);
  172. test_true_got(ret == GEARMAN_SUCCESS, gearman_client_error(client));
  173. } while (gearman_task_is_running(task));
  174. gearman_task_free(task);
  175. return TEST_SUCCESS;
  176. }
  177. test_return_t gearman_client_add_task_low_background_test(void *object)
  178. {
  179. gearman_client_st *client= (gearman_client_st *)object;
  180. const char *worker_function= (const char *)gearman_client_context(client);
  181. assert(worker_function);
  182. gearman_return_t ret;
  183. gearman_task_st *task= gearman_client_add_task_high_background(client, NULL, NULL,
  184. worker_function, NULL, "dog", 3,
  185. &ret);
  186. test_true_got(ret == GEARMAN_SUCCESS, gearman_strerror(ret));
  187. test_truth(task);
  188. do
  189. {
  190. ret= gearman_client_run_tasks(client);
  191. test_true_got(ret == GEARMAN_SUCCESS, gearman_client_error(client));
  192. } while (gearman_task_is_running(task));
  193. gearman_task_free(task);
  194. return TEST_SUCCESS;
  195. }
  196. static gearman_return_t gearman_warning_test_function(gearman_task_st *task)
  197. {
  198. bool *success= (bool *)gearman_task_context(task);
  199. if (not success)
  200. return GEARMAN_WORK_FAIL;
  201. *success= true;
  202. return GEARMAN_SUCCESS;
  203. }
  204. test_return_t gearman_client_add_task_warning(void *object)
  205. {
  206. gearman_client_st *client= (gearman_client_st *)object;
  207. const char *worker_function= (const char *)gearman_client_context(client);
  208. assert(worker_function);
  209. gearman_return_t ret;
  210. gearman_client_set_warning_fn(client, gearman_warning_test_function);
  211. bool warning_success= false;
  212. gearman_task_st *task= gearman_client_add_task(client, NULL, &warning_success,
  213. worker_function, NULL,
  214. gearman_literal_param("warning"),
  215. &ret);
  216. test_true_got(ret == GEARMAN_SUCCESS, gearman_strerror(ret));
  217. test_truth(task);
  218. ret= gearman_client_run_tasks(client);
  219. test_true_got(ret == GEARMAN_SUCCESS, gearman_strerror(ret));
  220. test_truth(warning_success);
  221. gearman_client_set_warning_fn(client, NULL);
  222. gearman_task_free(task);
  223. return TEST_SUCCESS;
  224. }