workers.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 <libgearman/gearman.h>
  41. #include <cassert>
  42. #include <cerrno>
  43. #include <cstdlib>
  44. #include <cstring>
  45. #include <climits>
  46. #include <cstdio>
  47. #include <string>
  48. #include <iostream>
  49. #include <tests/workers.h>
  50. #ifndef __INTEL_COMPILER
  51. #pragma GCC diagnostic ignored "-Wold-style-cast"
  52. #endif
  53. gearman_return_t echo_or_react_worker_v2(gearman_job_st *job, void *)
  54. {
  55. const void *workload= gearman_job_workload(job);
  56. const size_t result_size= gearman_job_workload_size(job);
  57. if (workload == NULL or result_size == 0)
  58. {
  59. assert(workload == NULL and result_size == 0);
  60. return GEARMAN_SUCCESS;
  61. }
  62. else if (result_size == test_literal_param_size("fail") and (not memcmp(workload, test_literal_param("fail"))))
  63. {
  64. return GEARMAN_FATAL;
  65. }
  66. else if (result_size == test_literal_param_size("exception") and (not memcmp(workload, test_literal_param("exception"))))
  67. {
  68. gearman_return_t rc= gearman_job_send_exception(job, test_literal_param("test exception"));
  69. if (gearman_failed(rc))
  70. {
  71. return GEARMAN_ERROR;
  72. }
  73. }
  74. else if (result_size == test_literal_param_size("warning") and (not memcmp(workload, test_literal_param("warning"))))
  75. {
  76. gearman_return_t rc= gearman_job_send_warning(job, test_literal_param("test warning"));
  77. if (gearman_failed(rc))
  78. {
  79. return GEARMAN_ERROR;
  80. }
  81. }
  82. if (gearman_failed(gearman_job_send_data(job, workload, result_size)))
  83. {
  84. return GEARMAN_ERROR;
  85. }
  86. return GEARMAN_SUCCESS;
  87. }
  88. gearman_return_t echo_or_react_chunk_worker_v2(gearman_job_st *job, void *)
  89. {
  90. const char *workload= (const char *)gearman_job_workload(job);
  91. const size_t workload_size= gearman_job_workload_size(job);
  92. bool fail= false;
  93. if (workload_size == test_literal_param_size("fail") and (not memcmp(workload, test_literal_param("fail"))))
  94. {
  95. fail= true;
  96. }
  97. else if (workload_size == test_literal_param_size("exception") and (not memcmp(workload, test_literal_param("exception"))))
  98. {
  99. if (gearman_failed(gearman_job_send_exception(job, test_literal_param("test exception"))))
  100. {
  101. return GEARMAN_ERROR;
  102. }
  103. }
  104. else if (workload_size == test_literal_param_size("warning") and (not memcmp(workload, test_literal_param("warning"))))
  105. {
  106. if (gearman_failed(gearman_job_send_warning(job, test_literal_param("test warning"))))
  107. {
  108. return GEARMAN_ERROR;
  109. }
  110. }
  111. for (size_t x= 0; x < workload_size; x++)
  112. {
  113. // Chunk
  114. {
  115. if (gearman_failed(gearman_job_send_data(job, &workload[x], 1)))
  116. {
  117. return GEARMAN_ERROR;
  118. }
  119. }
  120. // report status
  121. {
  122. if (gearman_failed(gearman_job_send_status(job, (uint32_t)x, (uint32_t)workload_size)))
  123. {
  124. return GEARMAN_ERROR;
  125. }
  126. if (fail)
  127. {
  128. return GEARMAN_FATAL;
  129. }
  130. }
  131. }
  132. return GEARMAN_SUCCESS;
  133. }
  134. // payload is unique value
  135. gearman_return_t unique_worker_v2(gearman_job_st *job, void *)
  136. {
  137. const char *workload= static_cast<const char *>(gearman_job_workload(job));
  138. assert(job->assigned.command == GEARMAN_COMMAND_JOB_ASSIGN_UNIQ);
  139. assert(gearman_job_unique(job));
  140. assert(strlen(gearman_job_unique(job)));
  141. assert(gearman_job_workload_size(job));
  142. assert(strlen(gearman_job_unique(job)) == gearman_job_workload_size(job));
  143. assert(not memcmp(workload, gearman_job_unique(job), gearman_job_workload_size(job)));
  144. if (gearman_job_workload_size(job) == strlen(gearman_job_unique(job)))
  145. {
  146. if (not memcmp(workload, gearman_job_unique(job), gearman_job_workload_size(job)))
  147. {
  148. if (gearman_failed(gearman_job_send_data(job, workload, gearman_job_workload_size(job))))
  149. {
  150. return GEARMAN_ERROR;
  151. }
  152. return GEARMAN_SUCCESS;
  153. }
  154. }
  155. return GEARMAN_FATAL;
  156. }
  157. gearman_return_t count_worker(gearman_job_st *job, void *)
  158. {
  159. char buffer[GEARMAN_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
  160. int length= snprintf(buffer, sizeof(buffer), "%lu", static_cast<unsigned long>(gearman_job_workload_size(job)));
  161. if (size_t(length) > sizeof(buffer) or length < 0)
  162. {
  163. return GEARMAN_FATAL;
  164. }
  165. return GEARMAN_SUCCESS;
  166. }
  167. static pthread_mutex_t increment_reset_worker_mutex= PTHREAD_MUTEX_INITIALIZER;
  168. gearman_return_t increment_reset_worker_v2(gearman_job_st *job, void *)
  169. {
  170. static long counter= 0;
  171. long change= 0;
  172. const char *workload= (const char*)gearman_job_workload(job);
  173. if (gearman_job_workload_size(job) == test_literal_param_size("reset") and (not memcmp(workload, test_literal_param("reset"))))
  174. {
  175. pthread_mutex_lock(&increment_reset_worker_mutex);
  176. counter= 0;
  177. pthread_mutex_unlock(&increment_reset_worker_mutex);
  178. return GEARMAN_SUCCESS;
  179. }
  180. else if (workload and gearman_job_workload_size(job))
  181. {
  182. if (gearman_job_workload_size(job) > GEARMAN_MAXIMUM_INTEGER_DISPLAY_LENGTH)
  183. {
  184. return GEARMAN_FATAL;
  185. }
  186. char temp[GEARMAN_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
  187. memcpy(temp, workload, gearman_job_workload_size(job));
  188. temp[gearman_job_workload_size(job)]= 0;
  189. change= strtol(temp, (char **)NULL, 10);
  190. if (change == LONG_MIN or change == LONG_MAX or ( change == 0 and errno < 0))
  191. {
  192. gearman_job_send_warning(job, test_literal_param("strtol() failed"));
  193. return GEARMAN_FATAL;
  194. }
  195. }
  196. {
  197. pthread_mutex_lock(&increment_reset_worker_mutex);
  198. counter= counter +change;
  199. char result[GEARMAN_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
  200. size_t result_size= size_t(snprintf(result, sizeof(result), "%ld", counter));
  201. if (gearman_failed(gearman_job_send_data(job, result, result_size)))
  202. {
  203. return GEARMAN_FATAL;
  204. }
  205. pthread_mutex_unlock(&increment_reset_worker_mutex);
  206. }
  207. return GEARMAN_SUCCESS;
  208. }