gearman_execute_partition.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <tests/gearman_execute_partition.h>
  44. #ifndef __INTEL_COMPILER
  45. #pragma GCC diagnostic ignored "-Wold-style-cast"
  46. #endif
  47. test_return_t gearman_execute_partition_check_parameters(void *object)
  48. {
  49. gearman_client_st *client= (gearman_client_st *)object;
  50. test_true_got(gearman_success(gearman_client_echo(client, test_literal_param("this is mine"))), gearman_client_error(client));
  51. // This just hear to make it easier to trace when gearman_execute() is
  52. // called (look in the log to see the failed option setting.
  53. gearman_client_set_server_option(client, test_literal_param("should fail"));
  54. gearman_argument_t workload= gearman_argument_make(0, 0, test_literal_param("this dog does not hunt"));
  55. gearman_task_st *task;
  56. // Test client as NULL
  57. test_false(task= gearman_execute_by_partition(NULL,
  58. test_literal_param("split_worker"),
  59. test_literal_param("client_test"),
  60. NULL, 0, // unique
  61. NULL,
  62. &workload, 0));
  63. // Test no partition function
  64. test_false(task= gearman_execute_by_partition(client,
  65. NULL, 0,
  66. NULL, 0,
  67. NULL, 0, // unique
  68. NULL,
  69. &workload, 0));
  70. return TEST_SUCCESS;
  71. }
  72. test_return_t gearman_execute_partition_basic(void *object)
  73. {
  74. gearman_client_st *client= (gearman_client_st *)object;
  75. test_true_got(gearman_success(gearman_client_echo(client, test_literal_param("this is mine"))), gearman_client_error(client));
  76. // This just hear to make it easier to trace when
  77. // gearman_execute_partition() is called (look in the log to see the
  78. // failed option setting.
  79. gearman_client_set_server_option(client, test_literal_param("should fail"));
  80. gearman_argument_t workload= gearman_argument_make(0, 0, test_literal_param("this dog does not hunt"));
  81. gearman_task_st *task;
  82. test_true_got(task= gearman_execute_by_partition(client,
  83. test_literal_param("split_worker"),
  84. test_literal_param("client_test"),
  85. NULL, 0, // unique
  86. NULL,
  87. &workload, 0), gearman_client_error(client));
  88. test_compare(GEARMAN_SUCCESS, gearman_task_return(task));
  89. gearman_result_st *result= gearman_task_result(task);
  90. test_truth(result);
  91. const char *value= gearman_result_value(result);
  92. test_truth(value);
  93. test_compare(18UL, gearman_result_size(result));
  94. gearman_task_free(task);
  95. gearman_client_task_free_all(client);
  96. return TEST_SUCCESS;
  97. }
  98. test_return_t gearman_execute_partition_workfail(void *object)
  99. {
  100. gearman_client_st *client= (gearman_client_st *)object;
  101. const char *worker_function= (const char *)gearman_client_context(client);
  102. test_true_got(gearman_success(gearman_client_echo(client, test_literal_param("this is mine"))), gearman_client_error(client));
  103. gearman_argument_t workload= gearman_argument_make(0, 0, test_literal_param("this dog does not hunt mapper_fail"));
  104. gearman_task_st *task;
  105. test_true_got(task= gearman_execute_by_partition(client,
  106. test_literal_param("split_worker"),
  107. gearman_string_param_cstr(worker_function),
  108. NULL, 0, // unique
  109. NULL,
  110. &workload, 0), gearman_client_error(client));
  111. test_compare_got(GEARMAN_WORK_FAIL, gearman_task_return(task), gearman_task_error(task));
  112. gearman_task_free(task);
  113. gearman_client_task_free_all(client);
  114. return TEST_SUCCESS;
  115. }
  116. test_return_t gearman_execute_partition_fail_in_reduction(void *object)
  117. {
  118. gearman_client_st *client= (gearman_client_st *)object;
  119. const char *worker_function= (const char *)gearman_client_context(client);
  120. test_true_got(gearman_success(gearman_client_echo(client, test_literal_param("this is mine"))), gearman_client_error(client));
  121. gearman_argument_t workload= gearman_argument_make(0, 0, test_literal_param("this dog does not hunt fail"));
  122. gearman_task_st *task;
  123. test_true_got(task= gearman_execute_by_partition(client,
  124. test_literal_param("split_worker"),
  125. gearman_string_param_cstr(worker_function),
  126. NULL, 0, // unique
  127. NULL,
  128. &workload, 0), gearman_client_error(client));
  129. test_compare_got(GEARMAN_WORK_FAIL, gearman_task_return(task), gearman_task_error(task));
  130. gearman_task_free(task);
  131. gearman_client_task_free_all(client);
  132. return TEST_SUCCESS;
  133. }
  134. test_return_t gearman_execute_partition_use_as_function(void *object)
  135. {
  136. gearman_client_st *client= (gearman_client_st *)object;
  137. test_true_got(gearman_success(gearman_client_echo(client, test_literal_param("this is mine"))), gearman_client_error(client));
  138. // This just hear to make it easier to trace when
  139. // gearman_execute_partition() is called (look in the log to see the
  140. // failed option setting.
  141. gearman_client_set_server_option(client, test_literal_param("should fail"));
  142. gearman_argument_t workload= gearman_argument_make(0, 0, test_literal_param("this dog does not hunt"));
  143. gearman_task_st *task;
  144. test_true_got(task= gearman_execute(client,
  145. test_literal_param("split_worker"),
  146. NULL, 0, // unique
  147. NULL,
  148. &workload, 0), gearman_client_error(client));
  149. test_compare(GEARMAN_SUCCESS, gearman_task_return(task));
  150. gearman_result_st *result= gearman_task_result(task);
  151. test_truth(result);
  152. const char *value= gearman_result_value(result);
  153. test_truth(value);
  154. test_compare(18UL, gearman_result_size(result));
  155. gearman_task_free(task);
  156. gearman_client_task_free_all(client);
  157. return TEST_SUCCESS;
  158. }
  159. test_return_t gearman_execute_partition_no_aggregate(void *object)
  160. {
  161. gearman_client_st *client= (gearman_client_st *)object;
  162. gearman_argument_t workload= gearman_argument_make(0, 0, test_literal_param("this dog does not hunt"));
  163. gearman_task_st *task;
  164. test_true_got(task= gearman_execute_by_partition(client,
  165. test_literal_param("client_test"),
  166. test_literal_param("count"),
  167. NULL, 0, // unique
  168. NULL,
  169. &workload, 0), gearman_client_error(client));
  170. test_compare_got(GEARMAN_SUCCESS,
  171. gearman_task_return(task),
  172. gearman_client_error(client));
  173. test_compare(GEARMAN_SUCCESS, gearman_task_return(task));
  174. test_false(gearman_task_result(task));
  175. gearman_task_free(task);
  176. gearman_client_task_free_all(client);
  177. return TEST_SUCCESS;
  178. }