gearman_execute_partition.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 "tests/gearman_execute_partition.h"
  44. #include "tests/libgearman-1.0/client_test.h"
  45. #include "tests/workers/v2/echo_or_react.h"
  46. #include "tests/workers/v2/split.h"
  47. #include "tests/workers/aggregator/cat.h"
  48. #ifndef __INTEL_COMPILER
  49. #pragma GCC diagnostic ignored "-Wold-style-cast"
  50. #endif
  51. #define WORKER_FUNCTION_NAME "partition_client_test"
  52. #define WORKER_SPLIT_FUNCTION_NAME "split_worker"
  53. test_return_t partition_SETUP(void *object)
  54. {
  55. client_test_st *test= (client_test_st *)object;
  56. ASSERT_TRUE(test);
  57. test->set_worker_name(WORKER_FUNCTION_NAME);
  58. gearman_function_t echo_react_fn= gearman_function_create_v2(echo_or_react_worker_v2);
  59. test->push(test_worker_start(libtest::default_port(), NULL,
  60. test->worker_name(),
  61. echo_react_fn, NULL, gearman_worker_options_t()));
  62. gearman_function_t split_worker_fn= gearman_function_create_partition(split_worker, cat_aggregator_fn);
  63. test->push(test_worker_start(libtest::default_port(), NULL,
  64. WORKER_SPLIT_FUNCTION_NAME,
  65. split_worker_fn, NULL, GEARMAN_WORKER_GRAB_ALL));
  66. return TEST_SUCCESS;
  67. }
  68. test_return_t partition_free_SETUP(void *object)
  69. {
  70. client_test_st *test= (client_test_st *)object;
  71. ASSERT_EQ(TEST_SUCCESS, partition_SETUP(object));
  72. gearman_client_add_options(test->client(), GEARMAN_CLIENT_FREE_TASKS);
  73. return TEST_SUCCESS;
  74. }
  75. test_return_t gearman_execute_partition_check_parameters(void *object)
  76. {
  77. gearman_client_st *client= (gearman_client_st *)object;
  78. ASSERT_TRUE(client);
  79. ASSERT_EQ(GEARMAN_SUCCESS,
  80. gearman_client_echo(client, test_literal_param("this is mine")));
  81. // This just hear to make it easier to trace when gearman_execute() is
  82. // called (look in the log to see the failed option setting.
  83. gearman_client_set_server_option(client, test_literal_param("should fail"));
  84. gearman_argument_t workload= gearman_argument_make(0, 0, test_literal_param("this dog does not hunt"));
  85. // Test client as NULL
  86. gearman_task_st *task= gearman_execute_by_partition(NULL,
  87. test_literal_param(WORKER_SPLIT_FUNCTION_NAME),
  88. test_literal_param(WORKER_FUNCTION_NAME),
  89. NULL, 0, // unique
  90. NULL,
  91. &workload, 0);
  92. test_null(task);
  93. // Test no partition function
  94. task= gearman_execute_by_partition(client,
  95. NULL, 0,
  96. NULL, 0,
  97. NULL, 0, // unique
  98. NULL,
  99. &workload, 0);
  100. test_null(task);
  101. return TEST_SUCCESS;
  102. }
  103. test_return_t gearman_execute_partition_basic(void *object)
  104. {
  105. gearman_client_st *client= (gearman_client_st *)object;
  106. ASSERT_TRUE(client);
  107. ASSERT_EQ(GEARMAN_SUCCESS,
  108. gearman_client_echo(client, test_literal_param("this is mine")));
  109. // This just hear to make it easier to trace when
  110. // gearman_execute_partition() is called (look in the log to see the
  111. // failed option setting.
  112. ASSERT_FALSE(gearman_client_set_server_option(client, test_literal_param("should fail")));
  113. // This is the real work
  114. gearman_argument_t workload= gearman_argument_make(0, 0, test_literal_param("this dog does not hunt"));
  115. gearman_task_st *task= gearman_execute_by_partition(client,
  116. test_literal_param(WORKER_SPLIT_FUNCTION_NAME),
  117. test_literal_param(WORKER_FUNCTION_NAME),
  118. NULL, 0, // unique
  119. NULL,
  120. &workload, 0);
  121. ASSERT_TRUE(task);
  122. ASSERT_EQ(GEARMAN_SUCCESS, gearman_task_return(task));
  123. gearman_result_st *result= gearman_task_result(task);
  124. ASSERT_TRUE(result);
  125. const char *value= gearman_result_value(result);
  126. ASSERT_TRUE(value);
  127. ASSERT_EQ(18UL, gearman_result_size(result));
  128. gearman_task_free(task);
  129. gearman_client_task_free_all(client);
  130. return TEST_SUCCESS;
  131. }
  132. test_return_t gearman_execute_partition_workfail(void *object)
  133. {
  134. gearman_client_st *client= (gearman_client_st *)object;
  135. const char *worker_function= (const char *)gearman_client_context(client);
  136. ASSERT_EQ(GEARMAN_SUCCESS,
  137. gearman_client_echo(client, test_literal_param("this is mine")));
  138. gearman_argument_t workload= gearman_argument_make(0, 0, test_literal_param("this dog does not hunt mapper_fail"));
  139. gearman_task_st *task= gearman_execute_by_partition(client,
  140. test_literal_param(WORKER_SPLIT_FUNCTION_NAME),
  141. gearman_string_param_cstr(worker_function),
  142. NULL, 0, // unique
  143. NULL,
  144. &workload, 0);
  145. ASSERT_TRUE(task);
  146. ASSERT_EQ(GEARMAN_WORK_EXCEPTION, gearman_task_return(task));
  147. gearman_task_free(task);
  148. gearman_client_task_free_all(client);
  149. return TEST_SUCCESS;
  150. }
  151. test_return_t gearman_execute_partition_fail_in_reduction(void *object)
  152. {
  153. gearman_client_st *client= (gearman_client_st *)object;
  154. const char *worker_function= (const char *)gearman_client_context(client);
  155. ASSERT_EQ(gearman_client_echo(client, test_literal_param("this is mine")), GEARMAN_SUCCESS);
  156. gearman_argument_t workload= gearman_argument_make(0, 0, test_literal_param("this dog does not hunt fail"));
  157. gearman_task_st *task= gearman_execute_by_partition(client,
  158. test_literal_param(WORKER_SPLIT_FUNCTION_NAME),
  159. gearman_string_param_cstr(worker_function),
  160. NULL, 0, // unique
  161. NULL,
  162. &workload, 0);
  163. ASSERT_TRUE(task);
  164. ASSERT_EQ(GEARMAN_WORK_FAIL, gearman_task_return(task));
  165. gearman_task_free(task);
  166. gearman_client_task_free_all(client);
  167. return TEST_SUCCESS;
  168. }
  169. test_return_t gearman_execute_partition_use_as_function(void *object)
  170. {
  171. gearman_client_st *client= (gearman_client_st *)object;
  172. ASSERT_EQ(gearman_client_echo(client, test_literal_param("this is mine")), GEARMAN_SUCCESS);
  173. // This just hear to make it easier to trace when
  174. // gearman_execute_partition() is called (look in the log to see the
  175. // failed option setting.
  176. gearman_client_set_server_option(client, test_literal_param("should fail"));
  177. gearman_argument_t workload= gearman_argument_make(0, 0, test_literal_param("this dog does not hunt"));
  178. gearman_task_st *task= gearman_execute(client,
  179. test_literal_param(WORKER_SPLIT_FUNCTION_NAME),
  180. NULL, 0, // unique
  181. NULL,
  182. &workload, 0);
  183. ASSERT_TRUE(task);
  184. ASSERT_EQ(GEARMAN_SUCCESS, gearman_task_return(task));
  185. gearman_result_st *result= gearman_task_result(task);
  186. ASSERT_TRUE(result);
  187. const char *value= gearman_result_value(result);
  188. ASSERT_TRUE(value);
  189. ASSERT_EQ(18UL, gearman_result_size(result));
  190. gearman_task_free(task);
  191. gearman_client_task_free_all(client);
  192. return TEST_SUCCESS;
  193. }
  194. test_return_t gearman_execute_partition_no_aggregate(void *object)
  195. {
  196. gearman_client_st *client= (gearman_client_st *)object;
  197. gearman_argument_t workload= gearman_argument_make(0, 0, test_literal_param("this dog does not hunt"));
  198. gearman_task_st *task= gearman_execute_by_partition(client,
  199. test_literal_param(WORKER_FUNCTION_NAME),
  200. test_literal_param("count"),
  201. NULL, 0, // unique
  202. NULL,
  203. &workload, 0);
  204. ASSERT_TRUE(task);
  205. ASSERT_EQ(GEARMAN_SUCCESS,
  206. gearman_task_return(task));
  207. ASSERT_EQ(GEARMAN_SUCCESS, gearman_task_return(task));
  208. test_false(gearman_task_result(task));
  209. gearman_task_free(task);
  210. gearman_client_task_free_all(client);
  211. return TEST_SUCCESS;
  212. }