internals.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #if defined(NDEBUG)
  40. # undef NDEBUG
  41. #endif
  42. #include <assert.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #define GEARMAN_CORE
  47. #include <libgearman/common.h>
  48. #include <libgearman/packet.hpp>
  49. #include "libtest/test.h"
  50. #include "libtest/server.h"
  51. #include <libtest/worker.h>
  52. #include "libgearman/universal.hpp"
  53. #define CLIENT_TEST_PORT 32123
  54. #ifndef __INTEL_COMPILER
  55. #pragma GCC diagnostic ignored "-Wold-style-cast"
  56. #endif
  57. static test_return_t init_test(void *)
  58. {
  59. gearman_universal_st gear;
  60. gearman_universal_initialize(gear);
  61. test_false(gear.options.dont_track_packets);
  62. test_false(gear.options.non_blocking);
  63. test_false(gear.options.stored_non_blocking);
  64. test_false(gear._namespace);
  65. gearman_universal_free(gear);
  66. return TEST_SUCCESS;
  67. }
  68. static test_return_t clone_test(void *)
  69. {
  70. gearman_universal_st gear;
  71. gearman_universal_initialize(gear);
  72. /* Can we init from null? */
  73. {
  74. gearman_universal_st destination;
  75. gearman_universal_clone(destination, gear);
  76. { // Test all of the flags
  77. test_truth(destination.options.dont_track_packets == gear.options.dont_track_packets);
  78. test_truth(destination.options.non_blocking == gear.options.non_blocking);
  79. test_truth(destination.options.stored_non_blocking == gear.options.stored_non_blocking);
  80. }
  81. test_truth(destination._namespace == gear._namespace);
  82. test_truth(destination.verbose == gear.verbose);
  83. test_truth(destination.con_count == gear.con_count);
  84. test_truth(destination.packet_count == gear.packet_count);
  85. test_truth(destination.pfds_size == gear.pfds_size);
  86. test_truth(destination.error.last_errno == gear.error.last_errno);
  87. test_truth(destination.timeout == gear.timeout);
  88. test_truth(destination.con_list == gear.con_list);
  89. test_truth(destination.packet_list == gear.packet_list);
  90. test_truth(destination.pfds == gear.pfds);
  91. test_truth(destination.log_fn == gear.log_fn);
  92. test_truth(destination.log_context == gear.log_context);
  93. test_truth(destination.workload_malloc_fn == gear.workload_malloc_fn);
  94. test_truth(destination.workload_malloc_context == gear.workload_malloc_context);
  95. test_truth(destination.workload_free_fn == gear.workload_free_fn);
  96. test_truth(destination.workload_free_context == gear.workload_free_context);
  97. gearman_universal_free(gear);
  98. }
  99. gearman_universal_free(gear);
  100. return TEST_SUCCESS;
  101. }
  102. static test_return_t set_timout_test(void *)
  103. {
  104. gearman_universal_st universal;
  105. gearman_universal_initialize(universal);
  106. test_compare(-1, gearman_universal_timeout(universal)); // Current default
  107. gearman_universal_set_timeout(universal, 20);
  108. test_compare(20, gearman_universal_timeout(universal)); // New value of 20
  109. gearman_universal_set_timeout(universal, 10);
  110. test_compare(10, gearman_universal_timeout(universal)); // New value of 10
  111. gearman_universal_free(universal);
  112. return TEST_SUCCESS;
  113. }
  114. static test_return_t basic_error_test(void *)
  115. {
  116. gearman_universal_st universal;
  117. gearman_universal_initialize(universal);
  118. const char *error= gearman_universal_error(universal);
  119. test_false(error);
  120. test_compare(0, gearman_universal_errno(universal));
  121. gearman_universal_free(universal);
  122. return TEST_SUCCESS;
  123. }
  124. static test_return_t state_option_test(void *)
  125. {
  126. gearman_universal_st universal;
  127. gearman_universal_initialize(universal);
  128. { // Initial Allocated, no changes
  129. test_false(universal.options.dont_track_packets);
  130. test_false(universal.options.non_blocking);
  131. test_false(universal.options.stored_non_blocking);
  132. }
  133. gearman_universal_free(universal);
  134. return TEST_SUCCESS;
  135. }
  136. static test_return_t state_option_on_create_test(void *)
  137. {
  138. gearman_universal_st universal;
  139. gearman_universal_options_t options[]= { GEARMAN_NON_BLOCKING, GEARMAN_DONT_TRACK_PACKETS, GEARMAN_MAX};
  140. gearman_universal_initialize(universal, options);
  141. { // Initial Allocated, no changes
  142. test_truth(universal.options.dont_track_packets);
  143. test_truth(universal.options.non_blocking);
  144. test_false(universal.options.stored_non_blocking);
  145. }
  146. gearman_universal_free(universal);
  147. return TEST_SUCCESS;
  148. }
  149. static test_return_t gearman_universal_set_namespace_test(void *)
  150. {
  151. gearman_universal_st universal;
  152. gearman_universal_initialize(universal);
  153. test_false(universal._namespace);
  154. gearman_universal_set_namespace(universal, gearman_literal_param("foo23"));
  155. test_truth(universal._namespace);
  156. gearman_universal_free(universal);
  157. return TEST_SUCCESS;
  158. }
  159. static test_return_t clone_gearman_universal_set_namespace_test(void *)
  160. {
  161. gearman_universal_st universal;
  162. gearman_universal_initialize(universal);
  163. test_false(universal._namespace);
  164. gearman_universal_set_namespace(universal, gearman_literal_param("my_dog"));
  165. test_truth(universal._namespace);
  166. gearman_universal_st clone;
  167. gearman_universal_clone(clone, universal);
  168. test_truth(clone._namespace);
  169. gearman_universal_free(universal);
  170. gearman_universal_free(clone);
  171. return TEST_SUCCESS;
  172. }
  173. static test_return_t state_option_set_test(void *)
  174. {
  175. gearman_universal_st universal;
  176. gearman_universal_options_t options[]= { GEARMAN_NON_BLOCKING, GEARMAN_DONT_TRACK_PACKETS, GEARMAN_MAX};
  177. gearman_universal_initialize(universal, options);
  178. { // Initial Allocated, no changes
  179. test_truth(universal.options.dont_track_packets);
  180. test_truth(universal.options.non_blocking);
  181. test_false(universal.options.stored_non_blocking);
  182. }
  183. test_truth(gearman_universal_is_non_blocking(universal));
  184. gearman_universal_initialize(universal);
  185. { // Initial Allocated, no changes
  186. test_false(universal.options.dont_track_packets);
  187. test_false(universal.options.non_blocking);
  188. test_false(universal.options.stored_non_blocking);
  189. }
  190. gearman_universal_add_options(universal, GEARMAN_DONT_TRACK_PACKETS);
  191. { // Initial Allocated, no changes
  192. test_truth(universal.options.dont_track_packets);
  193. test_false(universal.options.non_blocking);
  194. test_false(universal.options.stored_non_blocking);
  195. }
  196. gearman_universal_remove_options(universal, GEARMAN_DONT_TRACK_PACKETS);
  197. { // Initial Allocated, no changes
  198. test_false(universal.options.dont_track_packets);
  199. test_false(universal.options.non_blocking);
  200. test_false(universal.options.stored_non_blocking);
  201. }
  202. gearman_universal_free(universal);
  203. return TEST_SUCCESS;
  204. }
  205. test_st universal_st_test[] ={
  206. {"init", 0, init_test },
  207. {"clone_test", 0, clone_test },
  208. {"set_timeout", 0, set_timout_test },
  209. {"basic_error", 0, basic_error_test },
  210. {"state_options", 0, state_option_test },
  211. {"state_options_on_create", 0, state_option_on_create_test},
  212. {"state_options_set", 0, state_option_set_test },
  213. {"gearman_universal_set_namespace()", 0, gearman_universal_set_namespace_test },
  214. {"gearman_universal_clone() with gearman_universal_set_namespace()", 0, clone_gearman_universal_set_namespace_test },
  215. {0, 0, 0}
  216. };
  217. static test_return_t connection_init_test(void *)
  218. {
  219. gearman_universal_st universal;
  220. gearman_universal_initialize(universal);
  221. gearman_connection_st *connection_ptr= gearman_connection_create(universal, NULL);
  222. test_truth(connection_ptr);
  223. test_false(connection_ptr->options.ready);
  224. test_false(connection_ptr->options.packet_in_use);
  225. delete connection_ptr;
  226. return TEST_SUCCESS;
  227. }
  228. static test_return_t connection_alloc_test(void *)
  229. {
  230. gearman_universal_st universal;
  231. gearman_universal_initialize(universal);
  232. gearman_connection_st *connection_ptr= gearman_connection_create(universal, NULL);
  233. test_truth(connection_ptr);
  234. test_false(connection_ptr->options.ready);
  235. test_false(connection_ptr->options.packet_in_use);
  236. delete connection_ptr;
  237. return TEST_SUCCESS;
  238. }
  239. test_st connection_st_test[] ={
  240. {"init", 0, connection_init_test },
  241. {"alloc", 0, connection_alloc_test },
  242. {0, 0, 0}
  243. };
  244. static test_return_t packet_init_test(void *)
  245. {
  246. gearman_universal_st universal;
  247. gearman_packet_st packet;
  248. gearman_packet_st *packet_ptr;
  249. gearman_universal_initialize(universal);
  250. packet_ptr= gearman_packet_create(universal, &packet);
  251. test_false(packet.options.allocated);
  252. test_false(packet_ptr->options.allocated);
  253. test_false(packet.options.complete);
  254. test_false(packet.options.free_data);
  255. test_truth(packet_ptr == &packet);
  256. gearman_packet_free(packet_ptr);
  257. test_false(packet.options.allocated);
  258. return TEST_SUCCESS;
  259. }
  260. static test_return_t gearman_packet_give_data_test(void *)
  261. {
  262. char *data = strdup("Mine!");
  263. size_t data_size= strlen(data);
  264. gearman_universal_st universal;
  265. gearman_packet_st packet;
  266. gearman_universal_initialize(universal);
  267. test_truth(gearman_packet_create(universal, &packet));
  268. gearman_packet_give_data(packet, data, data_size);
  269. test_truth(packet.data == data);
  270. test_truth(packet.data_size == data_size);
  271. test_truth(packet.options.free_data);
  272. gearman_packet_free(&packet);
  273. gearman_universal_free(universal);
  274. return TEST_SUCCESS;
  275. }
  276. static test_return_t gearman_packet_take_data_test(void *)
  277. {
  278. char *data = strdup("Mine!");
  279. size_t data_size= strlen(data);
  280. gearman_universal_st universal;
  281. gearman_packet_st packet;
  282. gearman_universal_initialize(universal);
  283. gearman_packet_st *packet_ptr= gearman_packet_create(universal, &packet);
  284. test_truth(packet_ptr);
  285. gearman_packet_give_data(packet, data, data_size);
  286. test_truth(packet_ptr->data == data);
  287. test_compare(data_size, packet_ptr->data_size);
  288. test_truth(packet_ptr->options.free_data);
  289. size_t mine_size;
  290. char *mine= (char *)gearman_packet_take_data(packet, &mine_size);
  291. test_false(packet_ptr->data);
  292. test_compare(0, packet_ptr->data_size);
  293. test_false(packet_ptr->options.free_data);
  294. test_compare(mine, data);
  295. test_compare(data_size, mine_size);
  296. gearman_packet_free(packet_ptr);
  297. gearman_universal_free(universal);
  298. free(data);
  299. return TEST_SUCCESS;
  300. }
  301. test_st packet_st_test[] ={
  302. {"init", 0, packet_init_test },
  303. {"gearman_packet_give_data", 0, gearman_packet_give_data_test },
  304. {"gearman_packet_take_data", 0, gearman_packet_take_data_test },
  305. {0, 0, 0}
  306. };
  307. collection_st collection[] ={
  308. {"gearman_universal_st", 0, 0, universal_st_test},
  309. {"gearman_connection_st", 0, 0, connection_st_test},
  310. {"gearman_packet_st", 0, 0, packet_st_test},
  311. {0, 0, 0, 0}
  312. };
  313. void get_world(world_st *world)
  314. {
  315. world->collections= collection;
  316. }