client_test.cc 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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. #include <ctime>
  46. #include <iostream>
  47. #include <vector>
  48. #include <boost/foreach.hpp>
  49. #include <tests/ports.h>
  50. #define GEARMAN_CORE
  51. #include <libgearman/gearman.h>
  52. #include <tests/start_worker.h>
  53. #define NAMESPACE_KEY "foo123"
  54. #define WORKER_FUNCTION_NAME "client_test"
  55. #define WORKER_CHUNKED_FUNCTION_NAME "reverse_test"
  56. #define WORKER_UNIQUE_FUNCTION_NAME "unique_test"
  57. #define WORKER_SPLIT_FUNCTION_NAME "split_worker"
  58. #include <tests/do.h>
  59. #include <tests/server_options.h>
  60. #include <tests/do_background.h>
  61. #include <tests/execute.h>
  62. #include <tests/gearman_client_do_job_handle.h>
  63. #include <tests/gearman_execute_partition.h>
  64. #include <tests/protocol.h>
  65. #include <tests/regression.h>
  66. #include <tests/task.h>
  67. #include <tests/unique.h>
  68. #include <tests/workers.h>
  69. #include <tests/workers_v1.h>
  70. #ifndef __INTEL_COMPILER
  71. #pragma GCC diagnostic ignored "-Wold-style-cast"
  72. #endif
  73. struct client_test_st
  74. {
  75. gearman_client_st *_client;
  76. bool _clone;
  77. std::vector<worker_handle_st *> workers;
  78. const char *_worker_name;
  79. client_test_st() :
  80. _clone(true),
  81. _worker_name(WORKER_FUNCTION_NAME)
  82. {
  83. if (not (_client= gearman_client_create(NULL)))
  84. {
  85. abort(); // This would only happen from a programming error
  86. }
  87. }
  88. ~client_test_st()
  89. {
  90. for (std::vector<worker_handle_st *>::iterator iter= workers.begin(); iter != workers.end(); iter++)
  91. {
  92. delete *iter;
  93. }
  94. workers.clear();
  95. gearman_client_free(_client);
  96. }
  97. void push(worker_handle_st *arg)
  98. {
  99. workers.push_back(arg);
  100. }
  101. const char *worker_name() const
  102. {
  103. return _worker_name;
  104. }
  105. void set_worker_name(const char *arg)
  106. {
  107. _worker_name= arg;
  108. }
  109. void set_clone(bool arg)
  110. {
  111. _clone= arg;
  112. }
  113. bool clone() const
  114. {
  115. return _clone;
  116. }
  117. gearman_client_st *client()
  118. {
  119. return _client;
  120. }
  121. void reset_client()
  122. {
  123. gearman_client_free(_client);
  124. _client= gearman_client_create(NULL);
  125. }
  126. };
  127. #ifndef __INTEL_COMPILER
  128. #pragma GCC diagnostic ignored "-Wold-style-cast"
  129. #endif
  130. /**
  131. @note Just here until I fix libhashkit.
  132. */
  133. static uint32_t internal_generate_hash(const char *key, size_t key_length)
  134. {
  135. const char *ptr= key;
  136. uint32_t value= 0;
  137. while (key_length--)
  138. {
  139. uint32_t val= (uint32_t) *ptr++;
  140. value += val;
  141. value += (value << 10);
  142. value ^= (value >> 6);
  143. }
  144. value += (value << 3);
  145. value ^= (value >> 11);
  146. value += (value << 15);
  147. return value == 0 ? 1 : (uint32_t) value;
  148. }
  149. /* Prototypes */
  150. void *client_test_temp_worker(gearman_job_st *job, void *context,
  151. size_t *result_size, gearman_return_t *ret_ptr);
  152. static void *client_thread(void *object)
  153. {
  154. volatile gearman_return_t *ret= (volatile gearman_return_t *)object;
  155. gearman_client_st client;
  156. gearman_client_st *client_ptr;
  157. size_t result_size;
  158. client_ptr= gearman_client_create(&client);
  159. if (client_ptr == NULL)
  160. {
  161. *ret= GEARMAN_MEMORY_ALLOCATION_FAILURE;
  162. pthread_exit(0);
  163. }
  164. gearman_return_t rc= gearman_client_add_server(&client, NULL, CLIENT_TEST_PORT);
  165. if (gearman_failed(rc))
  166. {
  167. *ret= rc;
  168. pthread_exit(0);
  169. }
  170. gearman_client_set_timeout(&client, 400);
  171. for (size_t x= 0; x < 5; x++)
  172. {
  173. (void)gearman_client_do(&client, "client_test_temp", NULL, NULL, 0, &result_size, &rc);
  174. if (gearman_failed(rc))
  175. {
  176. *ret= rc;
  177. pthread_exit(0);
  178. }
  179. }
  180. gearman_client_free(client_ptr);
  181. pthread_exit(0);
  182. }
  183. static test_return_t init_test(void *)
  184. {
  185. gearman_client_st client;
  186. test_truth(gearman_client_create(&client));
  187. gearman_client_free(&client);
  188. return TEST_SUCCESS;
  189. }
  190. static test_return_t allocation_test(void *)
  191. {
  192. gearman_client_st *client;
  193. test_truth(client= gearman_client_create(NULL));
  194. gearman_client_free(client);
  195. return TEST_SUCCESS;
  196. }
  197. static test_return_t clone_test(void *object)
  198. {
  199. const gearman_client_st *from= (gearman_client_st *)object;
  200. gearman_client_st *from_with_host;
  201. gearman_client_st *client;
  202. client= gearman_client_clone(NULL, NULL);
  203. test_truth(client);
  204. test_truth(client->options.allocated);
  205. gearman_client_free(client);
  206. client= gearman_client_clone(NULL, from);
  207. test_truth(client);
  208. gearman_client_free(client);
  209. from_with_host= gearman_client_create(NULL);
  210. test_truth(from_with_host);
  211. gearman_client_add_server(from_with_host, "127.0.0.1", 12345);
  212. client= gearman_client_clone(NULL, from_with_host);
  213. test_truth(client);
  214. test_truth(client->universal.con_list);
  215. test_truth(gearman_client_compare(client, from_with_host));
  216. gearman_client_free(client);
  217. gearman_client_free(from_with_host);
  218. return TEST_SUCCESS;
  219. }
  220. static test_return_t option_test(void *)
  221. {
  222. gearman_client_st *gear;
  223. gearman_client_options_t default_options;
  224. gear= gearman_client_create(NULL);
  225. test_truth(gear);
  226. { // Initial Allocated, no changes
  227. test_truth(gear->options.allocated);
  228. test_false(gear->options.non_blocking);
  229. test_false(gear->options.unbuffered_result);
  230. test_false(gear->options.no_new);
  231. test_false(gear->options.free_tasks);
  232. }
  233. /* Set up for default options */
  234. default_options= gearman_client_options(gear);
  235. /*
  236. We take the basic options, and push
  237. them back in. See if we change anything.
  238. */
  239. gearman_client_set_options(gear, default_options);
  240. { // Initial Allocated, no changes
  241. test_truth(gear->options.allocated);
  242. test_false(gear->options.non_blocking);
  243. test_false(gear->options.unbuffered_result);
  244. test_false(gear->options.no_new);
  245. test_false(gear->options.free_tasks);
  246. }
  247. /*
  248. We will trying to modify non-mutable options (which should not be allowed)
  249. */
  250. {
  251. gearman_client_remove_options(gear, GEARMAN_CLIENT_ALLOCATED);
  252. { // Initial Allocated, no changes
  253. test_truth(gear->options.allocated);
  254. test_false(gear->options.non_blocking);
  255. test_false(gear->options.unbuffered_result);
  256. test_false(gear->options.no_new);
  257. test_false(gear->options.free_tasks);
  258. }
  259. gearman_client_remove_options(gear, GEARMAN_CLIENT_NO_NEW);
  260. { // Initial Allocated, no changes
  261. test_truth(gear->options.allocated);
  262. test_false(gear->options.non_blocking);
  263. test_false(gear->options.unbuffered_result);
  264. test_false(gear->options.no_new);
  265. test_false(gear->options.free_tasks);
  266. }
  267. }
  268. /*
  269. We will test modifying GEARMAN_CLIENT_NON_BLOCKING in several manners.
  270. */
  271. {
  272. gearman_client_remove_options(gear, GEARMAN_CLIENT_NON_BLOCKING);
  273. { // GEARMAN_CLIENT_NON_BLOCKING set to default, by default.
  274. test_truth(gear->options.allocated);
  275. test_false(gear->options.non_blocking);
  276. test_false(gear->options.unbuffered_result);
  277. test_false(gear->options.no_new);
  278. test_false(gear->options.free_tasks);
  279. }
  280. gearman_client_add_options(gear, GEARMAN_CLIENT_NON_BLOCKING);
  281. { // GEARMAN_CLIENT_NON_BLOCKING set to default, by default.
  282. test_truth(gear->options.allocated);
  283. test_truth(gear->options.non_blocking);
  284. test_false(gear->options.unbuffered_result);
  285. test_false(gear->options.no_new);
  286. test_false(gear->options.free_tasks);
  287. }
  288. gearman_client_set_options(gear, GEARMAN_CLIENT_NON_BLOCKING);
  289. { // GEARMAN_CLIENT_NON_BLOCKING set to default, by default.
  290. test_truth(gear->options.allocated);
  291. test_truth(gear->options.non_blocking);
  292. test_false(gear->options.unbuffered_result);
  293. test_false(gear->options.no_new);
  294. test_false(gear->options.free_tasks);
  295. }
  296. gearman_client_set_options(gear, GEARMAN_CLIENT_UNBUFFERED_RESULT);
  297. { // Everything is now set to false except GEARMAN_CLIENT_UNBUFFERED_RESULT, and non-mutable options
  298. test_truth(gear->options.allocated);
  299. test_false(gear->options.non_blocking);
  300. test_truth(gear->options.unbuffered_result);
  301. test_false(gear->options.no_new);
  302. test_false(gear->options.free_tasks);
  303. }
  304. /*
  305. Reset options to default. Then add an option, and then add more options. Make sure
  306. the options are all additive.
  307. */
  308. {
  309. gearman_client_set_options(gear, default_options);
  310. { // See if we return to defaults
  311. test_truth(gear->options.allocated);
  312. test_false(gear->options.non_blocking);
  313. test_false(gear->options.unbuffered_result);
  314. test_false(gear->options.no_new);
  315. test_false(gear->options.free_tasks);
  316. }
  317. gearman_client_add_options(gear, GEARMAN_CLIENT_FREE_TASKS);
  318. { // All defaults, except timeout_return
  319. test_truth(gear->options.allocated);
  320. test_false(gear->options.non_blocking);
  321. test_false(gear->options.unbuffered_result);
  322. test_false(gear->options.no_new);
  323. test_truth(gear->options.free_tasks);
  324. }
  325. gearman_client_add_options(gear, (gearman_client_options_t)(GEARMAN_CLIENT_NON_BLOCKING|GEARMAN_CLIENT_UNBUFFERED_RESULT));
  326. { // GEARMAN_CLIENT_NON_BLOCKING set to default, by default.
  327. test_truth(gear->options.allocated);
  328. test_truth(gear->options.non_blocking);
  329. test_truth(gear->options.unbuffered_result);
  330. test_false(gear->options.no_new);
  331. test_truth(gear->options.free_tasks);
  332. }
  333. }
  334. /*
  335. Add an option, and then replace with that option plus a new option.
  336. */
  337. {
  338. gearman_client_set_options(gear, default_options);
  339. { // See if we return to defaults
  340. test_truth(gear->options.allocated);
  341. test_false(gear->options.non_blocking);
  342. test_false(gear->options.unbuffered_result);
  343. test_false(gear->options.no_new);
  344. test_false(gear->options.free_tasks);
  345. }
  346. gearman_client_add_options(gear, GEARMAN_CLIENT_FREE_TASKS);
  347. { // All defaults, except timeout_return
  348. test_truth(gear->options.allocated);
  349. test_false(gear->options.non_blocking);
  350. test_false(gear->options.unbuffered_result);
  351. test_false(gear->options.no_new);
  352. test_truth(gear->options.free_tasks);
  353. }
  354. gearman_client_add_options(gear, (gearman_client_options_t)(GEARMAN_CLIENT_FREE_TASKS|GEARMAN_CLIENT_UNBUFFERED_RESULT));
  355. { // GEARMAN_CLIENT_NON_BLOCKING set to default, by default.
  356. test_truth(gear->options.allocated);
  357. test_false(gear->options.non_blocking);
  358. test_truth(gear->options.unbuffered_result);
  359. test_false(gear->options.no_new);
  360. test_truth(gear->options.free_tasks);
  361. }
  362. }
  363. }
  364. gearman_client_free(gear);
  365. return TEST_SUCCESS;
  366. }
  367. static test_return_t echo_test(void *object)
  368. {
  369. gearman_client_st *client= (gearman_client_st *)object;
  370. test_truth(client);
  371. gearman_string_t value= { test_literal_param("This is my echo test") };
  372. test_compare(GEARMAN_SUCCESS, gearman_client_echo(client, gearman_string_param(value)));
  373. return TEST_SUCCESS;
  374. }
  375. static test_return_t submit_job_test(void *object)
  376. {
  377. gearman_client_st *client= (gearman_client_st *)object;
  378. const char *worker_function= (const char *)gearman_client_context(client);
  379. gearman_string_t value= { test_literal_param("submit_job_test") };
  380. size_t result_length;
  381. gearman_return_t rc;
  382. void *job_result= gearman_client_do(client, worker_function, NULL, gearman_string_param(value), &result_length, &rc);
  383. test_compare_got(GEARMAN_SUCCESS,
  384. rc, gearman_client_error(client) ? gearman_client_error(client) : gearman_strerror(rc));
  385. test_truth(job_result);
  386. test_compare(gearman_size(value), result_length);
  387. test_memcmp(gearman_c_str(value), job_result, gearman_size(value));
  388. free(job_result);
  389. return TEST_SUCCESS;
  390. }
  391. static test_return_t submit_null_job_test(void *object)
  392. {
  393. gearman_client_st *client= (gearman_client_st *)object;
  394. test_truth(client);
  395. const char *worker_function= (const char *)gearman_client_context(client);
  396. test_truth(worker_function);
  397. size_t result_length;
  398. gearman_return_t rc;
  399. void *job_result= gearman_client_do(client, worker_function, NULL, NULL, 0,
  400. &result_length, &rc);
  401. test_compare_got(GEARMAN_SUCCESS, rc, gearman_client_error(client));
  402. test_zero(result_length);
  403. test_false(job_result);
  404. return TEST_SUCCESS;
  405. }
  406. static test_return_t submit_exception_job_test(void *object)
  407. {
  408. gearman_client_st *client= (gearman_client_st *)object;
  409. test_truth(client);
  410. const char *worker_function= (const char *)gearman_client_context(client);
  411. test_truth(worker_function);
  412. size_t result_length;
  413. gearman_return_t rc;
  414. void *job_result= gearman_client_do(client, worker_function, NULL,
  415. test_literal_param("exception"),
  416. &result_length, &rc);
  417. test_compare_got(GEARMAN_SUCCESS, rc, gearman_client_error(client) ? gearman_client_error(client) : gearman_strerror(rc));
  418. test_memcmp("exception", job_result, result_length);
  419. free(job_result);
  420. return TEST_SUCCESS;
  421. }
  422. static test_return_t submit_warning_job_test(void *object)
  423. {
  424. gearman_client_st *client= (gearman_client_st *)object;
  425. test_truth(client);
  426. const char *worker_function= (const char *)gearman_client_context(client);
  427. test_truth(worker_function);
  428. size_t result_length;
  429. gearman_return_t rc;
  430. void *job_result= gearman_client_do(client, worker_function, NULL,
  431. test_literal_param("warning"),
  432. &result_length, &rc);
  433. test_compare_got(GEARMAN_SUCCESS, rc, gearman_client_error(client) ? gearman_client_error(client) : gearman_strerror(rc));
  434. test_memcmp("warning", job_result, result_length);
  435. free(job_result);
  436. return TEST_SUCCESS;
  437. }
  438. static test_return_t submit_fail_job_test(void *object)
  439. {
  440. gearman_client_st *client= (gearman_client_st *)object;
  441. test_truth(client);
  442. const char *worker_function= (const char *)gearman_client_context(client);
  443. test_truth(worker_function);
  444. size_t result_length;
  445. gearman_return_t rc;
  446. void *job_result= gearman_client_do(client, worker_function, NULL, "fail", 4,
  447. &result_length, &rc);
  448. test_compare_got(GEARMAN_WORK_FAIL, rc, gearman_client_error(client));
  449. test_false(job_result);
  450. test_false(result_length);
  451. return TEST_SUCCESS;
  452. }
  453. static test_return_t submit_multiple_do(void *object)
  454. {
  455. for (uint32_t x= 0; x < 100 /* arbitrary */; x++)
  456. {
  457. uint32_t option= random() %3;
  458. switch (option)
  459. {
  460. case 0:
  461. test_compare(TEST_SUCCESS, submit_null_job_test(object));
  462. break;
  463. case 1:
  464. test_compare(TEST_SUCCESS, submit_job_test(object));
  465. break;
  466. default:
  467. case 2:
  468. test_compare(TEST_SUCCESS, submit_fail_job_test(object));
  469. break;
  470. }
  471. }
  472. return TEST_SUCCESS;
  473. }
  474. static test_return_t gearman_client_job_status_test(void *object)
  475. {
  476. gearman_client_st *client= (gearman_client_st *)object;
  477. test_truth(client);
  478. gearman_string_t value= { test_literal_param("background_test") };
  479. const char *worker_function= (const char *)gearman_client_context(client);
  480. test_truth(worker_function);
  481. gearman_job_handle_t job_handle;
  482. test_compare_got(GEARMAN_SUCCESS,
  483. gearman_client_do_background(client, worker_function, NULL, gearman_string_param(value), job_handle),
  484. gearman_client_error(client));
  485. gearman_return_t ret;
  486. bool is_known;
  487. do
  488. {
  489. bool is_running;
  490. uint32_t numerator;
  491. uint32_t denominator;
  492. test_compare_got(GEARMAN_SUCCESS,
  493. ret= gearman_client_job_status(client, job_handle, &is_known, &is_running, &numerator, &denominator),
  494. gearman_client_error(client));
  495. } while (gearman_continue(ret) and is_known);
  496. return TEST_SUCCESS;
  497. }
  498. static void* test_malloc_fn(size_t size, void *context)
  499. {
  500. bool *malloc_check= (bool *)context;
  501. *malloc_check= true;
  502. return malloc(size);
  503. }
  504. static void test_free_fn(void *ptr, void *context)
  505. {
  506. bool *free_check= (bool *)context;
  507. *free_check= true;
  508. return free(ptr);
  509. }
  510. static test_return_t gearman_client_set_workload_malloc_fn_test(void *object)
  511. {
  512. gearman_client_st *client= (gearman_client_st *)object;
  513. test_truth(client);
  514. bool malloc_check= false;
  515. gearman_client_set_workload_malloc_fn(client, test_malloc_fn, &malloc_check);
  516. test_compare(TEST_SUCCESS, submit_job_test(object));
  517. test_compare(true, malloc_check);
  518. return TEST_SUCCESS;
  519. }
  520. static test_return_t gearman_client_set_workload_free_fn_test(void *object)
  521. {
  522. gearman_client_st *client= (gearman_client_st *)object;
  523. test_truth(client);
  524. bool free_check= false;
  525. gearman_client_set_workload_free_fn(client, test_free_fn, &free_check);
  526. test_compare(TEST_SUCCESS, submit_job_test(object));
  527. test_compare(true, free_check);
  528. return TEST_SUCCESS;
  529. }
  530. struct _alloc_test_st {
  531. int64_t count;
  532. int64_t total;
  533. _alloc_test_st():
  534. count(0),
  535. total(0)
  536. { }
  537. void add()
  538. {
  539. count++;
  540. total++;
  541. }
  542. void subtract()
  543. {
  544. count--;
  545. }
  546. bool success() // count is valid as 1 only with the current test
  547. {
  548. if (total and count == 1)
  549. return true;
  550. std::cerr << __func__ << ":" << __LINE__ << " Total:" << total << " Count:" << count << std::endl;
  551. return false;
  552. }
  553. };
  554. static void* test_malloc_count_fn(size_t size, void *context)
  555. {
  556. _alloc_test_st *_foo= (_alloc_test_st *)context;
  557. _foo->add();
  558. return malloc(size);
  559. }
  560. static void test_free_count_fn(void *ptr, void *context)
  561. {
  562. _alloc_test_st *_foo= (_alloc_test_st *)context;
  563. _foo->subtract();
  564. return free(ptr);
  565. }
  566. static test_return_t gearman_client_set_workload_allocators_test(void *object)
  567. {
  568. gearman_client_st *client= (gearman_client_st *)object;
  569. test_truth(client);
  570. _alloc_test_st _foo;
  571. gearman_client_set_workload_malloc_fn(client, test_malloc_count_fn, &_foo);
  572. gearman_client_set_workload_free_fn(client, test_free_count_fn, &_foo);
  573. test_compare(TEST_SUCCESS, submit_job_test(object));
  574. test_true(_foo.success());
  575. return TEST_SUCCESS;
  576. }
  577. static test_return_t gearman_client_job_status_with_return(void *object)
  578. {
  579. gearman_client_st *client= (gearman_client_st *)object;
  580. test_truth(client);
  581. gearman_string_t value= { test_literal_param("background_test") };
  582. const char *worker_function= (const char *)gearman_client_context(client);
  583. test_truth(worker_function);
  584. gearman_job_handle_t job_handle;
  585. test_compare_got(GEARMAN_SUCCESS,
  586. gearman_client_do_background(client, worker_function, NULL, gearman_string_param(value), job_handle),
  587. gearman_client_error(client));
  588. gearman_return_t ret;
  589. do
  590. {
  591. uint32_t numerator;
  592. uint32_t denominator;
  593. ret= gearman_client_job_status(client, job_handle, NULL, NULL, &numerator, &denominator);
  594. } while (gearman_continue(ret));
  595. test_compare(GEARMAN_SUCCESS, ret);
  596. return TEST_SUCCESS;
  597. }
  598. static test_return_t background_failure_test(void *object)
  599. {
  600. gearman_client_st *client= (gearman_client_st *)object;
  601. gearman_job_handle_t job_handle;
  602. bool is_known;
  603. bool is_running;
  604. uint32_t numerator;
  605. uint32_t denominator;
  606. gearman_return_t rc= gearman_client_do_background(client, "does_not_exist", NULL,
  607. test_literal_param("background_failure_test"),
  608. job_handle);
  609. test_compare_got(GEARMAN_SUCCESS, rc, gearman_client_error(client));
  610. do {
  611. rc= gearman_client_job_status(client, job_handle, &is_known, &is_running,
  612. &numerator, &denominator);
  613. test_true(is_known == true and is_running == false and numerator == 0 and denominator == 0);
  614. } while (gearman_continue(rc)); // We do not test for is_known since the server will keep the job around until a worker comes along
  615. test_compare(GEARMAN_SUCCESS, rc);
  616. return TEST_SUCCESS;
  617. }
  618. static test_return_t add_servers_test(void *)
  619. {
  620. gearman_client_st client, *client_ptr;
  621. client_ptr= gearman_client_create(&client);
  622. test_truth(client_ptr);
  623. gearman_return_t rc;
  624. rc= gearman_client_add_servers(&client, "127.0.0.1:4730,localhost");
  625. test_compare_got(GEARMAN_SUCCESS, rc, gearman_strerror(rc));
  626. rc= gearman_client_add_servers(&client, "old_jobserver:7003,broken:12345");
  627. test_compare_got(GEARMAN_SUCCESS, rc, gearman_strerror(rc));
  628. gearman_client_free(&client);
  629. return TEST_SUCCESS;
  630. }
  631. static test_return_t hostname_resolution(void *)
  632. {
  633. gearman_client_st *client= gearman_client_create(NULL);
  634. test_truth(client);
  635. test_compare(GEARMAN_SUCCESS,
  636. gearman_client_add_servers(client, "exist.gearman.info"));
  637. test_compare(GEARMAN_GETADDRINFO,
  638. gearman_client_echo(client, test_literal_param("foo")));
  639. gearman_client_free(client);
  640. return TEST_SUCCESS;
  641. }
  642. static test_return_t bug_518512_test(void *)
  643. {
  644. gearman_client_st client;
  645. size_t result_size;
  646. test_truth(gearman_client_create(&client));
  647. test_compare(GEARMAN_SUCCESS,
  648. gearman_client_add_server(&client, NULL, CLIENT_TEST_PORT));
  649. gearman_client_set_timeout(&client, 0);
  650. gearman_return_t rc;
  651. void *result= gearman_client_do(&client, "client_test_temp", NULL, NULL, 0,
  652. &result_size, &rc);
  653. test_compare_got(GEARMAN_TIMEOUT, rc, gearman_strerror(rc));
  654. test_false(result);
  655. test_zero(result_size);
  656. gearman_function_t func_arg= gearman_function_create_v1(client_test_temp_worker);
  657. struct worker_handle_st *completion_worker= test_worker_start(CLIENT_TEST_PORT, NULL, "client_test_temp",
  658. func_arg, NULL, gearman_worker_options_t());
  659. gearman_client_set_timeout(&client, -1);
  660. result= gearman_client_do(&client, "client_test_temp", NULL, NULL, 0,
  661. &result_size, &rc);
  662. test_true_got(rc != GEARMAN_TIMEOUT, gearman_strerror(rc));
  663. (void)result;
  664. delete completion_worker;
  665. gearman_client_free(&client);
  666. return TEST_SUCCESS;
  667. }
  668. #define NUMBER_OF_WORKERS 2
  669. static test_return_t loop_test(void *)
  670. {
  671. pthread_t one;
  672. pthread_t two;
  673. struct worker_handle_st *handles[NUMBER_OF_WORKERS];
  674. gearman_function_t func_arg= gearman_function_create_v1(client_test_temp_worker);
  675. for (size_t x= 0; x < NUMBER_OF_WORKERS; x++)
  676. {
  677. handles[x]= test_worker_start(CLIENT_TEST_PORT, NULL, "client_test_temp",
  678. func_arg, NULL, gearman_worker_options_t());
  679. }
  680. gearman_return_t one_rc= GEARMAN_SUCCESS;
  681. pthread_create(&one, NULL, client_thread, &one_rc);
  682. gearman_return_t two_rc= GEARMAN_SUCCESS;
  683. pthread_create(&two, NULL, client_thread, &two_rc);
  684. void *unused;
  685. pthread_join(one, &unused);
  686. pthread_join(two, &unused);
  687. for (size_t x= 0; x < NUMBER_OF_WORKERS; x++)
  688. {
  689. delete handles[x];
  690. }
  691. test_compare(GEARMAN_SUCCESS, one_rc);
  692. test_compare(GEARMAN_SUCCESS, two_rc);
  693. return TEST_SUCCESS;
  694. }
  695. static test_return_t regression_785203_do_test(void *object)
  696. {
  697. gearman_client_st *original_client= (gearman_client_st *)object;
  698. test_true(original_client);
  699. const char *worker_function= (const char *)gearman_client_context(original_client);
  700. test_truth(original_client);
  701. gearman_client_st *client;
  702. test_truth(client= gearman_client_create(NULL));
  703. test_compare(GEARMAN_SUCCESS,
  704. gearman_client_add_server(client, NULL, CLIENT_TEST_PORT));
  705. gearman_client_add_options(client, GEARMAN_CLIENT_FREE_TASKS);
  706. { // All defaults, except timeout_return
  707. test_truth(client->options.allocated);
  708. test_false(client->options.non_blocking);
  709. test_false(client->options.unbuffered_result);
  710. test_false(client->options.no_new);
  711. test_truth(client->options.free_tasks);
  712. }
  713. gearman_return_t rc;
  714. size_t result_length;
  715. void *result= gearman_client_do(client, worker_function, NULL,
  716. test_literal_param("keep it rocking and sing"),
  717. &result_length, &rc);
  718. test_true(result);
  719. free(result);
  720. gearman_client_free(client);
  721. return TEST_SUCCESS;
  722. }
  723. static test_return_t regression_785203_do_background_test(void *object)
  724. {
  725. gearman_client_st *original_client= (gearman_client_st *)object;
  726. test_true(original_client);
  727. const char *worker_function= (const char *)gearman_client_context(original_client);
  728. test_truth(worker_function);
  729. gearman_client_st *client;
  730. test_truth(client= gearman_client_create(NULL));
  731. test_compare(GEARMAN_SUCCESS,
  732. gearman_client_add_server(client, NULL, CLIENT_TEST_PORT));
  733. gearman_client_add_options(client, GEARMAN_CLIENT_FREE_TASKS);
  734. { // All defaults, except timeout_return
  735. test_truth(client->options.allocated);
  736. test_false(client->options.non_blocking);
  737. test_false(client->options.unbuffered_result);
  738. test_false(client->options.no_new);
  739. test_truth(client->options.free_tasks);
  740. }
  741. gearman_job_handle_t job_handle;
  742. test_compare_got(GEARMAN_SUCCESS,
  743. gearman_client_do_background(client, worker_function,
  744. NULL, // No unique requested
  745. test_literal_param("keep it rocking and sing"),
  746. job_handle),
  747. gearman_client_error(client));
  748. gearman_return_t ret;
  749. do
  750. {
  751. uint32_t numerator;
  752. uint32_t denominator;
  753. ret= gearman_client_job_status(client, job_handle, NULL, NULL, &numerator, &denominator);
  754. } while (gearman_continue(ret));
  755. test_compare(GEARMAN_SUCCESS, ret);
  756. gearman_client_free(client);
  757. return TEST_SUCCESS;
  758. }
  759. static test_return_t submit_log_failure(void *object)
  760. {
  761. gearman_client_st *client= (gearman_client_st *)object;
  762. test_truth(client);
  763. gearman_string_t value= { test_literal_param("submit_log_failure") };
  764. const char *worker_function= (const char *)gearman_client_context(client);
  765. test_truth(worker_function);
  766. size_t result_length;
  767. gearman_return_t rc;
  768. void *job_result= gearman_client_do(client, worker_function, NULL,
  769. gearman_string_param(value),
  770. &result_length, &rc);
  771. test_compare(GEARMAN_NO_SERVERS, rc);
  772. test_false(job_result);
  773. test_zero(result_length);
  774. return TEST_SUCCESS;
  775. }
  776. static void log_counter(const char *line, gearman_verbose_t verbose,
  777. void *context)
  778. {
  779. uint32_t *counter= (uint32_t *)context;
  780. (void)verbose;
  781. (void)line;
  782. *counter= *counter + 1;
  783. }
  784. static test_return_t strerror_count(void *)
  785. {
  786. test_compare((int)GEARMAN_MAX_RETURN, 51);
  787. return TEST_SUCCESS;
  788. }
  789. #undef MAKE_NEW_STRERROR
  790. static char * make_number(uint32_t expected, uint32_t got)
  791. {
  792. char buffer[1024];
  793. snprintf(buffer, sizeof(buffer), "Expected %uU, got %uU", expected, got);
  794. return strdup(buffer);
  795. }
  796. static test_return_t strerror_strings(void *)
  797. {
  798. uint32_t values[]= {
  799. 2723107532U, 1294272985U, 949848612U, 646434617U,
  800. 2273096667U, 3411376012U, 978198404U, 2644287234U,
  801. 1762137345U, 1727436301U, 1103093142U, 2958899803U,
  802. 3844590487U, 3520316764U, 3288532333U, 697573278U,
  803. 2328987341U, 1321921098U, 1475770122U, 4011631587U,
  804. 2468981698U, 2935753385U, 884320816U, 3006705975U,
  805. 2840498210U, 2953034368U, 501858685U, 1635925784U,
  806. 880765771U, 15612712U, 1489284002U, 2968621609U,
  807. 79936336U, 3059874010U, 3562217099U, 13337402U,
  808. 132823274U, 3950859856U, 237150774U, 290535510U,
  809. 2101976744U, 2262698284U, 3182950564U, 2391595326U,
  810. 1764731897U, 3485422815U, 99607280U, 2348849961U,
  811. 607991020U, 1597605008U, 1377573125U };
  812. for (int rc= GEARMAN_SUCCESS; rc < GEARMAN_MAX_RETURN; rc++)
  813. {
  814. char *make_number_str;
  815. uint32_t hash_val;
  816. const char *msg= gearman_strerror((gearman_return_t)rc);
  817. hash_val= internal_generate_hash(msg, strlen(msg));
  818. test_compare_got(values[rc], hash_val, make_number_str= make_number(values[rc], hash_val));
  819. free(make_number_str);
  820. }
  821. return TEST_SUCCESS;
  822. }
  823. static uint32_t global_counter;
  824. static test_return_t pre_chunk(void *object)
  825. {
  826. client_test_st *all= (client_test_st *)object;
  827. all->set_worker_name(WORKER_CHUNKED_FUNCTION_NAME);
  828. return TEST_SUCCESS;
  829. }
  830. static test_return_t pre_v2(void *object)
  831. {
  832. client_test_st *all= (client_test_st *)object;
  833. all->set_worker_name(WORKER_FUNCTION_NAME"_v2");
  834. return TEST_SUCCESS;
  835. }
  836. static test_return_t pre_chunk_v2(void *object)
  837. {
  838. client_test_st *all= (client_test_st *)object;
  839. all->set_worker_name(WORKER_CHUNKED_FUNCTION_NAME"_v2");
  840. return TEST_SUCCESS;
  841. }
  842. static test_return_t pre_free_tasks(void *object)
  843. {
  844. client_test_st *all= (client_test_st *)object;
  845. gearman_client_add_options(all->client(), GEARMAN_CLIENT_FREE_TASKS);
  846. return TEST_SUCCESS;
  847. }
  848. static test_return_t post_free_tasks(void *object)
  849. {
  850. client_test_st *all= (client_test_st *)object;
  851. gearman_client_remove_options(all->client(), GEARMAN_CLIENT_FREE_TASKS);
  852. return TEST_SUCCESS;
  853. }
  854. static test_return_t pre_namespace(void *object)
  855. {
  856. client_test_st *all= (client_test_st *)object;
  857. gearman_client_set_namespace(all->client(), NAMESPACE_KEY, strlen(NAMESPACE_KEY));
  858. assert(not gearman_client_has_option(all->client(), GEARMAN_CLIENT_FREE_TASKS));
  859. return TEST_SUCCESS;
  860. }
  861. static test_return_t pre_unique(void *object)
  862. {
  863. client_test_st *all= (client_test_st *)object;
  864. all->set_worker_name(WORKER_UNIQUE_FUNCTION_NAME);
  865. return TEST_SUCCESS;
  866. }
  867. static test_return_t post_function_reset(void *object)
  868. {
  869. client_test_st *all= (client_test_st *)object;
  870. all->set_worker_name(WORKER_FUNCTION_NAME);
  871. gearman_client_set_namespace(all->client(), 0, 0);
  872. assert(not gearman_client_has_option(all->client(), GEARMAN_CLIENT_FREE_TASKS));
  873. return TEST_SUCCESS;
  874. }
  875. static test_return_t pre_logging(void *object)
  876. {
  877. client_test_st *all= (client_test_st *)object;
  878. gearman_log_fn *func= log_counter;
  879. global_counter= 0;
  880. all->reset_client();
  881. all->set_clone(false);
  882. gearman_client_set_log_fn(all->client(), func, &global_counter, GEARMAN_VERBOSE_MAX);
  883. return TEST_SUCCESS;
  884. }
  885. static test_return_t post_logging(void *)
  886. {
  887. test_truth(global_counter);
  888. return TEST_SUCCESS;
  889. }
  890. void *client_test_temp_worker(gearman_job_st *, void *,
  891. size_t *result_size, gearman_return_t *ret_ptr)
  892. {
  893. *result_size= 0;
  894. *ret_ptr= GEARMAN_SUCCESS;
  895. return NULL;
  896. }
  897. static void *world_create(server_startup_st& servers, test_return_t& error)
  898. {
  899. const char *argv[1]= { "client_gearmand" };
  900. if (not server_startup(servers, "gearmand", CLIENT_TEST_PORT, 1, argv))
  901. {
  902. error= TEST_FAILURE;
  903. return NULL;
  904. }
  905. (void)pre_namespace;
  906. client_test_st *test= new client_test_st();
  907. if (not test)
  908. {
  909. error= TEST_MEMORY_ALLOCATION_FAILURE;
  910. return NULL;
  911. }
  912. // Version 1 functions
  913. gearman_function_t echo_react_fn_v1= gearman_function_create_v1(echo_or_react_worker);
  914. test->push(test_worker_start(CLIENT_TEST_PORT, NULL, WORKER_FUNCTION_NAME, echo_react_fn_v1, NULL, gearman_worker_options_t()));
  915. gearman_function_t echo_react_chunk_fn_v1= gearman_function_create_v1(echo_or_react_chunk_worker);
  916. test->push(test_worker_start(CLIENT_TEST_PORT, NULL, WORKER_CHUNKED_FUNCTION_NAME, echo_react_chunk_fn_v1, NULL, gearman_worker_options_t()));
  917. // Version 2 functsions
  918. gearman_function_t echo_react_fn_v2= gearman_function_create(echo_or_react_worker_v2);
  919. test->push(test_worker_start(CLIENT_TEST_PORT, NULL, WORKER_FUNCTION_NAME"_v2", echo_react_fn_v2, NULL, gearman_worker_options_t()));
  920. gearman_function_t echo_react_chunk_fn_v2= gearman_function_create(echo_or_react_chunk_worker_v2);
  921. test->push(test_worker_start(CLIENT_TEST_PORT, NULL, WORKER_CHUNKED_FUNCTION_NAME"_v2", echo_react_chunk_fn_v2, NULL, gearman_worker_options_t()));
  922. gearman_function_t unique_worker_arg= gearman_function_create_v1(unique_worker);
  923. test->push(test_worker_start(CLIENT_TEST_PORT, NULL, WORKER_UNIQUE_FUNCTION_NAME, unique_worker_arg, NULL, GEARMAN_WORKER_GRAB_UNIQ));
  924. gearman_function_t split_worker_fn= gearman_function_create_partition(split_worker, cat_aggregator_fn);
  925. test->push(test_worker_start(CLIENT_TEST_PORT, NULL, WORKER_SPLIT_FUNCTION_NAME, split_worker_fn, NULL, GEARMAN_WORKER_GRAB_ALL));
  926. // Namespace versions of the above
  927. test->push(test_worker_start(CLIENT_TEST_PORT, NAMESPACE_KEY, WORKER_FUNCTION_NAME, echo_react_fn_v1, NULL, gearman_worker_options_t()));
  928. test->push(test_worker_start(CLIENT_TEST_PORT, NAMESPACE_KEY, WORKER_CHUNKED_FUNCTION_NAME, echo_react_chunk_fn_v1, NULL, gearman_worker_options_t()));
  929. test->push(test_worker_start(CLIENT_TEST_PORT, NAMESPACE_KEY, WORKER_SPLIT_FUNCTION_NAME, split_worker_fn, NULL, GEARMAN_WORKER_GRAB_ALL));
  930. gearman_function_t increment_reset_worker_fn= gearman_function_create_v1(increment_reset_worker);
  931. for (uint32_t x= 0; x < 10; x++)
  932. {
  933. test->push(test_worker_start(CLIENT_TEST_PORT,
  934. NULL,
  935. "increment_reset_worker", increment_reset_worker_fn,
  936. NULL, gearman_worker_options_t()));
  937. }
  938. // Count worker
  939. gearman_function_t count_worker_fn= gearman_function_create(count_worker);
  940. test->push(test_worker_start(CLIENT_TEST_PORT, NULL, "count", count_worker_fn, NULL, gearman_worker_options_t()));
  941. if (gearman_failed(gearman_client_add_server(test->client(), NULL, CLIENT_TEST_PORT)))
  942. {
  943. error= TEST_FAILURE;
  944. return NULL;
  945. }
  946. error= TEST_SUCCESS;
  947. return (void *)test;
  948. }
  949. static bool world_destroy(void *object)
  950. {
  951. client_test_st *test= (client_test_st *)object;
  952. delete test;
  953. return TEST_SUCCESS;
  954. }
  955. test_st tests[] ={
  956. {"bug_518512_test", 0, bug_518512_test },
  957. {"init", 0, init_test },
  958. {"allocation", 0, allocation_test },
  959. {"clone_test", 0, clone_test },
  960. {"echo", 0, echo_test },
  961. {"options", 0, option_test },
  962. {"submit_job", 0, submit_job_test },
  963. {"submit_null_job", 0, submit_null_job_test },
  964. {"submit_fail_job", 0, submit_fail_job_test },
  965. {"exception", 0, submit_exception_job_test },
  966. {"warning", 0, submit_warning_job_test },
  967. {"submit_multiple_do", 0, submit_multiple_do },
  968. {"gearman_client_job_status()", 0, gearman_client_job_status_test },
  969. {"gearman_client_job_status() with gearman_return_t", 0, gearman_client_job_status_with_return },
  970. {"background_failure", 0, background_failure_test },
  971. {"add_servers", 0, add_servers_test },
  972. {"gearman_client_add_servers(GEARMAN_GETADDRINFO)", 0, hostname_resolution },
  973. {"loop_test", 0, loop_test },
  974. {0, 0, 0}
  975. };
  976. test_st gearman_command_t_tests[] ={
  977. {"gearman_command_t", 0, check_gearman_command_t },
  978. {0, 0, 0}
  979. };
  980. test_st tests_log[] ={
  981. {"submit_log_failure", 0, submit_log_failure },
  982. {0, 0, 0}
  983. };
  984. test_st gearman_strerror_tests[] ={
  985. {"count", 0, strerror_count },
  986. {"strings", 0, strerror_strings },
  987. {0, 0, 0}
  988. };
  989. test_st unique_tests[] ={
  990. {"compare sent unique", 0, unique_compare_test },
  991. {0, 0, 0}
  992. };
  993. test_st gearman_client_set_workload_malloc_fn_tests[] ={
  994. {"gearman_client_set_workload_malloc_fn()", 0, gearman_client_set_workload_malloc_fn_test },
  995. {"gearman_client_set_workload_free_fn()", 0, gearman_client_set_workload_free_fn_test },
  996. {"submit job and check for usage of both malloc/free", 0, gearman_client_set_workload_allocators_test },
  997. {0, 0, 0}
  998. };
  999. test_st regression_tests[] ={
  1000. #if 0
  1001. {"lp:768317", 0, regression_768317_test },
  1002. #endif
  1003. {"lp:785203 gearman_client_do()", 0, regression_785203_do_test },
  1004. {"lp:785203 gearman_client_do_background()", 0, regression_785203_do_background_test },
  1005. {0, 0, 0}
  1006. };
  1007. test_st gearman_client_do_tests[] ={
  1008. {"gearman_client_do() fail huge unique", 0, gearman_client_do_huge_unique },
  1009. {"gearman_client_do() with active background task", 0, gearman_client_do_with_active_background_task },
  1010. {0, 0, 0}
  1011. };
  1012. test_st gearman_execute_tests[] ={
  1013. {"gearman_execute()", 0, gearman_execute_test },
  1014. {"gearman_execute(GEARMAN_WORK_FAIL)", 0, gearman_execute_fail_test },
  1015. {"gearman_execute() epoch", 0, gearman_execute_epoch_test },
  1016. {"gearman_execute() epoch and test gearman_job_handle_t", 0, gearman_execute_epoch_check_job_handle_test },
  1017. {"gearman_execute(GEARMAN_TIMEOUT)", 0, gearman_execute_timeout_test },
  1018. {"gearman_execute() background", 0, gearman_execute_bg_test },
  1019. {"gearman_execute() multiple background", 0, gearman_execute_multile_bg_test },
  1020. {0, 0, 0}
  1021. };
  1022. test_st gearman_client_do_background_tests[] ={
  1023. {"gearman_client_do_background()", 0, gearman_client_do_background_basic },
  1024. {"gearman_client_do_high_background()", 0, gearman_client_do_high_background_basic },
  1025. {"gearman_client_do_low_background()", 0, gearman_client_do_low_background_basic },
  1026. {0, 0, 0}
  1027. };
  1028. test_st gearman_client_do_job_handle_tests[] ={
  1029. {"gearman_client_do_job_handle() no active tasks", 0, gearman_client_do_job_handle_no_active_task },
  1030. {"gearman_client_do_job_handle() follow do command", 0, gearman_client_do_job_handle_follow_do },
  1031. {0, 0, 0}
  1032. };
  1033. test_st gearman_execute_partition_tests[] ={
  1034. {"gearman_execute_by_partition() map reduce", 0, gearman_execute_partition_basic },
  1035. {"gearman_execute_by_partition(GEARMAN_ARGUMENT_TOO_LARGE) map reduce", 0, gearman_execute_partition_check_parameters },
  1036. {"gearman_execute_by_partition(GEARMAN_WORK_FAIL) map reduce", 0, gearman_execute_partition_workfail },
  1037. {"gearman_execute_by_partition() fail in reduction", 0, gearman_execute_partition_fail_in_reduction },
  1038. #if 0
  1039. {"gearman_execute() with V2 Worker that has aggregate defined", 0, gearman_execute_partition_use_as_function },
  1040. {"gearman_execute_by_partition() no aggregate function", 0, gearman_execute_partition_no_aggregate },
  1041. #endif
  1042. {0, 0, 0}
  1043. };
  1044. test_st gearman_client_set_server_option_tests[] ={
  1045. {"gearman_client_set_server_option(exceptions)", 0, gearman_client_set_server_option_exception},
  1046. {"gearman_client_set_server_option(bad)", 0, gearman_client_set_server_option_bad},
  1047. {0, 0, 0}
  1048. };
  1049. test_st gearman_task_tests[] ={
  1050. {"gearman_client_add_task() ", 0, gearman_client_add_task_test},
  1051. {"gearman_client_add_task() fail", 0, gearman_client_add_task_test_fail},
  1052. {"gearman_client_add_task() bad workload", 0, gearman_client_add_task_test_bad_workload},
  1053. {"gearman_client_add_task_background()", 0, gearman_client_add_task_background_test},
  1054. {"gearman_client_add_task_low_background()", 0, gearman_client_add_task_low_background_test},
  1055. {"gearman_client_add_task_high_background()", 0, gearman_client_add_task_high_background_test},
  1056. {"gearman_client_add_task() exception", 0, gearman_client_add_task_exception},
  1057. {"gearman_client_add_task() warning", 0, gearman_client_add_task_warning},
  1058. {"gearman_client_add_task(GEARMAN_NO_SERVERS)", 0, gearman_client_add_task_no_servers},
  1059. {"gearman_client_set_task_context_free_fn()", 0, gearman_client_set_task_context_free_fn_test},
  1060. {0, 0, 0}
  1061. };
  1062. test_st gearman_task_pause_tests[] ={
  1063. {"gearman_client_add_task(GEARMAN_PAUSE)", 0, gearman_client_add_task_pause_test},
  1064. {0, 0, 0}
  1065. };
  1066. collection_st collection[] ={
  1067. {"gearman_client_st", 0, 0, tests},
  1068. {"gearman_client_st chunky", pre_chunk, post_function_reset, tests}, // Test with a worker that will respond in part
  1069. {"gearman_strerror()", 0, 0, gearman_strerror_tests},
  1070. {"gearman_task_add_task()", 0, 0, gearman_task_tests},
  1071. {"gearman_task_add_task() v2 workers", pre_v2, post_function_reset, gearman_task_tests},
  1072. {"gearman_task_add_task() chunky", pre_chunk, post_function_reset, gearman_task_tests},
  1073. {"gearman_task_add_task() chunky v2 workers", pre_chunk_v2, post_function_reset, gearman_task_tests},
  1074. {"gearman_task_add_task() namespace", pre_namespace, post_function_reset, gearman_task_tests},
  1075. {"gearman_task_add_task(GEARMAN_CLIENT_FREE_TASKS)", pre_free_tasks, post_free_tasks, gearman_task_tests},
  1076. {"gearman_task_add_task(GEARMAN_PAUSE)", pre_chunk, post_function_reset, gearman_task_pause_tests},
  1077. {"gearman_task_add_task(GEARMAN_PAUSE)", pre_chunk_v2, post_function_reset, gearman_task_pause_tests},
  1078. {"unique", pre_unique, post_function_reset, unique_tests},
  1079. {"gearman_client_set_workload_malloc_fn()", 0, 0, gearman_client_set_workload_malloc_fn_tests},
  1080. {"gearman_client_do()", 0, 0, gearman_client_do_tests},
  1081. {"gearman_client_do() namespace", pre_namespace, post_function_reset, gearman_client_do_tests},
  1082. {"gearman_client_do(GEARMAN_CLIENT_FREE_TASKS)", pre_free_tasks, post_free_tasks, gearman_client_do_tests},
  1083. {"gearman_client_do_job_handle()", 0, 0, gearman_client_do_job_handle_tests},
  1084. {"gearman_client_do_job_handle() namespace", pre_namespace, post_function_reset, gearman_client_do_job_handle_tests},
  1085. {"gearman_client_do_job_handle(GEARMAN_CLIENT_FREE_TASKS)", pre_free_tasks, post_free_tasks, gearman_client_do_job_handle_tests},
  1086. {"gearman_client_do_background()", 0, 0, gearman_client_do_background_tests},
  1087. {"gearman_client_do_background(GEARMAN_CLIENT_FREE_TASKS)", pre_free_tasks, post_free_tasks, gearman_client_do_background_tests},
  1088. {"gearman_client_set_server_option", 0, 0, gearman_client_set_server_option_tests},
  1089. {"gearman_execute()", 0, 0, gearman_execute_tests},
  1090. {"gearman_execute(GEARMAN_CLIENT_FREE_TASKS)", pre_free_tasks, post_free_tasks, gearman_execute_tests},
  1091. {"gearman_execute() chunked return", pre_chunk, post_function_reset, gearman_execute_tests},
  1092. {"gearman_execute() chunked return", pre_chunk_v2, post_function_reset, gearman_execute_tests},
  1093. {"gearman_execute_partition()", 0, 0, gearman_execute_partition_tests},
  1094. {"gearman_execute_partition(GEARMAN_CLIENT_FREE_TASKS)", pre_free_tasks, post_free_tasks, gearman_execute_partition_tests},
  1095. {"gearman_command_t", 0, 0, gearman_command_t_tests},
  1096. {"regression_tests", 0, 0, regression_tests},
  1097. {"client-logging", pre_logging, post_logging, tests_log},
  1098. {0, 0, 0, 0}
  1099. };
  1100. typedef test_return_t (*libgearman_test_prepost_callback_fn)(client_test_st *);
  1101. typedef test_return_t (*libgearman_test_callback_fn)(gearman_client_st *);
  1102. static test_return_t _runner_prepost_default(libgearman_test_prepost_callback_fn func, client_test_st *container)
  1103. {
  1104. if (func)
  1105. {
  1106. return func(container);
  1107. }
  1108. return TEST_SUCCESS;
  1109. }
  1110. static test_return_t _runner_default(libgearman_test_callback_fn func, client_test_st *container)
  1111. {
  1112. if (func)
  1113. {
  1114. test_return_t rc;
  1115. if (container->clone())
  1116. {
  1117. gearman_client_st *client= gearman_client_clone(NULL, container->client());
  1118. test_truth(client);
  1119. gearman_client_set_context(client, (void *)container->worker_name());
  1120. rc= func(client);
  1121. if (rc == TEST_SUCCESS)
  1122. {
  1123. test_true_got(not client->task_list, "Client still had tasks");
  1124. }
  1125. gearman_client_free(client);
  1126. }
  1127. else
  1128. {
  1129. gearman_client_set_context(container->client(), (void *)container->worker_name());
  1130. rc= func(container->client());
  1131. assert(not container->client()->task_list);
  1132. }
  1133. return rc;
  1134. }
  1135. return TEST_SUCCESS;
  1136. }
  1137. class GearmandRunner : public Runner {
  1138. public:
  1139. test_return_t run(test_callback_fn* func, void *object)
  1140. {
  1141. return _runner_default(libgearman_test_callback_fn(func), (client_test_st*)object);
  1142. }
  1143. test_return_t pre(test_callback_fn* func, void *object)
  1144. {
  1145. return _runner_prepost_default(libgearman_test_prepost_callback_fn(func), (client_test_st*)object);
  1146. }
  1147. test_return_t post(test_callback_fn* func, void *object)
  1148. {
  1149. return _runner_prepost_default(libgearman_test_prepost_callback_fn(func), (client_test_st*)object);
  1150. }
  1151. };
  1152. static GearmandRunner defualt_runner;
  1153. void get_world(Framework *world)
  1154. {
  1155. world->collections= collection;
  1156. world->_create= world_create;
  1157. world->_destroy= world_destroy;
  1158. world->set_runner(&defualt_runner);
  1159. }