execute.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <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. const char *worker_function= (const char *)gearman_client_context(client);
  52. assert(worker_function);
  53. gearman_task_st *task;
  54. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("test load"));
  55. test_true_got(task= gearman_execute(client, test_string_make_from_cstr(worker_function), NULL, 0, NULL, &value, 0), gearman_client_error(client));
  56. test_compare(test_literal_param_size("test load"), gearman_result_size(gearman_task_result(task)));
  57. test_false(gearman_task_is_known(task));
  58. test_false(gearman_task_is_running(task));
  59. gearman_task_free(task);
  60. return TEST_SUCCESS;
  61. }
  62. test_return_t gearman_execute_fail_test(void *object)
  63. {
  64. gearman_client_st *client= (gearman_client_st *)object;
  65. const char *worker_function= (const char *)gearman_client_context(client);
  66. assert(worker_function);
  67. gearman_task_st *task;
  68. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("fail"));
  69. test_true_got(task= gearman_execute(client, test_string_make_from_cstr(worker_function), NULL, 0, NULL, &value, 0), gearman_client_error(client));
  70. test_compare_got(GEARMAN_WORK_FAIL, gearman_task_return(task), gearman_task_error(task));
  71. test_false(gearman_task_is_known(task));
  72. test_false(gearman_task_is_running(task));
  73. gearman_task_free(task);
  74. return TEST_SUCCESS;
  75. }
  76. test_return_t gearman_execute_timeout_test(void *object)
  77. {
  78. gearman_client_st *client= (gearman_client_st *)object;
  79. const char *worker_function= (const char *)gearman_client_context(client);
  80. assert(worker_function);
  81. test_truth(client);
  82. gearman_client_set_timeout(client, 4);
  83. // We should fail since the the timeout is small and the function should
  84. // not exist.
  85. gearman_task_st *task;
  86. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("test load"));
  87. test_true_got(task= gearman_execute(client, test_string_make_from_cstr(worker_function), NULL, 0, NULL, &value, 0), gearman_client_error(client));
  88. gearman_task_free(task);
  89. return TEST_SUCCESS;
  90. }
  91. test_return_t gearman_execute_epoch_test(void *object)
  92. {
  93. gearman_client_st *client= (gearman_client_st *)object;
  94. const char *worker_function= (const char *)gearman_client_context(client);
  95. assert(worker_function);
  96. gearman_task_attr_t workload= gearman_task_attr_init_epoch(time(NULL) +5, GEARMAN_JOB_PRIORITY_NORMAL);
  97. gearman_task_st *task;
  98. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("test load"));
  99. test_true_got(task= gearman_execute(client, test_string_make_from_cstr(worker_function), NULL, 0, &workload, &value, 0), gearman_client_error(client));
  100. test_truth(task);
  101. test_truth(gearman_task_job_handle(task));
  102. test_true(gearman_task_is_known(task));
  103. test_false(gearman_task_is_running(task));
  104. gearman_task_free(task);
  105. return TEST_SUCCESS;
  106. }
  107. test_return_t gearman_execute_epoch_check_job_handle_test(void *object)
  108. {
  109. gearman_client_st *client= (gearman_client_st *)object;
  110. const char *worker_function= (const char *)gearman_client_context(client);
  111. assert(worker_function);
  112. gearman_task_attr_t workload= gearman_task_attr_init_epoch(time(NULL) +5, GEARMAN_JOB_PRIORITY_NORMAL);
  113. gearman_task_st *task;
  114. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("test load"));
  115. test_true_got(task= gearman_execute(client, test_string_make_from_cstr(worker_function), NULL, 0, &workload, &value, 0), gearman_client_error(client));
  116. test_truth(task);
  117. test_truth(gearman_task_job_handle(task));
  118. gearman_return_t rc;
  119. bool is_known;
  120. do {
  121. rc= gearman_client_job_status(client, gearman_task_job_handle(task), &is_known, NULL, NULL, NULL);
  122. } while (gearman_continue(rc) or is_known);
  123. test_compare(GEARMAN_SUCCESS, rc);
  124. gearman_task_free(task);
  125. return TEST_SUCCESS;
  126. }
  127. test_return_t gearman_execute_bg_test(void *object)
  128. {
  129. gearman_client_st *client= (gearman_client_st *)object;
  130. const char *worker_function= (const char *)gearman_client_context(client);
  131. assert(worker_function);
  132. gearman_task_attr_t workload= gearman_task_attr_init_background(GEARMAN_JOB_PRIORITY_NORMAL);
  133. gearman_task_st *task;
  134. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("test load"));
  135. test_true_got(task= gearman_execute(client, test_string_make_from_cstr(worker_function), test_literal_param("my id"), &workload, &value, 0),
  136. gearman_client_error(client));
  137. // Lets make sure we have a task
  138. test_truth(task);
  139. test_truth(gearman_task_job_handle(task));
  140. test_true_got(gearman_success(gearman_client_run_tasks(client)), gearman_client_error(client));
  141. gearman_task_free(task);
  142. return TEST_SUCCESS;
  143. }
  144. test_return_t gearman_execute_multile_bg_test(void *object)
  145. {
  146. gearman_client_st *client= (gearman_client_st *)object;
  147. const char *worker_function= (const char *)gearman_client_context(client);
  148. assert(worker_function);
  149. for (uint32_t x= 0; x < 4; /* No reason for number */ x++)
  150. {
  151. gearman_task_attr_t workload= gearman_task_attr_init_background(GEARMAN_JOB_PRIORITY_NORMAL);
  152. gearman_task_st *task;
  153. gearman_argument_t value= gearman_argument_make(0, 0, test_literal_param("test load"));
  154. test_true_got(task= gearman_execute(client, test_string_make_from_cstr(worker_function), NULL, 0, &workload, &value, 0),
  155. gearman_client_error(client));
  156. // Lets make sure we have a task
  157. test_truth(task);
  158. test_truth(gearman_task_job_handle(task));
  159. }
  160. test_compare(GEARMAN_SUCCESS,
  161. gearman_client_run_tasks(client));
  162. gearman_client_task_free_all(client);
  163. return TEST_SUCCESS;
  164. }