internals.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 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 <cstdio>
  42. #include <cstdlib>
  43. #include <cstring>
  44. #define GEARMAN_CORE
  45. #include "libgearman/common.h"
  46. #include "libgearman/packet.hpp"
  47. #include "libgearman/universal.hpp"
  48. #include "tests/regression.h"
  49. #ifndef __INTEL_COMPILER
  50. #pragma GCC diagnostic ignored "-Wold-style-cast"
  51. #endif
  52. struct internal_test_st
  53. {
  54. pid_t gearmand_pid;
  55. internal_test_st() :
  56. gearmand_pid(-1)
  57. {
  58. }
  59. ~internal_test_st()
  60. { }
  61. };
  62. static test_return_t init_test(void *)
  63. {
  64. gearman_universal_st gear;
  65. ASSERT_FALSE(gear.options.non_blocking);
  66. ASSERT_FALSE(gear._namespace);
  67. gearman_universal_free(gear);
  68. return TEST_SUCCESS;
  69. }
  70. static test_return_t clone_test(void *)
  71. {
  72. gearman_universal_st gear;
  73. /* Can we init from null? */
  74. {
  75. gearman_universal_st destination;
  76. gearman_universal_clone(destination, gear);
  77. { // Test all of the flags
  78. ASSERT_EQ(destination.options.non_blocking, gear.options.non_blocking);
  79. }
  80. ASSERT_EQ(destination._namespace, gear._namespace);
  81. ASSERT_EQ(destination.verbose, gear.verbose);
  82. ASSERT_EQ(destination.con_count, gear.con_count);
  83. ASSERT_EQ(destination.packet_count, gear.packet_count);
  84. ASSERT_EQ(destination.pfds_size, gear.pfds_size);
  85. ASSERT_EQ(destination.last_errno(), gear.last_errno());
  86. ASSERT_EQ(destination.timeout, gear.timeout);
  87. ASSERT_EQ(destination.con_list, gear.con_list);
  88. ASSERT_EQ(destination.packet_list, gear.packet_list);
  89. ASSERT_EQ(destination.pfds, gear.pfds);
  90. ASSERT_EQ(destination.log_fn, gear.log_fn);
  91. ASSERT_EQ(destination.log_context, gear.log_context);
  92. ASSERT_EQ(destination.allocator.malloc, gear.allocator.malloc);
  93. ASSERT_EQ(destination.allocator.context, gear.allocator.context);
  94. ASSERT_EQ(destination.allocator.free, gear.allocator.free);
  95. gearman_universal_free(gear);
  96. }
  97. gearman_universal_free(gear);
  98. return TEST_SUCCESS;
  99. }
  100. static test_return_t set_timout_test(void *)
  101. {
  102. gearman_universal_st universal;
  103. ASSERT_EQ(-1, gearman_universal_timeout(universal)); // Current default
  104. gearman_universal_set_timeout(universal, 20);
  105. ASSERT_EQ(20, gearman_universal_timeout(universal)); // New value of 20
  106. gearman_universal_set_timeout(universal, 10);
  107. ASSERT_EQ(10, gearman_universal_timeout(universal)); // New value of 10
  108. gearman_universal_free(universal);
  109. return TEST_SUCCESS;
  110. }
  111. static test_return_t basic_error_test(void *)
  112. {
  113. gearman_universal_st universal;
  114. const char *error= gearman_universal_error(universal);
  115. ASSERT_FALSE(error);
  116. ASSERT_EQ(0, gearman_universal_errno(universal));
  117. gearman_universal_free(universal);
  118. return TEST_SUCCESS;
  119. }
  120. static test_return_t state_option_test(void *)
  121. {
  122. gearman_universal_st universal;
  123. { // Initial Allocated, no changes
  124. ASSERT_FALSE(universal.options.non_blocking);
  125. }
  126. gearman_universal_free(universal);
  127. return TEST_SUCCESS;
  128. }
  129. static test_return_t state_option_on_create_test(void *)
  130. {
  131. universal_options_t options[]= { GEARMAN_UNIVERSAL_NON_BLOCKING, GEARMAN_UNIVERSAL_DONT_TRACK_PACKETS, GEARMAN_UNIVERSAL_MAX};
  132. gearman_universal_st universal(options);
  133. { // Initial Allocated, no changes
  134. ASSERT_TRUE(universal.options.non_blocking);
  135. }
  136. gearman_universal_free(universal);
  137. return TEST_SUCCESS;
  138. }
  139. static test_return_t gearman_universal_set_namespace_test(void *)
  140. {
  141. gearman_universal_st universal;
  142. ASSERT_FALSE(universal._namespace);
  143. gearman_universal_set_namespace(universal, gearman_literal_param("foo23"));
  144. ASSERT_TRUE(universal._namespace);
  145. gearman_universal_free(universal);
  146. return TEST_SUCCESS;
  147. }
  148. static test_return_t clone_gearman_universal_set_namespace_test(void *)
  149. {
  150. gearman_universal_st universal;
  151. ASSERT_FALSE(universal._namespace);
  152. gearman_universal_set_namespace(universal, gearman_literal_param("my_dog"));
  153. ASSERT_TRUE(universal._namespace);
  154. gearman_universal_st clone;
  155. gearman_universal_clone(clone, universal);
  156. ASSERT_TRUE(clone._namespace);
  157. gearman_universal_free(universal);
  158. gearman_universal_free(clone);
  159. return TEST_SUCCESS;
  160. }
  161. static test_return_t state_option_set_test(void *)
  162. {
  163. universal_options_t options[]= { GEARMAN_UNIVERSAL_NON_BLOCKING, GEARMAN_UNIVERSAL_DONT_TRACK_PACKETS, GEARMAN_UNIVERSAL_MAX};
  164. gearman_universal_st universal(options);
  165. { // Initial Allocated, no changes
  166. ASSERT_TRUE(universal.options.non_blocking);
  167. }
  168. ASSERT_TRUE(gearman_universal_is_non_blocking(universal));
  169. { // Initial Allocated, no changes
  170. ASSERT_TRUE(universal.options.non_blocking);
  171. }
  172. gearman_universal_add_options(universal, GEARMAN_UNIVERSAL_DONT_TRACK_PACKETS);
  173. { // Initial Allocated, no changes
  174. ASSERT_TRUE(universal.options.non_blocking);
  175. }
  176. gearman_universal_remove_options(universal, GEARMAN_UNIVERSAL_DONT_TRACK_PACKETS);
  177. { // Initial Allocated, no changes
  178. ASSERT_TRUE(universal.options.non_blocking);
  179. }
  180. gearman_universal_free(universal);
  181. return TEST_SUCCESS;
  182. }
  183. test_st universal_st_test[] ={
  184. {"init", 0, init_test },
  185. {"clone_test", 0, clone_test },
  186. {"set_timeout", 0, set_timout_test },
  187. {"basic_error", 0, basic_error_test },
  188. {"state_options", 0, state_option_test },
  189. {"state_options_on_create", 0, state_option_on_create_test},
  190. {"state_options_set", 0, state_option_set_test },
  191. {"gearman_universal_set_namespace()", 0, gearman_universal_set_namespace_test },
  192. {"gearman_universal_clone() with gearman_universal_set_namespace()", 0, clone_gearman_universal_set_namespace_test },
  193. {0, 0, 0}
  194. };
  195. static test_return_t connection_init_test(void *)
  196. {
  197. gearman_universal_st universal;
  198. gearman_connection_st *connection_ptr= gearman_connection_create(universal, NULL, (const char*)(GEARMAN_DEFAULT_TCP_PORT_STRING));
  199. ASSERT_TRUE(connection_ptr);
  200. ASSERT_FALSE(connection_ptr->options.ready);
  201. ASSERT_FALSE(connection_ptr->options.packet_in_use);
  202. delete connection_ptr;
  203. return TEST_SUCCESS;
  204. }
  205. static test_return_t connection_alloc_test(void *)
  206. {
  207. gearman_universal_st universal;
  208. gearman_connection_st *connection_ptr= gearman_connection_create(universal, NULL, (const char*)(GEARMAN_DEFAULT_TCP_PORT_STRING));
  209. ASSERT_TRUE(connection_ptr);
  210. ASSERT_FALSE(connection_ptr->options.ready);
  211. ASSERT_FALSE(connection_ptr->options.packet_in_use);
  212. delete connection_ptr;
  213. return TEST_SUCCESS;
  214. }
  215. test_st connection_st_test[] ={
  216. {"init", 0, connection_init_test },
  217. {"alloc", 0, connection_alloc_test },
  218. {0, 0, 0}
  219. };
  220. static test_return_t packet_init_test(void *)
  221. {
  222. gearman_universal_st universal;
  223. gearman_packet_st packet;
  224. gearman_packet_st *packet_ptr;
  225. packet_ptr= gearman_packet_create(universal, packet);
  226. ASSERT_FALSE(packet.options.is_allocated);
  227. ASSERT_FALSE(packet_ptr->options.is_allocated);
  228. ASSERT_FALSE(packet.options.complete);
  229. ASSERT_FALSE(packet.options.free_data);
  230. ASSERT_EQ(packet_ptr, &packet);
  231. gearman_packet_free(packet_ptr);
  232. ASSERT_FALSE(packet.options.is_allocated);
  233. return TEST_SUCCESS;
  234. }
  235. static test_return_t gearman_packet_give_data_test(void *)
  236. {
  237. // @note Since this is a give data, ignore any errors that believe there is
  238. // an implicit memory leak.
  239. size_t data_size= test_literal_param_size("Mine!");
  240. char *data= (char *)calloc(data_size +1, sizeof(char));
  241. ASSERT_TRUE(data);
  242. memcpy(data, "Mine!", data_size);
  243. gearman_universal_st universal;
  244. gearman_packet_st packet;
  245. ASSERT_TRUE(gearman_packet_create(universal, packet));
  246. gearman_packet_give_data(packet, data, data_size);
  247. ASSERT_EQ(packet.data, data);
  248. ASSERT_EQ(packet.data_size, data_size);
  249. ASSERT_TRUE(packet.options.free_data);
  250. gearman_packet_free(&packet);
  251. gearman_universal_free(universal);
  252. return TEST_SUCCESS;
  253. }
  254. static test_return_t gearman_packet_take_data_test(void *)
  255. {
  256. // Since this is a take data, ignore any errors that believe there is an
  257. // implicit memory leak.
  258. size_t data_size= test_literal_param_size("Mine!");
  259. char *data= (char *)calloc(data_size +1, sizeof(char));
  260. ASSERT_TRUE(data);
  261. memcpy(data, "Mine!", data_size);
  262. gearman_universal_st universal;
  263. gearman_packet_st packet;
  264. gearman_packet_st *packet_ptr= gearman_packet_create(universal, packet);
  265. ASSERT_TRUE(packet_ptr);
  266. gearman_packet_give_data(packet, data, data_size);
  267. ASSERT_EQ(packet_ptr->data, data);
  268. ASSERT_EQ(data_size, packet_ptr->data_size);
  269. ASSERT_TRUE(packet_ptr->options.free_data);
  270. size_t mine_size;
  271. char *mine= (char *)gearman_packet_take_data(packet, &mine_size);
  272. ASSERT_FALSE(packet_ptr->data);
  273. test_zero(packet_ptr->data_size);
  274. ASSERT_FALSE(packet_ptr->options.free_data);
  275. test_strcmp(mine, "Mine!");
  276. ASSERT_EQ(data_size, mine_size);
  277. gearman_packet_free(packet_ptr);
  278. gearman_universal_free(universal);
  279. free(mine);
  280. return TEST_SUCCESS;
  281. }
  282. test_st packet_st_test[] ={
  283. {"init", 0, packet_init_test },
  284. {"gearman_packet_give_data", 0, gearman_packet_give_data_test },
  285. {"gearman_packet_take_data", 0, gearman_packet_take_data_test },
  286. {0, 0, 0}
  287. };
  288. test_st regression_tests[] ={
  289. {"lp:783141, multiple calls for bad host", 0, regression_bug_783141_test },
  290. {"lp:372074", 0, regression_bug_372074_test },
  291. {0, 0, 0}
  292. };
  293. collection_st collection[] ={
  294. {"gearman_universal_st", 0, 0, universal_st_test},
  295. {"gearman_connection_st", 0, 0, connection_st_test},
  296. {"gearman_packet_st", 0, 0, packet_st_test},
  297. {"regression", 0, 0, regression_tests},
  298. {0, 0, 0, 0}
  299. };
  300. static void *world_create(server_startup_st& servers, test_return_t& error)
  301. {
  302. /**
  303. We start up everything before we allocate so that we don't have to track memory in the forked process.
  304. */
  305. if (server_startup(servers, "gearmand", libtest::default_port(), NULL) == false)
  306. {
  307. error= TEST_SKIPPED;
  308. return NULL;
  309. }
  310. return NULL;
  311. }
  312. void get_world(libtest::Framework *world)
  313. {
  314. world->collections(collection);
  315. world->create(world_create);
  316. }