regression.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. #include "gear_config.h"
  39. #include <libtest/test.hpp>
  40. using namespace libtest;
  41. #include <cassert>
  42. #include <libgearman/gearman.h>
  43. #include <tests/regression.h>
  44. #include <libgearman/interface/universal.hpp>
  45. #include <libgearman/universal.hpp>
  46. #include <libgearman/connection.hpp>
  47. #include <libgearman/packet.hpp>
  48. #include <iostream>
  49. #include <cstring>
  50. #ifndef __INTEL_COMPILER
  51. #pragma GCC diagnostic ignored "-Wold-style-cast"
  52. #endif
  53. test_return_t regression_bug_783141_test(void *)
  54. {
  55. test_skip(true, test_is_local());
  56. #ifdef __APPLE__
  57. return TEST_SKIPPED;
  58. #endif
  59. { // Try with one bad host
  60. gearman_client_st *client= gearman_client_create(nullptr);
  61. ASSERT_TRUE(client);
  62. ASSERT_TRUE(gearman_success(gearman_client_add_server(client, "10.0.2.253", 0)));
  63. gearman_return_t ret{};
  64. gearman_task_st *task= gearman_client_add_task(client, nullptr, nullptr,
  65. "does not exist", nullptr,
  66. test_literal_param("dog"),
  67. &ret);
  68. ASSERT_EQ(ret, GEARMAN_SUCCESS);
  69. ASSERT_TRUE(task);
  70. gearman_return_t local_ret= gearman_client_run_tasks(client);
  71. ASSERT_EQ(local_ret, GEARMAN_COULD_NOT_CONNECT);
  72. local_ret= gearman_client_run_tasks(client);
  73. ASSERT_EQ(local_ret, GEARMAN_COULD_NOT_CONNECT);
  74. gearman_client_free(client);
  75. }
  76. { // Try with three bad hosts
  77. gearman_client_st *client= gearman_client_create(nullptr);
  78. ASSERT_TRUE(client);
  79. ASSERT_TRUE(gearman_success(gearman_client_add_server(client, "10.0.2.253", 0)));
  80. ASSERT_TRUE(gearman_success(gearman_client_add_server(client, "10.0.2.252", 0)));
  81. ASSERT_TRUE(gearman_success(gearman_client_add_server(client, "10.0.2.251", 0)));
  82. gearman_return_t ret{};
  83. gearman_task_st *task= gearman_client_add_task(client, nullptr, nullptr,
  84. "does not exist", nullptr,
  85. test_literal_param("dog"),
  86. &ret);
  87. ASSERT_EQ(ret, GEARMAN_SUCCESS);
  88. ASSERT_TRUE(task);
  89. gearman_return_t local_ret= gearman_client_run_tasks(client);
  90. ASSERT_EQ(local_ret, GEARMAN_COULD_NOT_CONNECT);
  91. local_ret= gearman_client_run_tasks(client);
  92. ASSERT_EQ(local_ret, GEARMAN_COULD_NOT_CONNECT);
  93. gearman_client_free(client);
  94. }
  95. return TEST_SUCCESS;
  96. }
  97. test_return_t regression_bug_372074_test(void *)
  98. {
  99. gearman_universal_st universal{};
  100. const void *args[1];
  101. size_t args_size[1];
  102. for (uint32_t x= 0; x < 2; x++)
  103. {
  104. gearman_packet_st packet{};
  105. gearman_connection_st *con_ptr{};
  106. ASSERT_TRUE(con_ptr= gearman_connection_create(universal, nullptr, default_port()));
  107. args[0]= "testUnregisterFunction";
  108. args_size[0]= strlen("testUnregisterFunction");
  109. ASSERT_TRUE(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  110. GEARMAN_COMMAND_SET_CLIENT_ID,
  111. args, args_size, 1)));
  112. ASSERT_TRUE(gearman_success(con_ptr->send_packet(packet, true)));
  113. gearman_packet_free(&packet);
  114. args[0]= "reverse";
  115. args_size[0]= strlen("reverse");
  116. ASSERT_TRUE(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST, GEARMAN_COMMAND_CAN_DO,
  117. args, args_size, 1)));
  118. ASSERT_TRUE(gearman_success(con_ptr->send_packet(packet, true)));
  119. gearman_packet_free(&packet);
  120. ASSERT_TRUE(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  121. GEARMAN_COMMAND_CANT_DO,
  122. args, args_size, 1)));
  123. ASSERT_TRUE(gearman_success(con_ptr->send_packet(packet, true)));
  124. gearman_packet_free(&packet);
  125. delete con_ptr;
  126. ASSERT_TRUE(con_ptr= gearman_connection_create(universal, nullptr, default_port()));
  127. args[0]= "testUnregisterFunction";
  128. args_size[0]= strlen("testUnregisterFunction");
  129. ASSERT_TRUE(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  130. GEARMAN_COMMAND_SET_CLIENT_ID,
  131. args, args_size, 1)));
  132. ASSERT_TRUE(gearman_success(con_ptr->send_packet(packet, true)));
  133. gearman_packet_free(&packet);
  134. args[0]= "digest";
  135. args_size[0]= strlen("digest");
  136. ASSERT_TRUE(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  137. GEARMAN_COMMAND_CAN_DO,
  138. args, args_size, 1)));
  139. ASSERT_TRUE(gearman_success(con_ptr->send_packet(packet, true)));
  140. gearman_packet_free(&packet);
  141. args[0]= "reverse";
  142. args_size[0]= strlen("reverse");
  143. ASSERT_TRUE(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  144. GEARMAN_COMMAND_CAN_DO,
  145. args, args_size, 1)));
  146. ASSERT_TRUE(gearman_success(con_ptr->send_packet(packet, true)));
  147. gearman_packet_free(&packet);
  148. ASSERT_TRUE(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  149. GEARMAN_COMMAND_RESET_ABILITIES,
  150. NULL, NULL, 0)));
  151. ASSERT_TRUE(gearman_success(con_ptr->send_packet(packet, true)));
  152. gearman_packet_free(&packet);
  153. delete con_ptr;
  154. }
  155. gearman_universal_free(universal);
  156. return TEST_SUCCESS;
  157. }
  158. test_return_t regression_768317_test(void *object)
  159. {
  160. gearman_client_st *client= (gearman_client_st *)object;
  161. ASSERT_TRUE(client);
  162. size_t result_length{};
  163. gearman_return_t rc{};
  164. char *job_result= (char*)gearman_client_do(client, "increment_reset_worker",
  165. nullptr,
  166. test_literal_param("reset"),
  167. &result_length, &rc);
  168. ASSERT_EQ(rc, GEARMAN_SUCCESS);
  169. test_false(job_result);
  170. // Check to see that the task ran just once
  171. job_result= (char*)gearman_client_do(client, "increment_reset_worker",
  172. nullptr,
  173. test_literal_param("10"),
  174. &result_length, &rc);
  175. ASSERT_EQ(rc, GEARMAN_SUCCESS);
  176. ASSERT_TRUE(job_result);
  177. long count= strtol(job_result, (char **)nullptr, 10);
  178. ASSERT_EQ(10L, count);
  179. free(job_result);
  180. // Check to see that the task ran just once out of the bg queue
  181. {
  182. gearman_job_handle_t job_handle{};
  183. rc= gearman_client_do_background(client,
  184. "increment_reset_worker",
  185. nullptr,
  186. test_literal_param("14"),
  187. job_handle);
  188. ASSERT_EQ(rc, GEARMAN_SUCCESS);
  189. bool is_known{};
  190. do {
  191. rc= gearman_client_job_status(client, job_handle, &is_known, nullptr, nullptr, nullptr);
  192. } while (gearman_continue(rc) or is_known);
  193. ASSERT_EQ(rc, GEARMAN_SUCCESS);
  194. job_result= (char*)gearman_client_do(client, "increment_reset_worker",
  195. nullptr,
  196. test_literal_param("10"),
  197. &result_length, &rc);
  198. ASSERT_EQ(rc, GEARMAN_SUCCESS);
  199. ASSERT_TRUE(job_result);
  200. count= atol(job_result);
  201. ASSERT_EQ(34L, count);
  202. free(job_result);
  203. }
  204. return TEST_SUCCESS;
  205. }