internals.cc 13 KB

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