client_test.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /* Gearman server and library
  2. * Copyright (C) 2008 Brian Aker, Eric Day
  3. * All rights reserved.
  4. *
  5. * Use and distribution licensed under the BSD license. See
  6. * the COPYING file in the parent directory for full text.
  7. */
  8. #include "config.h"
  9. #if defined(NDEBUG)
  10. # undef NDEBUG
  11. #endif
  12. #include <assert.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <libgearman/gearman.h>
  17. #include <libtest/server.h>
  18. #include <libtest/test.h>
  19. #include <libtest/worker.h>
  20. #define CLIENT_TEST_PORT 32123
  21. typedef struct
  22. {
  23. gearman_client_st client;
  24. pid_t gearmand_pid;
  25. struct worker_handle_st *handle;
  26. } client_test_st;
  27. #pragma GCC diagnostic ignored "-Wold-style-cast"
  28. /**
  29. @note Just here until I fix libhashkit.
  30. */
  31. static uint32_t internal_generate_hash(const char *key, size_t key_length)
  32. {
  33. const char *ptr= key;
  34. uint32_t value= 0;
  35. while (key_length--)
  36. {
  37. uint32_t val= (uint32_t) *ptr++;
  38. value += val;
  39. value += (value << 10);
  40. value ^= (value >> 6);
  41. }
  42. value += (value << 3);
  43. value ^= (value >> 11);
  44. value += (value << 15);
  45. return value == 0 ? 1 : (uint32_t) value;
  46. }
  47. /* Prototypes */
  48. test_return_t init_test(void *object);
  49. test_return_t allocation_test(void *object);
  50. test_return_t clone_test(void *object);
  51. test_return_t echo_test(void *object);
  52. test_return_t submit_job_test(void *object);
  53. test_return_t submit_null_job_test(void *object);
  54. test_return_t submit_fail_job_test(void *object);
  55. test_return_t background_test(void *object);
  56. test_return_t background_failure_test(void *object);
  57. test_return_t add_servers_test(void *object);
  58. void *client_test_worker(gearman_job_st *job, void *context,
  59. size_t *result_size, gearman_return_t *ret_ptr);
  60. void *client_test_temp_worker(gearman_job_st *job, void *context,
  61. size_t *result_size, gearman_return_t *ret_ptr);
  62. void *world_create(test_return_t *error);
  63. test_return_t world_destroy(void *object);
  64. static void *client_thread(void *object)
  65. {
  66. (void)object;
  67. gearman_return_t rc;
  68. gearman_client_st client;
  69. gearman_client_st *client_ptr;
  70. size_t result_size;
  71. client_ptr= gearman_client_create(&client);
  72. if (client_ptr == NULL)
  73. abort(); // This would be pretty bad.
  74. rc= gearman_client_add_server(&client, NULL, CLIENT_TEST_PORT);
  75. if (rc != GEARMAN_SUCCESS)
  76. {
  77. pthread_exit(0);
  78. }
  79. gearman_client_set_timeout(&client, 400);
  80. for (size_t x= 0; x < 5; x++)
  81. {
  82. (void) gearman_client_do(&client, "client_test_temp", NULL, NULL, 0,
  83. &result_size, &rc);
  84. }
  85. gearman_client_free(client_ptr);
  86. pthread_exit(0);
  87. }
  88. test_return_t init_test(void *object __attribute__((unused)))
  89. {
  90. gearman_client_st client;
  91. if (gearman_client_create(&client) == NULL)
  92. return TEST_FAILURE;
  93. gearman_client_free(&client);
  94. return TEST_SUCCESS;
  95. }
  96. test_return_t allocation_test(void *object __attribute__((unused)))
  97. {
  98. gearman_client_st *client;
  99. client= gearman_client_create(NULL);
  100. if (client == NULL)
  101. return TEST_FAILURE;
  102. gearman_client_free(client);
  103. return TEST_SUCCESS;
  104. }
  105. test_return_t clone_test(void *object)
  106. {
  107. const gearman_client_st *from= (gearman_client_st *)object;
  108. gearman_client_st *from_with_host;
  109. gearman_client_st *client;
  110. client= gearman_client_clone(NULL, NULL);
  111. test_truth(client);
  112. test_truth(client->options.allocated);
  113. gearman_client_free(client);
  114. client= gearman_client_clone(NULL, from);
  115. test_truth(client);
  116. gearman_client_free(client);
  117. from_with_host= gearman_client_create(NULL);
  118. test_truth(from_with_host);
  119. gearman_client_add_server(from_with_host, "127.0.0.1", 12345);
  120. client= gearman_client_clone(NULL, from_with_host);
  121. test_truth(client);
  122. test_truth(client->universal.con_list);
  123. test_truth(!strcmp(client->universal.con_list->host, from_with_host->universal.con_list->host));
  124. test_truth(client->universal.con_list->port == from_with_host->universal.con_list->port);
  125. gearman_client_free(client);
  126. gearman_client_free(from_with_host);
  127. return TEST_SUCCESS;
  128. }
  129. static test_return_t option_test(void *object __attribute__((unused)))
  130. {
  131. gearman_client_st *gear;
  132. gearman_client_options_t default_options;
  133. gear= gearman_client_create(NULL);
  134. test_truth(gear);
  135. { // Initial Allocated, no changes
  136. test_truth(gear->options.allocated);
  137. test_false(gear->options.non_blocking);
  138. test_false(gear->options.task_in_use);
  139. test_false(gear->options.unbuffered_result);
  140. test_false(gear->options.no_new);
  141. test_false(gear->options.free_tasks);
  142. }
  143. /* Set up for default options */
  144. default_options= gearman_client_options(gear);
  145. /*
  146. We take the basic options, and push
  147. them back in. See if we change anything.
  148. */
  149. gearman_client_set_options(gear, default_options);
  150. { // Initial Allocated, no changes
  151. test_truth(gear->options.allocated);
  152. test_false(gear->options.non_blocking);
  153. test_false(gear->options.task_in_use);
  154. test_false(gear->options.unbuffered_result);
  155. test_false(gear->options.no_new);
  156. test_false(gear->options.free_tasks);
  157. }
  158. /*
  159. We will trying to modify non-mutable options (which should not be allowed)
  160. */
  161. {
  162. gearman_client_remove_options(gear, GEARMAN_CLIENT_ALLOCATED);
  163. { // Initial Allocated, no changes
  164. test_truth(gear->options.allocated);
  165. test_false(gear->options.non_blocking);
  166. test_false(gear->options.task_in_use);
  167. test_false(gear->options.unbuffered_result);
  168. test_false(gear->options.no_new);
  169. test_false(gear->options.free_tasks);
  170. }
  171. gearman_client_remove_options(gear, GEARMAN_CLIENT_NO_NEW);
  172. { // Initial Allocated, no changes
  173. test_truth(gear->options.allocated);
  174. test_false(gear->options.non_blocking);
  175. test_false(gear->options.task_in_use);
  176. test_false(gear->options.unbuffered_result);
  177. test_false(gear->options.no_new);
  178. test_false(gear->options.free_tasks);
  179. }
  180. }
  181. /*
  182. We will test modifying GEARMAN_CLIENT_NON_BLOCKING in several manners.
  183. */
  184. {
  185. gearman_client_remove_options(gear, GEARMAN_CLIENT_NON_BLOCKING);
  186. { // GEARMAN_CLIENT_NON_BLOCKING set to default, by default.
  187. test_truth(gear->options.allocated);
  188. test_false(gear->options.non_blocking);
  189. test_false(gear->options.task_in_use);
  190. test_false(gear->options.unbuffered_result);
  191. test_false(gear->options.no_new);
  192. test_false(gear->options.free_tasks);
  193. }
  194. gearman_client_add_options(gear, GEARMAN_CLIENT_NON_BLOCKING);
  195. { // GEARMAN_CLIENT_NON_BLOCKING set to default, by default.
  196. test_truth(gear->options.allocated);
  197. test_truth(gear->options.non_blocking);
  198. test_false(gear->options.task_in_use);
  199. test_false(gear->options.unbuffered_result);
  200. test_false(gear->options.no_new);
  201. test_false(gear->options.free_tasks);
  202. }
  203. gearman_client_set_options(gear, GEARMAN_CLIENT_NON_BLOCKING);
  204. { // GEARMAN_CLIENT_NON_BLOCKING set to default, by default.
  205. test_truth(gear->options.allocated);
  206. test_truth(gear->options.non_blocking);
  207. test_false(gear->options.task_in_use);
  208. test_false(gear->options.unbuffered_result);
  209. test_false(gear->options.no_new);
  210. test_false(gear->options.free_tasks);
  211. }
  212. gearman_client_set_options(gear, GEARMAN_CLIENT_UNBUFFERED_RESULT);
  213. { // Everything is now set to false except GEARMAN_CLIENT_UNBUFFERED_RESULT, and non-mutable options
  214. test_truth(gear->options.allocated);
  215. test_false(gear->options.non_blocking);
  216. test_false(gear->options.task_in_use);
  217. test_truth(gear->options.unbuffered_result);
  218. test_false(gear->options.no_new);
  219. test_false(gear->options.free_tasks);
  220. }
  221. /*
  222. Reset options to default. Then add an option, and then add more options. Make sure
  223. the options are all additive.
  224. */
  225. {
  226. gearman_client_set_options(gear, default_options);
  227. { // See if we return to defaults
  228. test_truth(gear->options.allocated);
  229. test_false(gear->options.non_blocking);
  230. test_false(gear->options.task_in_use);
  231. test_false(gear->options.unbuffered_result);
  232. test_false(gear->options.no_new);
  233. test_false(gear->options.free_tasks);
  234. }
  235. gearman_client_add_options(gear, GEARMAN_CLIENT_FREE_TASKS);
  236. { // All defaults, except timeout_return
  237. test_truth(gear->options.allocated);
  238. test_false(gear->options.non_blocking);
  239. test_false(gear->options.task_in_use);
  240. test_false(gear->options.unbuffered_result);
  241. test_false(gear->options.no_new);
  242. test_truth(gear->options.free_tasks);
  243. }
  244. gearman_client_add_options(gear, (gearman_client_options_t)(GEARMAN_CLIENT_NON_BLOCKING|GEARMAN_CLIENT_UNBUFFERED_RESULT));
  245. { // GEARMAN_CLIENT_NON_BLOCKING set to default, by default.
  246. test_truth(gear->options.allocated);
  247. test_truth(gear->options.non_blocking);
  248. test_false(gear->options.task_in_use);
  249. test_truth(gear->options.unbuffered_result);
  250. test_false(gear->options.no_new);
  251. test_truth(gear->options.free_tasks);
  252. }
  253. }
  254. /*
  255. Add an option, and then replace with that option plus a new option.
  256. */
  257. {
  258. gearman_client_set_options(gear, default_options);
  259. { // See if we return to defaults
  260. test_truth(gear->options.allocated);
  261. test_false(gear->options.non_blocking);
  262. test_false(gear->options.task_in_use);
  263. test_false(gear->options.unbuffered_result);
  264. test_false(gear->options.no_new);
  265. test_false(gear->options.free_tasks);
  266. }
  267. gearman_client_add_options(gear, GEARMAN_CLIENT_FREE_TASKS);
  268. { // All defaults, except timeout_return
  269. test_truth(gear->options.allocated);
  270. test_false(gear->options.non_blocking);
  271. test_false(gear->options.task_in_use);
  272. test_false(gear->options.unbuffered_result);
  273. test_false(gear->options.no_new);
  274. test_truth(gear->options.free_tasks);
  275. }
  276. gearman_client_add_options(gear, (gearman_client_options_t)(GEARMAN_CLIENT_FREE_TASKS|GEARMAN_CLIENT_UNBUFFERED_RESULT));
  277. { // GEARMAN_CLIENT_NON_BLOCKING set to default, by default.
  278. test_truth(gear->options.allocated);
  279. test_false(gear->options.non_blocking);
  280. test_false(gear->options.task_in_use);
  281. test_truth(gear->options.unbuffered_result);
  282. test_false(gear->options.no_new);
  283. test_truth(gear->options.free_tasks);
  284. }
  285. }
  286. }
  287. gearman_client_free(gear);
  288. return TEST_SUCCESS;
  289. }
  290. test_return_t echo_test(void *object __attribute__((unused)))
  291. {
  292. gearman_client_st *client= (gearman_client_st *)object;
  293. gearman_return_t rc;
  294. size_t value_length;
  295. const char *value= "This is my echo test";
  296. value_length= strlen(value);
  297. rc= gearman_client_echo(client, (uint8_t *)value, value_length);
  298. if (rc != GEARMAN_SUCCESS)
  299. {
  300. printf("echo_test:%s\n", gearman_client_error(client));
  301. return TEST_FAILURE;
  302. }
  303. return TEST_SUCCESS;
  304. }
  305. test_return_t submit_job_test(void *object)
  306. {
  307. gearman_return_t rc;
  308. gearman_client_st *client= (gearman_client_st *)object;
  309. void *job_result;
  310. size_t job_length;
  311. uint8_t *value= (uint8_t *)"submit_job_test";
  312. size_t value_length= strlen("submit_job_test");
  313. job_result= gearman_client_do(client, "client_test", NULL, value,
  314. value_length, &job_length, &rc);
  315. if (rc != GEARMAN_SUCCESS)
  316. {
  317. printf("submit_job_test:%s\n", gearman_client_error(client));
  318. return TEST_FAILURE;
  319. }
  320. if (job_result == NULL)
  321. return TEST_FAILURE;
  322. if (value_length != job_length || memcmp(value, job_result, value_length))
  323. return TEST_FAILURE;
  324. free(job_result);
  325. return TEST_SUCCESS;
  326. }
  327. test_return_t submit_null_job_test(void *object)
  328. {
  329. gearman_return_t rc;
  330. gearman_client_st *client= (gearman_client_st *)object;
  331. void *job_result;
  332. size_t job_length;
  333. job_result= gearman_client_do(client, "client_test", NULL, NULL, 0,
  334. &job_length, &rc);
  335. if (rc != GEARMAN_SUCCESS)
  336. {
  337. printf("submit_null_job_test:%s\n", gearman_client_error(client));
  338. return TEST_FAILURE;
  339. }
  340. if (job_result != NULL || job_length != 0)
  341. return TEST_FAILURE;
  342. return TEST_SUCCESS;
  343. }
  344. test_return_t submit_fail_job_test(void *object)
  345. {
  346. gearman_return_t rc;
  347. gearman_client_st *client= (gearman_client_st *)object;
  348. void *job_result;
  349. size_t job_length;
  350. job_result= gearman_client_do(client, "client_test", NULL, "fail", 4,
  351. &job_length, &rc);
  352. if (rc != GEARMAN_WORK_FAIL)
  353. {
  354. printf("submit_fail_job_test:%s\n", gearman_client_error(client));
  355. return TEST_FAILURE;
  356. }
  357. return TEST_SUCCESS;
  358. }
  359. test_return_t background_test(void *object)
  360. {
  361. gearman_return_t rc;
  362. gearman_client_st *client= (gearman_client_st *)object;
  363. char job_handle[GEARMAN_JOB_HANDLE_SIZE];
  364. uint8_t *value= (uint8_t *)"background_test";
  365. size_t value_length= strlen("background_test");
  366. rc= gearman_client_do_background(client, "client_test", NULL, value,
  367. value_length, job_handle);
  368. if (rc != GEARMAN_SUCCESS)
  369. {
  370. printf("background_test:%s\n", gearman_client_error(client));
  371. return TEST_FAILURE;
  372. }
  373. while (1)
  374. {
  375. bool is_known;
  376. bool is_running;
  377. uint32_t numerator;
  378. uint32_t denominator;
  379. rc= gearman_client_job_status(client, job_handle, &is_known, &is_running,
  380. &numerator, &denominator);
  381. if (rc != GEARMAN_SUCCESS)
  382. {
  383. printf("background_test:%s\n", gearman_client_error(client));
  384. return TEST_FAILURE;
  385. }
  386. if (is_known == false)
  387. break;
  388. }
  389. return TEST_SUCCESS;
  390. }
  391. test_return_t background_failure_test(void *object)
  392. {
  393. gearman_return_t rc;
  394. gearman_client_st *client= (gearman_client_st *)object;
  395. char job_handle[GEARMAN_JOB_HANDLE_SIZE];
  396. bool is_known;
  397. bool is_running;
  398. uint32_t numerator;
  399. uint32_t denominator;
  400. uint8_t *value= (uint8_t *)"background_failure_test";
  401. size_t value_length= strlen("background_failure_test");
  402. rc= gearman_client_do_background(client, "does_not_exist", NULL, value,
  403. value_length, job_handle);
  404. if (rc != GEARMAN_SUCCESS)
  405. return TEST_FAILURE;
  406. rc= gearman_client_job_status(client, job_handle, &is_known, &is_running,
  407. &numerator, &denominator);
  408. if (rc != GEARMAN_SUCCESS || is_known != true || is_running != false ||
  409. numerator != 0 || denominator != 0)
  410. {
  411. printf("background_failure_test:%s\n", gearman_client_error(client));
  412. return TEST_FAILURE;
  413. }
  414. return TEST_SUCCESS;
  415. }
  416. test_return_t add_servers_test(void *object __attribute__((unused)))
  417. {
  418. gearman_client_st client;
  419. if (gearman_client_create(&client) == NULL)
  420. return TEST_FAILURE;
  421. if (gearman_client_add_servers(&client, "127.0.0.1:4730,localhost")
  422. != GEARMAN_SUCCESS)
  423. {
  424. return TEST_FAILURE;
  425. }
  426. if (gearman_client_add_servers(&client, "old_jobserver:7003,broken:12345")
  427. != GEARMAN_SUCCESS)
  428. {
  429. return TEST_FAILURE;
  430. }
  431. gearman_client_free(&client);
  432. return TEST_SUCCESS;
  433. }
  434. static test_return_t bug_518512_test(void *object)
  435. {
  436. gearman_return_t rc;
  437. gearman_client_st client;
  438. size_t result_size;
  439. (void) object;
  440. test_truth(gearman_client_create(&client));
  441. if (gearman_client_add_server(&client, NULL, CLIENT_TEST_PORT) != GEARMAN_SUCCESS)
  442. {
  443. fprintf(stderr, "bug_518512_test: gearman_client_add_server: %s\n", gearman_client_error(&client));
  444. return TEST_FAILURE;
  445. }
  446. gearman_client_set_timeout(&client, 100);
  447. (void) gearman_client_do(&client, "client_test_temp", NULL, NULL, 0,
  448. &result_size, &rc);
  449. if (rc != GEARMAN_TIMEOUT)
  450. {
  451. fprintf(stderr, "bug_518512_test: should have timed out\n");
  452. gearman_client_free(&client);
  453. return TEST_FAILURE;
  454. }
  455. struct worker_handle_st *handle= test_worker_start(CLIENT_TEST_PORT, "client_test_temp",
  456. client_test_temp_worker, NULL);
  457. gearman_client_set_timeout(&client, -1);
  458. (void) gearman_client_do(&client, "client_test_temp", NULL, NULL, 0,
  459. &result_size, &rc);
  460. if (rc != GEARMAN_SUCCESS)
  461. {
  462. fprintf(stderr, "bug_518512_test: gearman_client_do: %s\n", gearman_client_error(&client));
  463. test_worker_stop(handle);
  464. gearman_client_free(&client);
  465. return TEST_FAILURE;
  466. }
  467. test_worker_stop(handle);
  468. gearman_client_free(&client);
  469. return TEST_SUCCESS;
  470. }
  471. #define NUMBER_OF_WORKERS 2
  472. static test_return_t loop_test(void *object)
  473. {
  474. (void) object;
  475. void *unused;
  476. pthread_attr_t attr;
  477. pthread_t one;
  478. pthread_t two;
  479. pthread_attr_init(&attr);
  480. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  481. struct worker_handle_st *handles[NUMBER_OF_WORKERS];
  482. for (size_t x= 0; x < NUMBER_OF_WORKERS; x++)
  483. {
  484. handles[x]= test_worker_start(CLIENT_TEST_PORT, "client_test_temp",
  485. client_test_temp_worker, NULL);
  486. }
  487. pthread_create(&one, &attr, client_thread, NULL);
  488. pthread_create(&two, &attr, client_thread, NULL);
  489. pthread_join(one, &unused);
  490. pthread_join(two, &unused);
  491. for (size_t x= 0; x < NUMBER_OF_WORKERS; x++)
  492. {
  493. test_worker_stop(handles[x]);
  494. }
  495. pthread_attr_destroy(&attr);
  496. return TEST_SUCCESS;
  497. }
  498. static test_return_t submit_log_failure(void *object)
  499. {
  500. gearman_return_t rc;
  501. gearman_client_st *client= (gearman_client_st *)object;
  502. void *job_result;
  503. size_t job_length;
  504. uint8_t *value= (uint8_t *)"submit_log_failure";
  505. size_t value_length= strlen("submit_log_failure");
  506. job_result= gearman_client_do(client, "client_test", NULL, value,
  507. value_length, &job_length, &rc);
  508. if (rc != GEARMAN_SUCCESS)
  509. return TEST_SUCCESS;
  510. return TEST_FAILURE;
  511. }
  512. static void log_counter(const char *line, gearman_verbose_t verbose,
  513. void *context)
  514. {
  515. uint32_t *counter= (uint32_t *)context;
  516. (void)verbose;
  517. (void)line;
  518. *counter= *counter + 1;
  519. }
  520. static test_return_t strerror_count(void *object __attribute__((unused)))
  521. {
  522. test_truth(GEARMAN_MAX_RETURN == 49);
  523. return TEST_SUCCESS;
  524. }
  525. #undef MAKE_NEW_STRERROR
  526. static char * make_number(uint32_t expected, uint32_t got)
  527. {
  528. char buffer[1024];
  529. snprintf(buffer, sizeof(buffer), "Expected %uU, got %uU", expected, got);
  530. return strdup(buffer);
  531. }
  532. static test_return_t strerror_strings(void *object __attribute__((unused)))
  533. {
  534. uint32_t values[] = { 324335284U, 1940666259U, 4156775927U, 18028287U,
  535. 1834995715U, 1009419836U, 1038124396U, 3050095617U,
  536. 4004269877U, 2913489720U, 1389266665U, 1374361090U,
  537. 3775104989U, 1158738795U, 2490507301U, 426780991U,
  538. 2421852085U, 426121997U, 3669711613U, 2620567638U,
  539. 48094985U, 4052600452U, 2697110207U, 4260329382U,
  540. 3706494438U, 1765339649U, 1176029865U, 2899482444U,
  541. 2255507756U, 1844534215U, 1685626311U, 3134591697U,
  542. 1469920452U, 2236059486U, 1693700353U, 1173962212U,
  543. 2491943732U, 1864825729U, 523632457U, 1342225548U,
  544. 245155833U, 3999913926U, 2789053153U, 2576033598U,
  545. 463490826U, 1983660343U, 2268979717U, 1656388188U,
  546. 1558344702U};
  547. #ifdef MAKE_NEW_STRERROR
  548. int flip_flop= 0;
  549. printf("\n");
  550. #endif
  551. for (int rc= GEARMAN_SUCCESS; rc < GEARMAN_MAX_RETURN; rc++)
  552. {
  553. uint32_t hash_val;
  554. const char *msg= gearman_strerror((gearman_return_t)rc);
  555. hash_val= internal_generate_hash(msg, strlen(msg));
  556. #ifdef MAKE_NEW_STRERROR
  557. (void)values;
  558. printf("%uU,", hash_val);
  559. if (flip_flop == 3)
  560. {
  561. printf("\n");
  562. flip_flop= 0;
  563. }
  564. else
  565. {
  566. printf(" ");
  567. flip_flop++;
  568. }
  569. #else
  570. test_true_got(values[rc] == hash_val, make_number(values[rc], hash_val));
  571. #endif
  572. }
  573. #ifdef MAKE_NEW_STRERROR
  574. fflush(stdout);
  575. #endif
  576. return TEST_SUCCESS;
  577. }
  578. static uint32_t global_counter;
  579. static test_return_t pre_logging(void *object)
  580. {
  581. client_test_st *all= (client_test_st *)object;
  582. gearman_log_fn *func= log_counter;
  583. global_counter= 0;
  584. gearman_client_set_log_fn(&all->client, func, &global_counter, GEARMAN_VERBOSE_MAX);
  585. gearman_client_remove_servers(&all->client);
  586. return TEST_SUCCESS;
  587. }
  588. static test_return_t post_logging(void *object __attribute__((unused)))
  589. {
  590. test_truth(global_counter);
  591. return TEST_SUCCESS;
  592. }
  593. void *client_test_worker(gearman_job_st *job, void *context,
  594. size_t *result_size, gearman_return_t *ret_ptr)
  595. {
  596. const void *workload;
  597. void *result;
  598. (void)context;
  599. workload= gearman_job_workload(job);
  600. *result_size= gearman_job_workload_size(job);
  601. if (workload == NULL || *result_size == 0)
  602. {
  603. assert(workload == NULL && *result_size == 0);
  604. result= NULL;
  605. }
  606. else if (*result_size == 4 && !memcmp(workload, "fail", 4))
  607. {
  608. *ret_ptr= GEARMAN_WORK_FAIL;
  609. return NULL;
  610. }
  611. else
  612. {
  613. assert((result= malloc(*result_size)) != NULL);
  614. memcpy(result, workload, *result_size);
  615. }
  616. *ret_ptr= GEARMAN_SUCCESS;
  617. return result;
  618. }
  619. void *client_test_temp_worker(gearman_job_st *job, void *context,
  620. size_t *result_size, gearman_return_t *ret_ptr)
  621. {
  622. (void) job;
  623. (void) context;
  624. *result_size = 0;
  625. *ret_ptr= GEARMAN_SUCCESS;
  626. return NULL;
  627. }
  628. void *world_create(test_return_t *error)
  629. {
  630. client_test_st *test;
  631. pid_t gearmand_pid;
  632. /**
  633. * @TODO We cast this to char ** below, which is evil. We need to do the
  634. * right thing
  635. */
  636. const char *argv[1]= { "client_gearmand" };
  637. test= (client_test_st *)calloc(1, sizeof(client_test_st));
  638. if (! test)
  639. {
  640. *error= TEST_MEMORY_ALLOCATION_FAILURE;
  641. return NULL;
  642. }
  643. /**
  644. We start up everything before we allocate so that we don't have to track memory in the forked process.
  645. */
  646. gearmand_pid= test_gearmand_start(CLIENT_TEST_PORT, NULL, 1, argv);
  647. if (gearmand_pid == -1)
  648. {
  649. *error= TEST_FAILURE;
  650. return NULL;
  651. }
  652. test->handle= test_worker_start(CLIENT_TEST_PORT, "client_test", client_test_worker, NULL);
  653. test->gearmand_pid= gearmand_pid;
  654. if (gearman_client_create(&(test->client)) == NULL)
  655. {
  656. *error= TEST_FAILURE;
  657. return NULL;
  658. }
  659. if (gearman_client_add_server(&(test->client), NULL, CLIENT_TEST_PORT) != GEARMAN_SUCCESS)
  660. {
  661. *error= TEST_FAILURE;
  662. return NULL;
  663. }
  664. *error= TEST_SUCCESS;
  665. return (void *)test;
  666. }
  667. test_return_t world_destroy(void *object)
  668. {
  669. client_test_st *test= (client_test_st *)object;
  670. gearman_client_free(&(test->client));
  671. test_gearmand_stop(test->gearmand_pid);
  672. test_worker_stop(test->handle);
  673. free(test);
  674. return TEST_SUCCESS;
  675. }
  676. test_st tests[] ={
  677. {"init", 0, init_test },
  678. {"allocation", 0, allocation_test },
  679. {"clone_test", 0, clone_test },
  680. {"echo", 0, echo_test },
  681. {"options", 0, option_test },
  682. {"submit_job", 0, submit_job_test },
  683. {"submit_null_job", 0, submit_null_job_test },
  684. {"submit_fail_job", 0, submit_fail_job_test },
  685. {"background", 0, background_test },
  686. {"background_failure", 0, background_failure_test },
  687. {"add_servers", 0, add_servers_test },
  688. {"bug_518512_test", 0, bug_518512_test },
  689. {"loop_test", 0, loop_test },
  690. {0, 0, 0}
  691. };
  692. test_st tests_log[] ={
  693. {"submit_log_failure", 0, submit_log_failure },
  694. {0, 0, 0}
  695. };
  696. test_st gearman_strerror_tests[] ={
  697. {"count", 0, strerror_count },
  698. {"strings", 0, strerror_strings },
  699. {0, 0, 0}
  700. };
  701. collection_st collection[] ={
  702. {"gearman_client_st", 0, 0, tests},
  703. {"client-logging", pre_logging, post_logging, tests_log},
  704. {"gearman_strerror", 0, 0, gearman_strerror_tests},
  705. {0, 0, 0, 0}
  706. };
  707. typedef test_return_t (*libgearman_test_callback_fn)(gearman_client_st *);
  708. static test_return_t _runner_default(libgearman_test_callback_fn func, client_test_st *container)
  709. {
  710. if (func)
  711. {
  712. return func(&container->client);
  713. }
  714. else
  715. {
  716. return TEST_SUCCESS;
  717. }
  718. }
  719. static world_runner_st runner= {
  720. (test_callback_runner_fn)_runner_default,
  721. (test_callback_runner_fn)_runner_default,
  722. (test_callback_runner_fn)_runner_default
  723. };
  724. void get_world(world_st *world)
  725. {
  726. world->collections= collection;
  727. world->create= world_create;
  728. world->destroy= world_destroy;
  729. world->runner= &runner;
  730. }