execute.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <string>
  44. #include <tests/execute.h>
  45. #ifndef __INTEL_COMPILER
  46. #pragma GCC diagnostic ignored "-Wold-style-cast"
  47. #endif
  48. test_return_t gearman_execute_test(void *object)
  49. {
  50. gearman_client_st *client= (gearman_client_st *)object;
  51. ASSERT_TRUE(client);
  52. const char *worker_function= (const char *)gearman_client_context(client);
  53. ASSERT_TRUE(worker_function);
  54. gearman_task_st *task;
  55. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("test load"));
  56. ASSERT_TRUE(task= gearman_execute(client, test_string_make_from_cstr(worker_function), NULL, 0, NULL, &value, 0));
  57. ASSERT_EQ(test_literal_param_size("test load"), gearman_result_size(gearman_task_result(task)));
  58. test_false(gearman_task_is_known(task));
  59. test_false(gearman_task_is_running(task));
  60. gearman_task_free(task);
  61. return TEST_SUCCESS;
  62. }
  63. test_return_t gearman_execute_NULL_workload_TEST(void *object)
  64. {
  65. gearman_client_st *client= (gearman_client_st *)object;
  66. ASSERT_TRUE(client);
  67. const char *worker_function= (const char *)gearman_client_context(client);
  68. ASSERT_TRUE(worker_function);
  69. gearman_task_attr_t task_attr= gearman_task_attr_init_background(GEARMAN_JOB_PRIORITY_NORMAL);
  70. gearman_task_st *task;
  71. ASSERT_TRUE(task= gearman_execute(client,
  72. test_string_make_from_cstr(worker_function),
  73. NULL, 0, // unique
  74. &task_attr, // gearman_task_attr_t
  75. NULL, // argument
  76. 0));
  77. gearman_task_free(task);
  78. return TEST_SUCCESS;
  79. }
  80. test_return_t gearman_execute_NULL_attr_NULL_workload_TEST(void *object)
  81. {
  82. gearman_client_st *client= (gearman_client_st *)object;
  83. ASSERT_TRUE(client);
  84. const char *worker_function= (const char *)gearman_client_context(client);
  85. ASSERT_TRUE(worker_function);
  86. gearman_task_st *task;
  87. ASSERT_TRUE(task= gearman_execute(client,
  88. test_string_make_from_cstr(worker_function),
  89. NULL, 0, // unique
  90. NULL, // gearman_task_attr_t
  91. NULL, // argument
  92. 0));
  93. gearman_task_free(task);
  94. return TEST_SUCCESS;
  95. }
  96. test_return_t gearman_execute_fail_test(void *object)
  97. {
  98. gearman_client_st *client= (gearman_client_st *)object;
  99. const char *worker_function= (const char *)gearman_client_context(client);
  100. assert(worker_function);
  101. gearman_task_st *task;
  102. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("fail"));
  103. ASSERT_TRUE(task= gearman_execute(client, test_string_make_from_cstr(worker_function), NULL, 0, NULL, &value, 0));
  104. ASSERT_EQ(GEARMAN_WORK_FAIL, gearman_task_return(task));
  105. test_false(gearman_task_is_known(task));
  106. test_false(gearman_task_is_running(task));
  107. gearman_task_free(task);
  108. return TEST_SUCCESS;
  109. }
  110. test_return_t gearman_execute_timeout_test(void *object)
  111. {
  112. gearman_client_st *client= (gearman_client_st *)object;
  113. const char *worker_function= (const char *)gearman_client_context(client);
  114. assert(worker_function);
  115. ASSERT_TRUE(client);
  116. gearman_client_set_timeout(client, 4);
  117. // We should fail since the the timeout is small and the function should
  118. // not exist.
  119. gearman_task_st *task;
  120. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("test load"));
  121. ASSERT_TRUE(task= gearman_execute(client, test_string_make_from_cstr(worker_function), NULL, 0, NULL, &value, 0));
  122. gearman_task_free(task);
  123. return TEST_SUCCESS;
  124. }
  125. test_return_t gearman_execute_epoch_test(void *object)
  126. {
  127. gearman_client_st *client= (gearman_client_st *)object;
  128. const char *worker_function= (const char *)gearman_client_context(client);
  129. ASSERT_TRUE(worker_function);
  130. gearman_task_attr_t task_attr= gearman_task_attr_init_epoch(time(NULL) +5, GEARMAN_JOB_PRIORITY_NORMAL);
  131. gearman_task_st *task;
  132. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("test load"));
  133. ASSERT_TRUE(task= gearman_execute(client, test_string_make_from_cstr(worker_function),
  134. NULL, 0, // unique
  135. &task_attr, &value, 0));
  136. ASSERT_TRUE(task);
  137. ASSERT_TRUE(gearman_task_job_handle(task));
  138. ASSERT_TRUE(gearman_task_is_known(task));
  139. test_false(gearman_task_is_running(task));
  140. gearman_task_free(task);
  141. return TEST_SUCCESS;
  142. }
  143. test_return_t gearman_execute_epoch_check_job_handle_test(void *object)
  144. {
  145. gearman_client_st *client= (gearman_client_st *)object;
  146. const char *worker_function= (const char *)gearman_client_context(client);
  147. assert(worker_function);
  148. gearman_task_attr_t task_attr= gearman_task_attr_init_epoch(time(NULL) +5, GEARMAN_JOB_PRIORITY_NORMAL);
  149. gearman_task_st *task;
  150. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("test load"));
  151. ASSERT_TRUE(task= gearman_execute(client, test_string_make_from_cstr(worker_function), NULL, 0, &task_attr, &value, 0));
  152. ASSERT_TRUE(task);
  153. ASSERT_TRUE(gearman_task_job_handle(task));
  154. gearman_return_t rc;
  155. bool is_known;
  156. size_t limit= 10;
  157. do {
  158. if (--limit)
  159. {
  160. rc= gearman_client_job_status(client, gearman_task_job_handle(task), &is_known, NULL, NULL, NULL);
  161. }
  162. else
  163. {
  164. break;
  165. }
  166. } while (gearman_continue(rc) or is_known);
  167. if (limit)
  168. {
  169. ASSERT_EQ(GEARMAN_SUCCESS, rc);
  170. }
  171. gearman_task_free(task);
  172. return TEST_SUCCESS;
  173. }
  174. test_return_t gearman_execute_bg_test(void *object)
  175. {
  176. gearman_client_st *client= (gearman_client_st *)object;
  177. const char *worker_function= (const char *)gearman_client_context(client);
  178. assert(worker_function);
  179. gearman_task_attr_t task_attr= gearman_task_attr_init_background(GEARMAN_JOB_PRIORITY_NORMAL);
  180. gearman_task_st *task;
  181. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("test load"));
  182. ASSERT_TRUE(task= gearman_execute(client, test_string_make_from_cstr(worker_function), test_literal_param("my id"), &task_attr, &value, 0));
  183. // Lets make sure we have a task
  184. ASSERT_TRUE(task);
  185. ASSERT_TRUE(gearman_task_job_handle(task));
  186. ASSERT_TRUE(gearman_success(gearman_client_run_tasks(client)));
  187. gearman_task_free(task);
  188. return TEST_SUCCESS;
  189. }
  190. test_return_t gearman_execute_multile_bg_test(void *object)
  191. {
  192. gearman_client_st *client= (gearman_client_st *)object;
  193. const char *worker_function= (const char *)gearman_client_context(client);
  194. assert(worker_function);
  195. for (uint32_t x= 0; x < 4; /* No reason for number */ x++)
  196. {
  197. gearman_task_attr_t task_attr= gearman_task_attr_init_background(GEARMAN_JOB_PRIORITY_NORMAL);
  198. gearman_task_st *task;
  199. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("test load"));
  200. ASSERT_TRUE(task= gearman_execute(client, test_string_make_from_cstr(worker_function), NULL, 0, &task_attr, &value, 0));
  201. // Lets make sure we have a task
  202. ASSERT_TRUE(task);
  203. ASSERT_TRUE(gearman_task_job_handle(task));
  204. }
  205. ASSERT_EQ(GEARMAN_SUCCESS,
  206. gearman_client_run_tasks(client));
  207. gearman_client_task_free_all(client);
  208. return TEST_SUCCESS;
  209. }