worker_test.cc 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. #include "gear_config.h"
  39. #include <libtest/test.hpp>
  40. using namespace libtest;
  41. #include <cassert>
  42. #include <cstdio>
  43. #include <cstdlib>
  44. #include <cstring>
  45. #include <unistd.h>
  46. #include <libgearman-1.0/gearman.h>
  47. #include <libgearman/connection.hpp>
  48. #include "libgearman/command.h"
  49. #include "libgearman/packet.hpp"
  50. #include "libgearman/universal.hpp"
  51. #include "libgearman/is.hpp"
  52. #include "libgearman/interface/worker.hpp"
  53. #include "libgearman/client.hpp"
  54. #include "libgearman/worker.hpp"
  55. using namespace org::gearmand;
  56. #include "tests/start_worker.h"
  57. #include "tests/workers/v2/call_exception.h"
  58. #include "tests/workers/v2/call_exception.h"
  59. #include "tests/workers/v2/check_order.h"
  60. #include "tests/workers/v2/client_echo.h"
  61. #include "tests/workers/v2/echo_or_react.h"
  62. #include "tests/workers/v2/echo_or_react_chunk.h"
  63. #include "tests/workers/v2/echo_specific_worker.h"
  64. // Port to second gearmand server
  65. static in_port_t second_port;
  66. #if 0
  67. static gearman_return_t exception_fn(gearman_task_st* task)
  68. {
  69. Out << "GEARMAN_WORK_EXCEPTION: Task Handle: " << gearman_task_job_handle(task) << " return:" << gearman_strerror(gearman_task_return(task));
  70. return GEARMAN_SUCCESS;
  71. }
  72. #endif
  73. static void error_logger(const char* message, gearman_verbose_t, void*)
  74. {
  75. Error << message;
  76. }
  77. static test_return_t init_test(void *)
  78. {
  79. gearman_worker_st worker;
  80. ASSERT_TRUE(gearman_worker_create(&worker));
  81. gearman_worker_free(&worker);
  82. return TEST_SUCCESS;
  83. }
  84. static test_return_t allocation_test(void *)
  85. {
  86. gearman_worker_st *worker;
  87. ASSERT_TRUE(worker= gearman_worker_create(NULL));
  88. gearman_worker_free(worker);
  89. return TEST_SUCCESS;
  90. }
  91. static test_return_t sanity_TEST(void *)
  92. {
  93. // Sanity test on initial enum
  94. ASSERT_EQ(0, int(GEARMAN_SUCCESS));
  95. ASSERT_EQ(1, int(GEARMAN_IO_WAIT));
  96. return TEST_SUCCESS;
  97. }
  98. #ifndef __INTEL_COMPILER
  99. #pragma GCC diagnostic ignored "-Wold-style-cast"
  100. #endif
  101. static test_return_t gearman_worker_clone_NULL_NULL(void *)
  102. {
  103. gearman_worker_st *worker= gearman_worker_clone(NULL, NULL);
  104. ASSERT_TRUE(worker);
  105. ASSERT_EQ(true, gearman_is_allocated(worker));
  106. gearman_worker_free(worker);
  107. return TEST_SUCCESS;
  108. }
  109. static test_return_t gearman_worker_clone_NULL_SOURCE(void *)
  110. {
  111. libgearman::Worker source;
  112. gearman_function_t worker_fn= gearman_function_create(echo_specific_worker);
  113. ASSERT_EQ(GEARMAN_SUCCESS, gearman_worker_define_function(&source,
  114. __func__, strlen(__func__),
  115. worker_fn,
  116. 0,
  117. NULL));
  118. ASSERT_EQ(true, gearman_worker_function_exist(&source, __func__, strlen(__func__)));
  119. gearman_worker_st *worker= gearman_worker_clone(NULL, &source);
  120. ASSERT_TRUE(worker);
  121. ASSERT_EQ(true, gearman_is_allocated(worker));
  122. ASSERT_EQ(true, gearman_worker_function_exist(worker, __func__, strlen(__func__)));
  123. gearman_worker_free(worker);
  124. return TEST_SUCCESS;
  125. }
  126. static test_return_t gearman_worker_timeout_default_test(void *)
  127. {
  128. libgearman::Worker worker;
  129. ASSERT_EQ(-1, gearman_worker_timeout(&worker));
  130. return TEST_SUCCESS;
  131. }
  132. static test_return_t gearman_worker_register_TEST(void *)
  133. {
  134. libgearman::Worker worker(libtest::default_port());
  135. ASSERT_EQ(gearman_worker_register(&worker, __func__, 0), GEARMAN_SUCCESS);
  136. return TEST_SUCCESS;
  137. }
  138. static test_return_t gearman_worker_register_GEARMAN_COULD_NOT_CONNECT_TEST(void *)
  139. {
  140. libgearman::Worker worker;
  141. ASSERT_EQ(gearman_worker_register(&worker, __func__, 0), GEARMAN_SUCCESS);
  142. return TEST_SUCCESS;
  143. }
  144. static test_return_t gearman_worker_grab_job_TEST(void *)
  145. {
  146. libgearman::Worker worker(libtest::default_port());
  147. ASSERT_EQ(gearman_worker_register(&worker, __func__, 0), GEARMAN_SUCCESS);
  148. gearman_return_t ret;
  149. gearman_job_st* job= gearman_worker_grab_job(&worker, NULL, &ret);
  150. ASSERT_NULL(job);
  151. ASSERT_EQ(GEARMAN_NO_JOBS, ret);
  152. return TEST_SUCCESS;
  153. }
  154. static test_return_t gearman_worker_grab_job_GEARMAN_NO_SERVERS_NO_FUNCTIONS_TEST(void *)
  155. {
  156. libgearman::Worker worker;
  157. gearman_return_t ret;
  158. gearman_job_st* job= gearman_worker_grab_job(&worker, NULL, &ret);
  159. ASSERT_NULL(job);
  160. ASSERT_EQ(GEARMAN_NO_SERVERS, ret);
  161. return TEST_SUCCESS;
  162. }
  163. static test_return_t gearman_worker_grab_job_GEARMAN_NO_REGISTERED_FUNCTIONS_TEST(void *)
  164. {
  165. libgearman::Worker worker(libtest::default_port());
  166. gearman_return_t ret;
  167. gearman_job_st* job= gearman_worker_grab_job(&worker, NULL, &ret);
  168. ASSERT_NULL(job);
  169. ASSERT_EQ(GEARMAN_NO_REGISTERED_FUNCTIONS, ret);
  170. return TEST_SUCCESS;
  171. }
  172. static test_return_t gearman_worker_grab_job_GEARMAN_NO_SERVERS_TEST(void *)
  173. {
  174. libgearman::Worker worker;
  175. ASSERT_EQ(gearman_worker_register(&worker, __func__, 0), GEARMAN_SUCCESS);
  176. gearman_return_t ret;
  177. gearman_job_st* job= gearman_worker_grab_job(&worker, NULL, &ret);
  178. ASSERT_NULL(job);
  179. ASSERT_EQ(GEARMAN_NO_SERVERS, ret);
  180. return TEST_SUCCESS;
  181. }
  182. static test_return_t gearman_worker_grab_job_GEARMAN_COULD_NOT_CONNECT_TEST(void *)
  183. {
  184. libgearman::Worker worker(23); // Would anyone have telnet enabled?
  185. ASSERT_EQ(gearman_worker_register(&worker, __func__, 0), GEARMAN_SUCCESS);
  186. gearman_return_t ret;
  187. gearman_job_st* job= gearman_worker_grab_job(&worker, NULL, &ret);
  188. ASSERT_NULL(job);
  189. ASSERT_EQ(GEARMAN_COULD_NOT_CONNECT, ret);
  190. return TEST_SUCCESS;
  191. }
  192. static test_return_t gearman_worker_free_TEST(void *)
  193. {
  194. gearman_worker_free(NULL);
  195. return TEST_SUCCESS;
  196. }
  197. static test_return_t gearman_worker_error_TEST(void *)
  198. {
  199. test_null(gearman_worker_error(NULL));
  200. return TEST_SUCCESS;
  201. }
  202. static test_return_t gearman_worker_error_no_error_TEST(void *)
  203. {
  204. libgearman::Worker worker;
  205. test_null(gearman_worker_error(&worker));
  206. return TEST_SUCCESS;
  207. }
  208. static test_return_t gearman_worker_errno_TEST(void *)
  209. {
  210. ASSERT_EQ(EINVAL, gearman_worker_errno(NULL));
  211. return TEST_SUCCESS;
  212. }
  213. static test_return_t gearman_worker_errno_no_error_TEST(void *)
  214. {
  215. libgearman::Worker worker;
  216. ASSERT_EQ(0, gearman_worker_errno(&worker));
  217. return TEST_SUCCESS;
  218. }
  219. static test_return_t gearman_worker_options_TEST(void *)
  220. {
  221. ASSERT_EQ(gearman_worker_options_t(), gearman_worker_options(NULL));
  222. return TEST_SUCCESS;
  223. }
  224. static test_return_t gearman_worker_set_log_fn_TEST(void *)
  225. {
  226. gearman_worker_set_log_fn(NULL, NULL, NULL, GEARMAN_VERBOSE_MAX);
  227. return TEST_SUCCESS;
  228. }
  229. static test_return_t option_test(void *)
  230. {
  231. gearman_worker_options_t default_options;
  232. gearman_worker_st* gear= gearman_worker_create(NULL);
  233. ASSERT_TRUE(gear);
  234. { // Initial Allocated, no changes
  235. ASSERT_TRUE(gearman_is_allocated(gear));
  236. test_false(gearman_is_non_blocking(gear->impl()));
  237. ASSERT_TRUE(gear->impl()->options.packet_init);
  238. test_false(gear->impl()->options.change);
  239. ASSERT_TRUE(gear->impl()->options.grab_uniq);
  240. test_false(gear->impl()->options.timeout_return);
  241. }
  242. /* Set up for default options */
  243. default_options= gearman_worker_options(gear);
  244. /*
  245. We take the basic options, and push
  246. them back in. See if we change anything.
  247. */
  248. gearman_worker_set_options(gear, default_options);
  249. { // Initial Allocated, no changes
  250. ASSERT_TRUE(gearman_is_allocated(gear));
  251. test_false(gearman_is_non_blocking(gear->impl()));
  252. ASSERT_TRUE(gear->impl()->options.packet_init);
  253. test_false(gear->impl()->options.change);
  254. ASSERT_TRUE(gear->impl()->options.grab_uniq);
  255. test_false(gear->impl()->options.timeout_return);
  256. }
  257. /*
  258. We will trying to modify non-mutable options (which should not be allowed)
  259. */
  260. {
  261. gearman_worker_remove_options(gear, GEARMAN_WORKER_ALLOCATED);
  262. { // Initial Allocated, no changes
  263. ASSERT_TRUE(gearman_is_allocated(gear));
  264. test_false(gearman_is_non_blocking(gear->impl()));
  265. ASSERT_TRUE(gear->impl()->options.packet_init);
  266. test_false(gear->impl()->options.change);
  267. ASSERT_TRUE(gear->impl()->options.grab_uniq);
  268. test_false(gear->impl()->options.timeout_return);
  269. }
  270. gearman_worker_remove_options(gear, GEARMAN_WORKER_PACKET_INIT);
  271. { // Initial Allocated, no changes
  272. ASSERT_TRUE(gearman_is_allocated(gear));
  273. test_false(gearman_is_non_blocking(gear->impl()));
  274. ASSERT_TRUE(gear->impl()->options.packet_init);
  275. test_false(gear->impl()->options.change);
  276. ASSERT_TRUE(gear->impl()->options.grab_uniq);
  277. test_false(gear->impl()->options.timeout_return);
  278. }
  279. }
  280. /*
  281. We will test modifying GEARMAN_WORKER_NON_BLOCKING in several manners.
  282. */
  283. {
  284. gearman_worker_remove_options(gear, GEARMAN_WORKER_NON_BLOCKING);
  285. { // GEARMAN_WORKER_NON_BLOCKING set to default, by default.
  286. ASSERT_TRUE(gearman_is_allocated(gear));
  287. test_false(gearman_is_non_blocking(gear->impl()));
  288. ASSERT_TRUE(gear->impl()->options.packet_init);
  289. test_false(gear->impl()->options.change);
  290. ASSERT_TRUE(gear->impl()->options.grab_uniq);
  291. test_false(gear->impl()->options.timeout_return);
  292. }
  293. gearman_worker_add_options(gear, GEARMAN_WORKER_NON_BLOCKING);
  294. { // GEARMAN_WORKER_NON_BLOCKING set to default, by default.
  295. ASSERT_TRUE(gearman_is_allocated(gear));
  296. ASSERT_TRUE(gearman_is_non_blocking(gear->impl()));
  297. ASSERT_TRUE(gear->impl()->options.packet_init);
  298. test_false(gear->impl()->options.change);
  299. ASSERT_TRUE(gear->impl()->options.grab_uniq);
  300. test_false(gear->impl()->options.timeout_return);
  301. }
  302. gearman_worker_set_options(gear, GEARMAN_WORKER_NON_BLOCKING);
  303. { // GEARMAN_WORKER_NON_BLOCKING set to default, by default.
  304. ASSERT_TRUE(gearman_is_allocated(gear));
  305. ASSERT_TRUE(gearman_is_non_blocking(gear->impl()));
  306. ASSERT_TRUE(gear->impl()->options.packet_init);
  307. test_false(gear->impl()->options.change);
  308. test_false(gear->impl()->options.grab_uniq);
  309. test_false(gear->impl()->options.timeout_return);
  310. }
  311. gearman_worker_set_options(gear, GEARMAN_WORKER_GRAB_UNIQ);
  312. { // Everything is now set to false except GEARMAN_WORKER_GRAB_UNIQ, and non-mutable options
  313. ASSERT_TRUE(gearman_is_allocated(gear));
  314. test_false(gearman_is_non_blocking(gear->impl()));
  315. ASSERT_TRUE(gear->impl()->options.packet_init);
  316. test_false(gear->impl()->options.change);
  317. ASSERT_TRUE(gear->impl()->options.grab_uniq);
  318. test_false(gear->impl()->options.timeout_return);
  319. }
  320. /*
  321. Reset options to default. Then add an option, and then add more options. Make sure
  322. the options are all additive.
  323. */
  324. {
  325. gearman_worker_set_options(gear, default_options);
  326. { // See if we return to defaults
  327. ASSERT_TRUE(gearman_is_allocated(gear));
  328. test_false(gearman_is_non_blocking(gear->impl()));
  329. ASSERT_TRUE(gear->impl()->options.packet_init);
  330. test_false(gear->impl()->options.change);
  331. ASSERT_TRUE(gear->impl()->options.grab_uniq);
  332. test_false(gear->impl()->options.timeout_return);
  333. }
  334. gearman_worker_add_options(gear, GEARMAN_WORKER_TIMEOUT_RETURN);
  335. { // All defaults, except timeout_return
  336. ASSERT_TRUE(gearman_is_allocated(gear));
  337. test_false(gearman_is_non_blocking(gear->impl()));
  338. ASSERT_TRUE(gear->impl()->options.packet_init);
  339. test_false(gear->impl()->options.change);
  340. ASSERT_TRUE(gear->impl()->options.grab_uniq);
  341. ASSERT_TRUE(gear->impl()->options.timeout_return);
  342. }
  343. gearman_worker_add_options(gear, (gearman_worker_options_t)(GEARMAN_WORKER_NON_BLOCKING|GEARMAN_WORKER_GRAB_UNIQ));
  344. { // GEARMAN_WORKER_NON_BLOCKING set to default, by default.
  345. ASSERT_TRUE(gearman_is_allocated(gear));
  346. ASSERT_TRUE(gearman_is_non_blocking(gear->impl()));
  347. ASSERT_TRUE(gear->impl()->options.packet_init);
  348. test_false(gear->impl()->options.change);
  349. ASSERT_TRUE(gear->impl()->options.grab_uniq);
  350. ASSERT_TRUE(gear->impl()->options.timeout_return);
  351. }
  352. }
  353. /*
  354. Add an option, and then replace with that option plus a new option.
  355. */
  356. {
  357. gearman_worker_set_options(gear, default_options);
  358. { // See if we return to defaults
  359. ASSERT_TRUE(gearman_is_allocated(gear));
  360. test_false(gearman_is_non_blocking(gear->impl()));
  361. ASSERT_TRUE(gear->impl()->options.packet_init);
  362. test_false(gear->impl()->options.change);
  363. ASSERT_TRUE(gear->impl()->options.grab_uniq);
  364. test_false(gear->impl()->options.timeout_return);
  365. }
  366. gearman_worker_add_options(gear, GEARMAN_WORKER_TIMEOUT_RETURN);
  367. { // All defaults, except timeout_return
  368. ASSERT_TRUE(gearman_is_allocated(gear));
  369. test_false(gearman_is_non_blocking(gear->impl()));
  370. ASSERT_TRUE(gear->impl()->options.packet_init);
  371. test_false(gear->impl()->options.change);
  372. ASSERT_TRUE(gear->impl()->options.grab_uniq);
  373. ASSERT_TRUE(gear->impl()->options.timeout_return);
  374. }
  375. gearman_worker_add_options(gear, (gearman_worker_options_t)(GEARMAN_WORKER_TIMEOUT_RETURN|GEARMAN_WORKER_GRAB_UNIQ));
  376. { // GEARMAN_WORKER_NON_BLOCKING set to default, by default.
  377. ASSERT_TRUE(gearman_is_allocated(gear));
  378. test_false(gearman_is_non_blocking(gear->impl()));
  379. ASSERT_TRUE(gear->impl()->options.packet_init);
  380. test_false(gear->impl()->options.change);
  381. ASSERT_TRUE(gear->impl()->options.grab_uniq);
  382. ASSERT_TRUE(gear->impl()->options.timeout_return);
  383. }
  384. }
  385. }
  386. gearman_worker_free(gear);
  387. return TEST_SUCCESS;
  388. }
  389. static test_return_t echo_test(void*)
  390. {
  391. libgearman::Worker worker;
  392. ASSERT_EQ(gearman_worker_echo(&worker, test_literal_param("This is my echo test")), GEARMAN_NO_SERVERS);
  393. ASSERT_EQ(GEARMAN_SUCCESS, gearman_worker_add_server(&worker, "localhost", libtest::default_port()));
  394. ASSERT_EQ(GEARMAN_SUCCESS, gearman_worker_echo(&worker, test_literal_param("This is my echo test")));
  395. return TEST_SUCCESS;
  396. }
  397. static test_return_t echo_multi_test(void *)
  398. {
  399. libgearman::Worker worker;
  400. ASSERT_EQ(GEARMAN_SUCCESS, gearman_worker_add_server(&worker, "localhost", libtest::default_port()));
  401. const char *value[]= {
  402. "This is my echo test",
  403. "This land is my land",
  404. "This land is your land",
  405. "We the people",
  406. "in order to form a more perfect union",
  407. "establish justice",
  408. NULL
  409. };
  410. const char **ptr= value;
  411. while (*ptr)
  412. {
  413. ASSERT_EQ(gearman_worker_echo(&worker, test_string_make_from_cstr(*ptr)), GEARMAN_SUCCESS);
  414. ptr++;
  415. }
  416. return TEST_SUCCESS;
  417. }
  418. static test_return_t gearman_worker_add_server_GEARMAN_INVALID_ARGUMENT_TEST(void *)
  419. {
  420. if (libtest::check_dns())
  421. {
  422. ASSERT_EQ(GEARMAN_INVALID_ARGUMENT,
  423. gearman_worker_add_server(NULL, "does_not_exist.google.com", libtest::default_port()));
  424. }
  425. return TEST_SUCCESS;
  426. }
  427. static test_return_t gearman_worker_add_server_GEARMAN_GETADDRINFO_TEST(void *)
  428. {
  429. if (libtest::check_dns())
  430. {
  431. gearman_worker_st *worker= gearman_worker_create(NULL);
  432. ASSERT_TRUE(worker);
  433. ASSERT_EQ(gearman_worker_add_server(worker, "does_not_exist.google.com", libtest::default_port()), GEARMAN_GETADDRINFO);
  434. gearman_worker_free(worker);
  435. }
  436. return TEST_SUCCESS;
  437. }
  438. #pragma GCC diagnostic push
  439. #pragma GCC diagnostic ignored "-Wunknown-pragmas"
  440. #ifdef __clang__
  441. #pragma GCC diagnostic ignored "-Wunknown-warning-option"
  442. #endif
  443. #pragma GCC diagnostic ignored "-Wunsafe-loop-optimizations"
  444. static test_return_t job_order_TEST(void *)
  445. {
  446. libgearman::Client client(libtest::default_port());;
  447. gearman_client_add_options(&client, GEARMAN_CLIENT_EXCEPTION);
  448. gearman_client_add_options(&client, GEARMAN_CLIENT_GENERATE_UNIQUE);
  449. std::vector<gearman_task_st*> tasks;
  450. const long order_seed= __LINE__;
  451. uint32_t spaces= 0;
  452. for (long x= order_seed +10; x != order_seed; --x)
  453. {
  454. gearman_return_t ret;
  455. char buffer[30];
  456. memset(buffer, 0, sizeof(buffer));
  457. int buffer_length= snprintf(buffer, sizeof(buffer), "%ld", x);
  458. for (uint32_t y= 0; y <= spaces; ++y)
  459. {
  460. buffer[buffer_length +y]= ' ';
  461. }
  462. ++spaces;
  463. gearman_task_st* task= gearman_client_add_task(&client,
  464. NULL, // task
  465. NULL, // context
  466. __func__, // function_name,
  467. NULL, // unique
  468. buffer, // workload
  469. size_t(buffer_length +1 +spaces), // length of workload
  470. &ret);
  471. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  472. ASSERT_TRUE(task);
  473. tasks.push_back(task);
  474. }
  475. long order_context= order_seed +1;
  476. gearman_function_t check_order_worker_TEST_FN= gearman_function_create(check_order_worker);
  477. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  478. NULL,
  479. __func__,
  480. check_order_worker_TEST_FN,
  481. (void*)&order_context,
  482. gearman_worker_options_t(),
  483. 0)); // timeout
  484. {
  485. gearman_return_t ret;
  486. do {
  487. ret= gearman_client_run_tasks(&client);
  488. } while (gearman_continue(ret));
  489. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  490. }
  491. for (std::vector<gearman_task_st*>::iterator iter= tasks.begin();
  492. iter != tasks.end(); ++iter)
  493. {
  494. if (gearman_task_return(*iter) != GEARMAN_SUCCESS)
  495. {
  496. Error << gearman_task_error(*iter);
  497. }
  498. ASSERT_EQ(GEARMAN_SUCCESS, gearman_task_return(*iter));
  499. }
  500. return TEST_SUCCESS;
  501. }
  502. #pragma GCC diagnostic pop
  503. #pragma GCC diagnostic push
  504. #pragma GCC diagnostic ignored "-Wunknown-pragmas"
  505. #ifdef __clang__
  506. #pragma GCC diagnostic ignored "-Wunknown-warning-option"
  507. #endif
  508. #pragma GCC diagnostic ignored "-Wunsafe-loop-optimizations"
  509. static test_return_t job_order_background_TEST(void *)
  510. {
  511. libgearman::Client client(libtest::default_port());
  512. ASSERT_EQ(true, gearman_client_set_server_option(&client, test_literal_param("exceptions")));
  513. gearman_client_add_options(&client, GEARMAN_CLIENT_EXCEPTION);
  514. gearman_client_add_options(&client, GEARMAN_CLIENT_GENERATE_UNIQUE);
  515. libgearman::Worker worker(libtest::default_port());
  516. ASSERT_EQ(gearman_worker_register(&worker, __func__, 0), GEARMAN_SUCCESS);
  517. std::vector<gearman_task_st*> tasks;
  518. const uint32_t order_seed= __LINE__;
  519. uint32_t spaces= 0;
  520. for (uint32_t x= order_seed +10; x != order_seed; --x)
  521. {
  522. gearman_return_t ret;
  523. char buffer[30];
  524. memset(buffer, 0, sizeof(buffer));
  525. int buffer_length= snprintf(buffer, sizeof(buffer), "%u", x);
  526. for (uint32_t y= 0; y <= spaces; ++y)
  527. {
  528. buffer[buffer_length +y]= ' ';
  529. }
  530. ++spaces;
  531. gearman_task_st* task= gearman_client_add_task_background(&client,
  532. NULL, // task
  533. NULL, // context
  534. __func__, // function_name,
  535. NULL, // unique
  536. buffer, // workload
  537. size_t(buffer_length +1 +spaces), // length of workload
  538. &ret);
  539. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  540. ASSERT_TRUE(task);
  541. tasks.push_back(task);
  542. }
  543. {
  544. gearman_return_t ret;
  545. do {
  546. ret= gearman_client_run_tasks(&client);
  547. } while (gearman_continue(ret));
  548. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  549. }
  550. for (std::vector<gearman_task_st*>::iterator iter= tasks.begin();
  551. iter != tasks.end(); ++iter)
  552. {
  553. if (gearman_failed(gearman_task_return(*iter)))
  554. {
  555. if (gearman_task_return(*iter) != GEARMAN_UNKNOWN_STATE)
  556. {
  557. Error << "gearman_task_error(" << gearman_task_error(*iter) << ") gearman_task_return(" << gearman_strerror(gearman_task_return(*iter)) << ")";
  558. ASSERT_EQ(GEARMAN_UNKNOWN_STATE, gearman_task_return(*iter));
  559. ASSERT_NULL(gearman_task_error(*iter));
  560. }
  561. }
  562. }
  563. for (uint32_t x= 0; x < 10; ++x)
  564. {
  565. gearman_return_t ret;
  566. gearman_job_st* job= gearman_worker_grab_job(&worker, NULL, &ret);
  567. (void)job;
  568. ASSERT_TRUE(job);
  569. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  570. ASSERT_EQ(GEARMAN_SUCCESS, gearman_job_send_exception(job, test_literal_param("returned exception")));
  571. }
  572. return TEST_SUCCESS;
  573. }
  574. #pragma GCC diagnostic pop
  575. static test_return_t echo_max_test(void *)
  576. {
  577. libgearman::Worker worker(libtest::default_port());;
  578. ASSERT_EQ(GEARMAN_ARGUMENT_TOO_LARGE,
  579. gearman_worker_echo(&worker, "This is my echo test", GEARMAN_MAX_ECHO_SIZE +1));
  580. return TEST_SUCCESS;
  581. }
  582. // The idea is to return GEARMAN_ERROR until we hit limit, then return
  583. // GEARMAN_SUCCESS
  584. static gearman_return_t GEARMAN_ERROR_limit_worker(gearman_job_st* job, void *context)
  585. {
  586. assert(gearman_job_workload_size(job) == 0);
  587. assert(gearman_job_workload(job) == NULL);
  588. size_t *ret= (size_t*)context;
  589. if (*ret > 0)
  590. {
  591. *ret= (*ret) -1;
  592. return GEARMAN_ERROR;
  593. }
  594. if (gearman_failed(gearman_job_send_data(job, test_literal_param("OK"))))
  595. {
  596. // We should return ERROR here, but that would then possibly loop
  597. return GEARMAN_FAIL;
  598. }
  599. return GEARMAN_SUCCESS;
  600. }
  601. static gearman_return_t GEARMAN_ERROR_worker(gearman_job_st*, void* context)
  602. {
  603. size_t *ret= (size_t*)context;
  604. *ret= (*ret) +1;
  605. return GEARMAN_ERROR;
  606. }
  607. static gearman_return_t error_return_worker(gearman_job_st* job, void *)
  608. {
  609. assert(sizeof(gearman_return_t) == gearman_job_workload_size(job));
  610. const gearman_return_t *ret= (const gearman_return_t*)gearman_job_workload(job);
  611. if (gearman_failed(gearman_job_send_data(job, gearman_strerror(*ret), strlen(gearman_strerror(*ret)))))
  612. {
  613. return GEARMAN_ERROR;
  614. }
  615. return *ret;
  616. }
  617. static test_return_t error_return_TEST(void *)
  618. {
  619. libgearman::Client client(libtest::default_port());
  620. ASSERT_EQ(GEARMAN_SUCCESS, gearman_client_echo(&client, test_literal_param(__func__)));
  621. libgearman::Worker worker(libtest::default_port());
  622. ASSERT_EQ(gearman_worker_register(&worker, __func__, 0), GEARMAN_SUCCESS);
  623. gearman_task_attr_t task_attr= gearman_task_attr_init_background(GEARMAN_JOB_PRIORITY_NORMAL);
  624. std::vector<std::string> job_handles;
  625. job_handles.resize(int(GEARMAN_MAX_RETURN));
  626. for (gearman_return_t x= GEARMAN_IO_WAIT; int(x) < int(GEARMAN_MAX_RETURN); x= gearman_return_t((int(x) +1)))
  627. {
  628. if (x == GEARMAN_SHUTDOWN)
  629. {
  630. continue;
  631. }
  632. if (x == GEARMAN_WORK_ERROR)
  633. {
  634. continue;
  635. }
  636. gearman_argument_t arg= gearman_argument_make(NULL, 0, (const char*)&x, sizeof(gearman_return_t));
  637. gearman_task_st *task= gearman_execute(&client,
  638. test_literal_param(__func__),
  639. NULL, 0, // unique
  640. &task_attr, // gearman_task_attr_t
  641. &arg, // gearman_argument_t
  642. NULL); // context
  643. ASSERT_TRUE(task);
  644. bool is_known;
  645. ASSERT_EQ(gearman_client_job_status(&client, gearman_task_job_handle(task), &is_known, NULL, NULL, NULL), GEARMAN_SUCCESS);
  646. ASSERT_TRUE(is_known);
  647. job_handles[int(x)].append(gearman_task_job_handle(task));
  648. }
  649. gearman_function_t error_return_TEST_FN= gearman_function_create(error_return_worker);
  650. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  651. NULL,
  652. __func__,
  653. error_return_TEST_FN,
  654. NULL,
  655. gearman_worker_options_t(),
  656. 0)); // timeout
  657. for (gearman_return_t x= GEARMAN_IO_WAIT; int(x) < int(GEARMAN_MAX_RETURN); x= gearman_return_t((int(x) +1)))
  658. {
  659. if (x == GEARMAN_SHUTDOWN)
  660. {
  661. continue;
  662. }
  663. if (x == GEARMAN_WORK_ERROR)
  664. {
  665. continue;
  666. }
  667. bool is_known;
  668. gearman_return_t rc;
  669. do {
  670. rc= gearman_client_job_status(&client, job_handles[int(x)].c_str(), &is_known, NULL, NULL, NULL);
  671. } while (gearman_continue(rc) or is_known);
  672. test_false(handle->is_shutdown());
  673. }
  674. return TEST_SUCCESS;
  675. }
  676. static test_return_t GEARMAN_ERROR_check_retry_TEST(void *)
  677. {
  678. libgearman::Client client(libtest::default_port());
  679. ASSERT_EQ(GEARMAN_SUCCESS, gearman_client_echo(&client, test_literal_param(__func__)));
  680. size_t count= 0;
  681. gearman_function_t GEARMAN_ERROR_FN= gearman_function_create(GEARMAN_ERROR_worker);
  682. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  683. NULL,
  684. __func__,
  685. GEARMAN_ERROR_FN,
  686. &count,
  687. gearman_worker_options_t(),
  688. 0)); // timeout
  689. gearman_task_st *task= gearman_execute(&client,
  690. test_literal_param(__func__),
  691. NULL, 0, // unique
  692. NULL, // gearman_task_attr_t
  693. NULL, // gearman_argument_t
  694. NULL); // context
  695. ASSERT_NOT_NULL(task);
  696. gearman_return_t ret;
  697. do {
  698. ret= gearman_client_run_tasks(&client);
  699. ASSERT_TRUE(gearman_success(ret) or ret == GEARMAN_IO_WAIT);
  700. } while (gearman_client_has_active_tasks(&client));
  701. ASSERT_EQ(GEARMAN_WORK_FAIL, gearman_task_return(task));
  702. ASSERT_EQ(count, 30);
  703. return TEST_SUCCESS;
  704. }
  705. static test_return_t GEARMAN_ERROR_always_return_TEST(void *)
  706. {
  707. libgearman::Client client(libtest::default_port());
  708. ASSERT_EQ(GEARMAN_SUCCESS, gearman_client_echo(&client, test_literal_param(__func__)));
  709. size_t count= 0;
  710. gearman_function_t GEARMAN_ERROR_FN= gearman_function_create(GEARMAN_ERROR_worker);
  711. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  712. NULL,
  713. __func__,
  714. GEARMAN_ERROR_FN,
  715. &count,
  716. gearman_worker_options_t(),
  717. 0)); // timeout
  718. std::vector<gearman_task_st*> tasks;
  719. for (size_t x= 0; x < 24; x++)
  720. {
  721. gearman_task_st *task= gearman_execute(&client,
  722. test_literal_param(__func__),
  723. NULL, 0, // unique
  724. NULL, // gearman_task_attr_t
  725. NULL, // gearman_argument_t
  726. NULL); // context
  727. ASSERT_TRUE(task);
  728. tasks.push_back(task);
  729. }
  730. gearman_return_t ret;
  731. do {
  732. ret= gearman_client_run_tasks(&client);
  733. ASSERT_TRUE(gearman_success(ret) or ret == GEARMAN_IO_WAIT);
  734. } while (gearman_client_has_active_tasks(&client));
  735. for (std::vector<gearman_task_st*>::iterator iter= tasks.begin(); iter != tasks.end(); iter++)
  736. {
  737. ASSERT_TRUE(*iter);
  738. ASSERT_EQ(GEARMAN_WORK_FAIL, gearman_task_return(*iter));
  739. }
  740. return TEST_SUCCESS;
  741. }
  742. static test_return_t GEARMAN_ERROR_return_TEST(void *)
  743. {
  744. libgearman::Client client(libtest::default_port());
  745. ASSERT_EQ(GEARMAN_SUCCESS, gearman_client_echo(&client, test_literal_param(__func__)));
  746. size_t count= 0;
  747. gearman_function_t GEARMAN_ERROR_FN= gearman_function_create(GEARMAN_ERROR_limit_worker);
  748. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  749. NULL,
  750. __func__,
  751. GEARMAN_ERROR_FN,
  752. &count,
  753. gearman_worker_options_t(),
  754. 0)); // timeout
  755. for (size_t x= 0; x < 24; x++)
  756. {
  757. count= x;
  758. gearman_task_st *task= gearman_execute(&client,
  759. test_literal_param(__func__),
  760. NULL, 0, // unique
  761. NULL, // gearman_task_attr_t
  762. NULL, // gearman_argument_t
  763. NULL); // context
  764. ASSERT_TRUE(task);
  765. gearman_return_t rc;
  766. bool is_known;
  767. do {
  768. rc= gearman_client_job_status(&client, gearman_task_job_handle(task), &is_known, NULL, NULL, NULL);
  769. } while (gearman_continue(rc) or is_known);
  770. ASSERT_EQ(gearman_task_return(task), GEARMAN_SUCCESS);
  771. test_zero(count); // Since we hit zero we know that we ran enough times.
  772. gearman_result_st *result= gearman_task_result(task);
  773. ASSERT_TRUE(result);
  774. test_memcmp("OK", gearman_result_value(result), strlen("ok"));
  775. }
  776. return TEST_SUCCESS;
  777. }
  778. static test_return_t GEARMAN_FAIL_return_TEST(void *)
  779. {
  780. libgearman::Client client(libtest::default_port());
  781. ASSERT_EQ(GEARMAN_SUCCESS, gearman_client_echo(&client, test_literal_param(__func__)));
  782. gearman_function_t error_return_TEST_FN= gearman_function_create(error_return_worker);
  783. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  784. NULL,
  785. __func__,
  786. error_return_TEST_FN,
  787. NULL,
  788. gearman_worker_options_t(),
  789. 0)); // timeout
  790. int count= 3;
  791. while(--count)
  792. {
  793. gearman_return_t x= GEARMAN_FAIL;
  794. gearman_argument_t arg= gearman_argument_make(NULL, 0, (const char*)&x, sizeof(gearman_return_t));
  795. gearman_task_st *task= gearman_execute(&client,
  796. test_literal_param(__func__),
  797. NULL, 0, // unique
  798. NULL, // gearman_task_attr_t
  799. &arg, // gearman_argument_t
  800. NULL); // context
  801. ASSERT_TRUE(task);
  802. gearman_return_t rc;
  803. bool is_known;
  804. do {
  805. rc= gearman_client_job_status(&client, gearman_task_job_handle(task), &is_known, NULL, NULL, NULL);
  806. } while (gearman_continue(rc) or is_known);
  807. {
  808. ASSERT_EQ(GEARMAN_FAIL, gearman_task_return(task));
  809. }
  810. }
  811. return TEST_SUCCESS;
  812. }
  813. static test_return_t gearman_job_send_exception_mass_TEST(void *)
  814. {
  815. gearman_function_t call_exception_WORKER_FN= gearman_function_create(call_exception_WORKER);
  816. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  817. NULL,
  818. "exception",
  819. call_exception_WORKER_FN,
  820. NULL,
  821. gearman_worker_options_t(),
  822. 0)); // timeout
  823. std::vector<gearman_task_st*> tasks;
  824. libgearman::Client client(libtest::default_port());
  825. #if 0
  826. gearman_exception_fn *func= exception_fn;
  827. gearman_client_set_exception_fn(&client, func);
  828. #endif
  829. #if 0
  830. ASSERT_EQ(true, gearman_client_set_server_option(&client, test_literal_param("exceptions")));
  831. #endif
  832. gearman_client_add_options(&client, GEARMAN_CLIENT_EXCEPTION);
  833. for (size_t x= 0; x < 100; ++x)
  834. {
  835. char buffer[GEARMAN_MAXIMUM_INTEGER_DISPLAY_LENGTH];
  836. int buffer_length= snprintf(buffer, sizeof(buffer), "%d", int(x));
  837. gearman_return_t ret;
  838. gearman_task_st *task= gearman_client_add_task(&client,
  839. NULL, // preallocated task
  840. NULL, // context
  841. "exception", // function
  842. NULL, // unique
  843. (const void*)buffer, size_t(buffer_length), // workload
  844. &ret);
  845. ASSERT_EQ(ret, GEARMAN_SUCCESS);
  846. ASSERT_TRUE(task);
  847. tasks.push_back(task);
  848. }
  849. bool more= true;
  850. while (more)
  851. {
  852. for (std::vector<gearman_task_st*>::iterator iter= tasks.begin();
  853. iter != tasks.end(); ++iter)
  854. {
  855. if (gearman_task_return(*iter) == GEARMAN_UNKNOWN_STATE)
  856. {
  857. {
  858. gearman_return_t ret;
  859. do {
  860. ret= gearman_client_run_tasks(&client);
  861. } while (gearman_continue(ret));
  862. if (gearman_failed(ret))
  863. {
  864. Error << gearman_strerror(ret);
  865. }
  866. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  867. }
  868. continue;
  869. }
  870. else
  871. {
  872. if (gearman_client_has_option(&client, GEARMAN_CLIENT_EXCEPTION))
  873. {
  874. ASSERT_EQ(GEARMAN_WORK_EXCEPTION, gearman_task_return(*iter));
  875. }
  876. else
  877. {
  878. ASSERT_EQ(GEARMAN_WORK_FAIL, gearman_task_return(*iter));
  879. }
  880. }
  881. more= false;
  882. }
  883. }
  884. return TEST_SUCCESS;
  885. }
  886. static test_return_t gearman_job_client_TEST(void *)
  887. {
  888. gearman_function_t call_client_echo_WORKER_FN= gearman_function_create(client_echo_WORKER);
  889. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  890. NULL,
  891. "client_echo",
  892. call_client_echo_WORKER_FN,
  893. NULL,
  894. gearman_worker_options_t(),
  895. 0)); // timeout
  896. std::vector<gearman_task_st*> tasks;
  897. libgearman::Client client(libtest::default_port());
  898. gearman_client_add_options(&client, GEARMAN_CLIENT_EXCEPTION);
  899. for (size_t x= 0; x < 10; ++x)
  900. {
  901. char buffer[GEARMAN_MAXIMUM_INTEGER_DISPLAY_LENGTH];
  902. int buffer_length= snprintf(buffer, sizeof(buffer), "%d", int(x));
  903. gearman_return_t ret;
  904. gearman_task_st *task= gearman_client_add_task(&client,
  905. NULL, // preallocated task
  906. NULL, // context
  907. "client_echo", // function
  908. NULL, // unique
  909. (const void*)buffer, size_t(buffer_length), // workload
  910. &ret);
  911. ASSERT_EQ(ret, GEARMAN_SUCCESS);
  912. ASSERT_TRUE(task);
  913. tasks.push_back(task);
  914. }
  915. bool more= true;
  916. while (more)
  917. {
  918. for (std::vector<gearman_task_st*>::iterator iter= tasks.begin();
  919. iter != tasks.end(); ++iter)
  920. {
  921. if (gearman_task_return(*iter) == GEARMAN_UNKNOWN_STATE)
  922. {
  923. {
  924. gearman_return_t ret;
  925. do {
  926. ret= gearman_client_run_tasks(&client);
  927. } while (gearman_continue(ret));
  928. if (gearman_failed(ret))
  929. {
  930. Error << gearman_strerror(ret);
  931. }
  932. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  933. }
  934. continue;
  935. }
  936. else
  937. {
  938. ASSERT_EQ(GEARMAN_SUCCESS, gearman_task_return(*iter));
  939. }
  940. more= false;
  941. }
  942. }
  943. return TEST_SUCCESS;
  944. }
  945. static test_return_t gearman_job_send_exception_TEST(void *)
  946. {
  947. libgearman::Client client(libtest::default_port());
  948. gearman_client_set_log_fn(&client, error_logger, NULL, GEARMAN_VERBOSE_ERROR);
  949. #if 0
  950. gearman_exception_fn *func= exception_fn;
  951. gearman_client_set_exception_fn(&client, func);
  952. #endif
  953. gearman_client_add_options(&client, GEARMAN_CLIENT_EXCEPTION);
  954. gearman_function_t exception_WORKER_FN= gearman_function_create(exception_WORKER);
  955. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  956. NULL,
  957. __func__,
  958. exception_WORKER_FN,
  959. NULL,
  960. gearman_worker_options_t(),
  961. 0)); // timeout
  962. gearman_return_t ret;
  963. gearman_task_st *task= gearman_client_add_task(&client, NULL, NULL,
  964. __func__, NULL, "dog", 3,
  965. &ret);
  966. ASSERT_TRUE(task);
  967. ASSERT_EQ(ret, GEARMAN_SUCCESS);
  968. do {
  969. ret= gearman_client_run_tasks(&client);
  970. } while (gearman_continue(ret));
  971. if (gearman_client_has_option(&client, GEARMAN_CLIENT_EXCEPTION))
  972. {
  973. ASSERT_EQ(GEARMAN_WORK_EXCEPTION, gearman_task_return(task));
  974. }
  975. else
  976. {
  977. ASSERT_EQ(GEARMAN_WORK_FAIL, gearman_task_return(task));
  978. }
  979. ASSERT_TRUE(gearman_task_has_exception(task));
  980. gearman_string_t exception= gearman_task_exception(task);
  981. ASSERT_STREQ("dog", gearman_c_str(exception));
  982. return TEST_SUCCESS;
  983. }
  984. static test_return_t gearman_client_job_status_is_known_TEST(void *)
  985. {
  986. libgearman::Client client(libtest::default_port());
  987. libgearman::Worker worker(libtest::default_port());
  988. ASSERT_EQ(gearman_worker_register(&worker, __func__, 0), GEARMAN_SUCCESS);
  989. gearman_job_handle_t job_handle;
  990. ASSERT_EQ(gearman_client_do_background(&client, __func__, NULL, NULL, 0, job_handle), GEARMAN_SUCCESS);
  991. bool is_known;
  992. ASSERT_EQ(gearman_client_job_status(&client, job_handle, &is_known, NULL, NULL, NULL), GEARMAN_SUCCESS);
  993. ASSERT_TRUE(is_known);
  994. gearman_function_t echo_or_react_worker_v2_FN= gearman_function_create(echo_or_react_worker_v2);
  995. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  996. NULL,
  997. __func__,
  998. echo_or_react_worker_v2_FN,
  999. NULL,
  1000. gearman_worker_options_t(),
  1001. 0)); // timeout
  1002. return TEST_SUCCESS;
  1003. }
  1004. static test_return_t abandoned_worker_test(void *)
  1005. {
  1006. gearman_job_handle_t job_handle;
  1007. const void *args[2];
  1008. size_t args_size[2];
  1009. {
  1010. libgearman::Client client(libtest::default_port());
  1011. ASSERT_EQ(gearman_client_do_background(&client, "abandoned_worker", NULL, NULL, 0, job_handle),
  1012. GEARMAN_SUCCESS);
  1013. }
  1014. /* Now take job with one worker. */
  1015. gearman_universal_st universal;
  1016. universal.timeout= 3000;
  1017. gearman_connection_st *connection1;
  1018. ASSERT_TRUE(connection1= gearman_connection_create(universal, NULL, default_port()));
  1019. gearman_packet_st packet;
  1020. args[0]= "abandoned_worker";
  1021. args_size[0]= strlen("abandoned_worker");
  1022. ASSERT_EQ(GEARMAN_SUCCESS,
  1023. gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  1024. GEARMAN_COMMAND_CAN_DO,
  1025. args, args_size, 1));
  1026. ASSERT_EQ(connection1->send_packet(packet, true),
  1027. GEARMAN_SUCCESS);
  1028. gearman_packet_free(&packet);
  1029. gearman_return_t ret;
  1030. ASSERT_EQ(GEARMAN_SUCCESS,
  1031. gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST, GEARMAN_COMMAND_GRAB_JOB, NULL, NULL, 0));
  1032. ASSERT_EQ(GEARMAN_SUCCESS, connection1->send_packet(packet, true));
  1033. gearman_packet_free(&packet);
  1034. connection1->receiving(packet, ret, false);
  1035. ASSERT_TRUE(not (ret != GEARMAN_SUCCESS or packet.command != GEARMAN_COMMAND_JOB_ASSIGN));
  1036. test_strcmp(job_handle, packet.arg[0]); // unexepcted job
  1037. gearman_packet_free(&packet);
  1038. gearman_connection_st *connection2;
  1039. ASSERT_TRUE(connection2= gearman_connection_create(universal, NULL, default_port()));
  1040. args[0]= "abandoned_worker";
  1041. args_size[0]= strlen("abandoned_worker");
  1042. ASSERT_EQ(GEARMAN_SUCCESS, gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  1043. GEARMAN_COMMAND_CAN_DO,
  1044. args, args_size, 1));
  1045. ASSERT_EQ(GEARMAN_SUCCESS, connection2->send_packet(packet, true));
  1046. gearman_packet_free(&packet);
  1047. args[0]= job_handle;
  1048. args_size[0]= strlen(job_handle) + 1;
  1049. args[1]= "test";
  1050. args_size[1]= 4;
  1051. ASSERT_EQ(GEARMAN_SUCCESS, gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  1052. GEARMAN_COMMAND_WORK_COMPLETE,
  1053. args, args_size, 2));
  1054. ASSERT_EQ(GEARMAN_SUCCESS, connection2->send_packet(packet, true));
  1055. gearman_packet_free(&packet);
  1056. gearman_universal_set_timeout(universal, 1000);
  1057. connection2->receiving(packet, ret, false);
  1058. ASSERT_TRUE(not (ret != GEARMAN_SUCCESS or packet.command != GEARMAN_COMMAND_ERROR));
  1059. delete connection1;
  1060. delete connection2;
  1061. gearman_packet_free(&packet);
  1062. gearman_universal_free(universal);
  1063. return TEST_SUCCESS;
  1064. }
  1065. static void *no_unique_worker(gearman_job_st *job,
  1066. void *, size_t *size,
  1067. gearman_return_t *ret_ptr)
  1068. {
  1069. if (gearman_job_unique(job) and strlen(gearman_job_unique(job)))
  1070. {
  1071. *ret_ptr= GEARMAN_WORK_FAIL;
  1072. }
  1073. else
  1074. {
  1075. *ret_ptr= GEARMAN_SUCCESS;
  1076. }
  1077. *size= 0;
  1078. return NULL;
  1079. }
  1080. static void *check_unique_worker(gearman_job_st *job,
  1081. void *context, size_t *size,
  1082. gearman_return_t *ret_ptr)
  1083. {
  1084. if (gearman_job_unique(job))
  1085. {
  1086. size_t length= strlen(gearman_job_unique(job));
  1087. if (length == gearman_job_workload_size(job))
  1088. {
  1089. if (not memcmp(gearman_job_unique(job), gearman_job_workload(job),length))
  1090. {
  1091. bool *success= (bool *)context;
  1092. if (success)
  1093. *success= true;
  1094. *ret_ptr= GEARMAN_SUCCESS;
  1095. *size= length;
  1096. return strdup((char*)gearman_job_unique(job));
  1097. }
  1098. }
  1099. }
  1100. *size= 0;
  1101. *ret_ptr= GEARMAN_WORK_FAIL;
  1102. return NULL;
  1103. }
  1104. static void *fail_worker(gearman_job_st *,
  1105. void *, size_t *size,
  1106. gearman_return_t *ret_ptr)
  1107. {
  1108. *ret_ptr= GEARMAN_WORK_FAIL;
  1109. *size= 0;
  1110. return NULL;
  1111. }
  1112. static test_return_t gearman_worker_add_function_test(void *)
  1113. {
  1114. libgearman::Worker worker;
  1115. char function_name[GEARMAN_FUNCTION_MAX_SIZE];
  1116. snprintf(function_name, GEARMAN_FUNCTION_MAX_SIZE, "_%s%d", __func__, int(random()));
  1117. ASSERT_EQ(GEARMAN_SUCCESS,
  1118. gearman_worker_add_function(&worker, function_name,0, fail_worker, NULL));
  1119. ASSERT_EQ(true, gearman_worker_function_exist(&worker, test_string_make_from_array(function_name)));
  1120. ASSERT_EQ(GEARMAN_SUCCESS,
  1121. gearman_worker_unregister(&worker, function_name));
  1122. ASSERT_EQ(false, gearman_worker_function_exist(&worker, function_name, strlen(function_name)));
  1123. /* Make sure we have removed it */
  1124. ASSERT_EQ(GEARMAN_NO_REGISTERED_FUNCTION,
  1125. gearman_worker_unregister(&worker, function_name));
  1126. return TEST_SUCCESS;
  1127. }
  1128. static void log_callback(const char *, gearman_verbose_t, void *context)
  1129. {
  1130. uint32_t *counter= (uint32_t*)context;
  1131. *counter= *counter +1;
  1132. }
  1133. static test_return_t gearman_worker_timeout_TEST(void *)
  1134. {
  1135. libgearman::Worker worker(libtest::default_port());
  1136. ASSERT_EQ(GEARMAN_SUCCESS,
  1137. gearman_worker_add_function(&worker, __func__, 0, fail_worker, NULL));
  1138. gearman_worker_set_timeout(&worker, 1000);
  1139. uint32_t counter= 0;
  1140. gearman_worker_set_log_fn(&worker, log_callback, &counter, GEARMAN_VERBOSE_ERROR);
  1141. gearman_return_t ret= gearman_worker_work(&worker);
  1142. ASSERT_EQ(ret, GEARMAN_TIMEOUT);
  1143. ASSERT_EQ(counter, 1);
  1144. return TEST_SUCCESS;
  1145. }
  1146. static test_return_t gearman_worker_add_function_multi_test(void *)
  1147. {
  1148. libgearman::Worker worker;
  1149. for (uint32_t x= 0; x < 100; x++)
  1150. {
  1151. char buffer[1024];
  1152. snprintf(buffer, 1024, "%u%s", x, __func__);
  1153. ASSERT_EQ(GEARMAN_SUCCESS,
  1154. gearman_worker_add_function(&worker, buffer, 0, fail_worker, NULL));
  1155. }
  1156. for (uint32_t x= 0; x < 100; x++)
  1157. {
  1158. char buffer[1024];
  1159. snprintf(buffer, 1024, "%u%s", x, __func__);
  1160. ASSERT_EQ(GEARMAN_SUCCESS,
  1161. gearman_worker_unregister(&worker, buffer));
  1162. }
  1163. for (uint32_t x= 0; x < 100; x++)
  1164. {
  1165. char buffer[1024];
  1166. snprintf(buffer, 1024, "%u%s", x, __func__);
  1167. ASSERT_EQ(GEARMAN_NO_REGISTERED_FUNCTION,
  1168. gearman_worker_unregister(&worker, buffer));
  1169. }
  1170. return TEST_SUCCESS;
  1171. }
  1172. static test_return_t gearman_worker_unregister_all_test(void *)
  1173. {
  1174. libgearman::Worker worker;
  1175. for (uint32_t x= 0; x < 100; x++)
  1176. {
  1177. char buffer[1024];
  1178. snprintf(buffer, sizeof(buffer), "%u%s", x, __func__);
  1179. gearman_return_t rc= gearman_worker_add_function(&worker,
  1180. buffer,
  1181. 0, fail_worker, NULL);
  1182. ASSERT_EQ(rc, GEARMAN_SUCCESS);
  1183. }
  1184. ASSERT_EQ(GEARMAN_SUCCESS,
  1185. gearman_worker_unregister_all(&worker));
  1186. for (uint32_t x= 0; x < 100; x++)
  1187. {
  1188. char buffer[1024];
  1189. snprintf(buffer, sizeof(buffer), "%u%s", x, __func__);
  1190. gearman_return_t rc= gearman_worker_unregister(&worker, buffer);
  1191. ASSERT_EQ(rc, GEARMAN_NO_REGISTERED_FUNCTION);
  1192. }
  1193. ASSERT_EQ(gearman_worker_unregister_all(&worker),
  1194. GEARMAN_NO_REGISTERED_FUNCTIONS);
  1195. return TEST_SUCCESS;
  1196. }
  1197. static test_return_t gearman_worker_work_with_test(int timeout, gearman_worker_options_t option)
  1198. {
  1199. libgearman::Worker worker(libtest::default_port());
  1200. if (option)
  1201. {
  1202. gearman_worker_add_options(&worker, option);
  1203. if (option == GEARMAN_WORKER_NON_BLOCKING)
  1204. {
  1205. ASSERT_TRUE(gearman_worker_options(&worker) & GEARMAN_WORKER_NON_BLOCKING);
  1206. }
  1207. }
  1208. char function_name[GEARMAN_FUNCTION_MAX_SIZE];
  1209. snprintf(function_name, GEARMAN_FUNCTION_MAX_SIZE, "_%s%d", __func__, int(random()));
  1210. ASSERT_EQ(gearman_worker_add_function(&worker,
  1211. function_name,
  1212. 0, fail_worker, NULL),
  1213. GEARMAN_SUCCESS);
  1214. gearman_worker_set_timeout(&worker, timeout);
  1215. if (option == GEARMAN_WORKER_NON_BLOCKING)
  1216. {
  1217. ASSERT_EQ(GEARMAN_IO_WAIT,
  1218. gearman_worker_work(&worker));
  1219. ASSERT_EQ(GEARMAN_IO_WAIT,
  1220. gearman_worker_work(&worker));
  1221. }
  1222. else
  1223. {
  1224. ASSERT_EQ(GEARMAN_TIMEOUT,
  1225. gearman_worker_work(&worker));
  1226. ASSERT_EQ(GEARMAN_TIMEOUT,
  1227. gearman_worker_work(&worker));
  1228. }
  1229. /* Make sure we have removed the worker function */
  1230. ASSERT_EQ(GEARMAN_SUCCESS,
  1231. gearman_worker_unregister(&worker, function_name));
  1232. return TEST_SUCCESS;
  1233. }
  1234. static test_return_t gearman_worker_work_with_option(gearman_worker_options_t option)
  1235. {
  1236. int timeout[]= { 500, 1000, 2000, 8000, 0 };
  1237. // First we try with immediate timeout
  1238. ASSERT_EQ(TEST_SUCCESS, gearman_worker_work_with_test(0, option));
  1239. for (size_t x= 0; timeout[x]; ++x)
  1240. {
  1241. ASSERT_EQ(TEST_SUCCESS, gearman_worker_work_with_test(timeout[x], option));
  1242. }
  1243. return TEST_SUCCESS;
  1244. }
  1245. static test_return_t gearman_worker_work_with_TEST(void*)
  1246. {
  1247. return gearman_worker_work_with_option(gearman_worker_options_t());
  1248. }
  1249. static test_return_t gearman_worker_work_with_GEARMAN_WORKER_NON_BLOCKING_TEST(void*)
  1250. {
  1251. return gearman_worker_work_with_option(GEARMAN_WORKER_NON_BLOCKING);
  1252. }
  1253. static test_return_t gearman_worker_context_test(void *)
  1254. {
  1255. libgearman::Worker worker;
  1256. test_false(gearman_worker_context(&worker));
  1257. int value= 5;
  1258. gearman_worker_set_context(&worker, &value);
  1259. int *ptr= (int *)gearman_worker_context(&worker);
  1260. ASSERT_TRUE(ptr == &value);
  1261. ASSERT_TRUE(*ptr == value);
  1262. gearman_worker_set_context(&worker, NULL);
  1263. return TEST_SUCCESS;
  1264. }
  1265. static test_return_t gearman_worker_check_options_GEARMAN_WORKER_GRAB_UNIQ(void *)
  1266. {
  1267. libgearman::Worker worker;
  1268. ASSERT_TRUE(worker->impl()->options.grab_uniq);
  1269. return TEST_SUCCESS;
  1270. }
  1271. static test_return_t gearman_worker_remove_options_GEARMAN_WORKER_GRAB_UNIQ(void *)
  1272. {
  1273. libgearman::Worker worker(libtest::default_port());
  1274. char function_name[GEARMAN_FUNCTION_MAX_SIZE];
  1275. snprintf(function_name, GEARMAN_FUNCTION_MAX_SIZE, "_%s%d", __func__, int(random()));
  1276. char unique_name[GEARMAN_MAX_UNIQUE_SIZE];
  1277. snprintf(unique_name, GEARMAN_MAX_UNIQUE_SIZE, "_%s%d", __func__, int(random()));
  1278. ASSERT_EQ(GEARMAN_SUCCESS,
  1279. gearman_worker_add_function(&worker, function_name, 0, no_unique_worker, NULL));
  1280. {
  1281. libgearman::Client client(libtest::default_port());
  1282. ASSERT_EQ(GEARMAN_SUCCESS,
  1283. gearman_client_do_background(&client, function_name, unique_name, test_string_make_from_array(unique_name), NULL));
  1284. }
  1285. gearman_worker_remove_options(&worker, GEARMAN_WORKER_GRAB_UNIQ);
  1286. ASSERT_FALSE(worker->impl()->options.grab_uniq);
  1287. gearman_worker_set_timeout(&worker, 800);
  1288. gearman_return_t rc;
  1289. gearman_job_st *job= gearman_worker_grab_job(&worker, NULL, &rc);
  1290. ASSERT_EQ(rc, GEARMAN_SUCCESS);
  1291. ASSERT_TRUE(job);
  1292. size_t size= 0;
  1293. void *result= no_unique_worker(job, NULL, &size, &rc);
  1294. ASSERT_EQ(rc, GEARMAN_SUCCESS);
  1295. test_false(result);
  1296. test_false(size);
  1297. return TEST_SUCCESS;
  1298. }
  1299. static test_return_t gearman_worker_add_options_GEARMAN_WORKER_GRAB_UNIQ(void *)
  1300. {
  1301. char function_name[GEARMAN_FUNCTION_MAX_SIZE];
  1302. snprintf(function_name, GEARMAN_FUNCTION_MAX_SIZE, "_%s%d", __func__, int(random()));
  1303. char unique_name[GEARMAN_MAX_UNIQUE_SIZE];
  1304. snprintf(unique_name, GEARMAN_MAX_UNIQUE_SIZE, "_%s%d", __func__, int(random()));
  1305. {
  1306. libgearman::Client client(libtest::default_port());
  1307. ASSERT_EQ(gearman_client_do_background(&client, function_name, unique_name,
  1308. test_string_make_from_array(unique_name), NULL),
  1309. GEARMAN_SUCCESS);
  1310. }
  1311. libgearman::Worker worker(libtest::default_port());
  1312. ASSERT_EQ(GEARMAN_SUCCESS,
  1313. gearman_worker_add_function(&worker, function_name, 0, check_unique_worker, NULL));
  1314. gearman_worker_add_options(&worker, GEARMAN_WORKER_GRAB_UNIQ);
  1315. ASSERT_TRUE(worker->impl()->options.grab_uniq);
  1316. gearman_return_t rc;
  1317. gearman_job_st *job= gearman_worker_grab_job(&worker, NULL, &rc);
  1318. ASSERT_EQ(GEARMAN_SUCCESS, rc);
  1319. ASSERT_TRUE(job);
  1320. size_t size= 0;
  1321. void *result= check_unique_worker(job, NULL, &size, &rc);
  1322. ASSERT_EQ(GEARMAN_SUCCESS, rc);
  1323. ASSERT_TRUE(result);
  1324. ASSERT_TRUE(size);
  1325. free(result);
  1326. return TEST_SUCCESS;
  1327. }
  1328. static test_return_t gearman_worker_set_identifier_TEST(void *)
  1329. {
  1330. libgearman::Worker worker(libtest::default_port());
  1331. ASSERT_EQ(GEARMAN_SUCCESS, gearman_worker_add_server(&worker, NULL, second_port));
  1332. ASSERT_EQ(GEARMAN_SUCCESS,
  1333. gearman_worker_set_identifier(&worker, test_literal_param(__func__)));
  1334. return TEST_SUCCESS;
  1335. }
  1336. static test_return_t gearman_worker_add_options_GEARMAN_WORKER_GRAB_UNIQ_worker_work(void *)
  1337. {
  1338. libgearman::Worker worker(libtest::default_port());
  1339. ASSERT_EQ(GEARMAN_SUCCESS, gearman_worker_add_server(&worker, NULL, second_port));
  1340. char function_name[GEARMAN_FUNCTION_MAX_SIZE];
  1341. snprintf(function_name, GEARMAN_FUNCTION_MAX_SIZE, "_%s%d", __func__, int(random()));
  1342. char unique_name[GEARMAN_MAX_UNIQUE_SIZE];
  1343. int n = snprintf(unique_name, sizeof unique_name, "_%s%06d",
  1344. std::string(__func__).substr(0, GEARMAN_MAX_UNIQUE_SIZE-7).c_str(),
  1345. int(random()) % 100000u);
  1346. ASSERT_TRUE(n >= 0);
  1347. ASSERT_TRUE((size_t)n <= sizeof unique_name);
  1348. bool success= false;
  1349. ASSERT_EQ(GEARMAN_SUCCESS,
  1350. gearman_worker_add_function(&worker, function_name, 0, check_unique_worker, &success));
  1351. {
  1352. libgearman::Client client(libtest::default_port());
  1353. ASSERT_EQ(gearman_client_do_background(&client, function_name, unique_name,
  1354. test_string_make_from_array(unique_name), NULL),
  1355. GEARMAN_SUCCESS);
  1356. }
  1357. ASSERT_TRUE(worker->impl()->options.grab_uniq);
  1358. gearman_worker_add_options(&worker, GEARMAN_WORKER_GRAB_UNIQ);
  1359. ASSERT_TRUE(worker->impl()->options.grab_uniq);
  1360. gearman_worker_set_timeout(&worker, 400);
  1361. ASSERT_EQ(gearman_worker_work(&worker), GEARMAN_SUCCESS);
  1362. ASSERT_TRUE(success);
  1363. return TEST_SUCCESS;
  1364. }
  1365. static test_return_t _increase_TEST(gearman_function_t &func, gearman_client_options_t options, size_t block_size)
  1366. {
  1367. libgearman::Client client(libtest::default_port());
  1368. ASSERT_EQ(GEARMAN_SUCCESS, gearman_client_echo(&client, test_literal_param(__func__)));
  1369. gearman_client_add_options(&client, options);
  1370. std::unique_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
  1371. NULL,
  1372. __func__,
  1373. func,
  1374. NULL,
  1375. gearman_worker_options_t(),
  1376. 0)); // timeout
  1377. size_t max_block_size= 4;
  1378. if (libtest::is_massive())
  1379. {
  1380. max_block_size= 24;
  1381. }
  1382. libtest::vchar_t workload;
  1383. libtest::vchar::make(workload, block_size);
  1384. for (size_t x= 1; x < max_block_size; ++x)
  1385. {
  1386. if (valgrind_is_caller() and (x * block_size) > 15728640)
  1387. {
  1388. continue;
  1389. }
  1390. workload.resize(x * block_size);
  1391. gearman_argument_t value= gearman_argument_make(0, 0, vchar_param(workload));
  1392. gearman_task_st *task= gearman_execute(&client,
  1393. test_literal_param(__func__),
  1394. NULL, 0, // unique
  1395. NULL, // gearman_task_attr_t
  1396. &value, // gearman_argument_t
  1397. NULL); // context
  1398. ASSERT_TRUE(task);
  1399. gearman_return_t rc;
  1400. do {
  1401. rc= gearman_client_run_tasks(&client);
  1402. if (options)
  1403. {
  1404. gearman_client_wait(&client);
  1405. }
  1406. } while (gearman_continue(rc));
  1407. ASSERT_EQ(GEARMAN_SUCCESS,
  1408. gearman_task_return(task));
  1409. gearman_result_st *result= gearman_task_result(task);
  1410. ASSERT_TRUE(result);
  1411. ASSERT_EQ(gearman_result_size(result), workload.size());
  1412. }
  1413. return TEST_SUCCESS;
  1414. }
  1415. static test_return_t gearman_client_run_tasks_increase_TEST(void*)
  1416. {
  1417. gearman_function_t func= gearman_function_create(echo_or_react_worker_v2);
  1418. return _increase_TEST(func, gearman_client_options_t(), 1024 * 1024);
  1419. }
  1420. static test_return_t gearman_client_run_tasks_increase_GEARMAN_CLIENT_NON_BLOCKING_TEST(void*)
  1421. {
  1422. gearman_function_t func= gearman_function_create(echo_or_react_worker_v2);
  1423. return _increase_TEST(func, GEARMAN_CLIENT_NON_BLOCKING, 1024 * 1024);
  1424. }
  1425. static test_return_t gearman_client_run_tasks_increase_chunk_TEST(void*)
  1426. {
  1427. gearman_function_t func= gearman_function_create(echo_or_react_chunk_worker_v2);
  1428. return _increase_TEST(func, gearman_client_options_t(), 1024);
  1429. }
  1430. static test_return_t gearman_worker_failover_test(void *)
  1431. {
  1432. libgearman::Worker worker(libtest::default_port());
  1433. // Now add a port which we do not have a server running on
  1434. ASSERT_EQ(GEARMAN_SUCCESS, gearman_worker_add_server(&worker, NULL, libtest::default_port() +1));
  1435. char function_name[GEARMAN_FUNCTION_MAX_SIZE];
  1436. snprintf(function_name, GEARMAN_FUNCTION_MAX_SIZE, "_%s%d", __func__, int(random()));
  1437. ASSERT_EQ(GEARMAN_SUCCESS,
  1438. gearman_worker_add_function(&worker, function_name, 0, fail_worker, NULL));
  1439. gearman_worker_set_timeout(&worker, 400);
  1440. ASSERT_EQ(GEARMAN_TIMEOUT, gearman_worker_work(&worker));
  1441. /* Make sure we have remove worker function */
  1442. ASSERT_EQ(GEARMAN_SUCCESS,
  1443. gearman_worker_unregister(&worker, function_name));
  1444. return TEST_SUCCESS;
  1445. }
  1446. static test_return_t gearman_worker_set_timeout_FAILOVER_TEST(void *)
  1447. {
  1448. test_skip_valgrind(); // lp:961904
  1449. in_port_t known_server_port= libtest::default_port();
  1450. libgearman::Worker worker(known_server_port);
  1451. ASSERT_EQ(GEARMAN_SUCCESS, gearman_worker_add_server(&worker, NULL, known_server_port));
  1452. char function_name[GEARMAN_FUNCTION_MAX_SIZE];
  1453. snprintf(function_name, GEARMAN_FUNCTION_MAX_SIZE, "_%s%d", __func__, int(random()));
  1454. ASSERT_EQ(GEARMAN_SUCCESS,
  1455. gearman_worker_add_function(&worker, function_name, 0, fail_worker, NULL));
  1456. gearman_worker_set_timeout(&worker, 2);
  1457. ASSERT_EQ(GEARMAN_TIMEOUT, gearman_worker_work(&worker));
  1458. /* Make sure we have remove worker function */
  1459. ASSERT_EQ(GEARMAN_SUCCESS,
  1460. gearman_worker_unregister(&worker, function_name));
  1461. return TEST_SUCCESS;
  1462. }
  1463. static test_return_t worker_connect_too_multiple_server_TEST(void *)
  1464. {
  1465. #if 0
  1466. libgearman::Worker worker(libtest::default_port());
  1467. // Now add a port which we do not have a server running on
  1468. ASSERT_EQ(GEARMAN_SUCCESS, gearman_worker_add_server(&worker, NULL, second_port));
  1469. gearman_function_t worker_fn= gearman_function_create(echo_specific_worker);
  1470. ASSERT_EQ(GEARMAN_SUCCESS, gearman_worker_define_function(&worker,
  1471. __func__, strlen(__func__),
  1472. worker_fn,
  1473. 0,
  1474. NULL));
  1475. std::unique_ptr<worker_handle_st> handle(worker_run(worker));
  1476. libgearman::Client client_one(libtest::default_port());
  1477. libgearman::Client client_two(libtest::default_port());
  1478. {
  1479. size_t result_size;
  1480. gearman_return_t rc;
  1481. void* result= gearman_client_do(&client_one,
  1482. __func__,
  1483. NULL,
  1484. NULL, 0, // workload, workload_size
  1485. &result_size,
  1486. &rc);
  1487. ASSERT_TRUE(result_size);
  1488. ASSERT_NOT_NULL(result);
  1489. }
  1490. {
  1491. size_t result_size;
  1492. gearman_return_t rc;
  1493. void* result= gearman_client_do(&client_two,
  1494. __func__,
  1495. NULL,
  1496. NULL, 0, // workload, workload_size
  1497. &result_size,
  1498. &rc);
  1499. ASSERT_TRUE(result_size);
  1500. ASSERT_NOT_NULL(result);
  1501. }
  1502. #endif
  1503. return TEST_SUCCESS;
  1504. }
  1505. /*********************** World functions **************************************/
  1506. static void *world_create(server_startup_st& servers, test_return_t&)
  1507. {
  1508. const char *argv[]= { "--job-retries=30", NULL };
  1509. ASSERT_TRUE(server_startup(servers, "gearmand", libtest::default_port(), argv));
  1510. second_port= libtest::get_free_port();
  1511. ASSERT_TRUE(server_startup(servers, "gearmand", second_port, argv));
  1512. return &servers;
  1513. }
  1514. test_st worker_TESTS[] ={
  1515. {"init", 0, init_test },
  1516. {"allocation", 0, allocation_test },
  1517. {"sanity", 0, sanity_TEST },
  1518. {"gearman_worker_timeout(1000)", 0, gearman_worker_timeout_TEST },
  1519. {"gearman_worker_clone(NULL, NULL)", 0, gearman_worker_clone_NULL_NULL },
  1520. {"gearman_worker_clone(NULL, source)", 0, gearman_worker_clone_NULL_SOURCE },
  1521. {"gearman_worker_add_server(GEARMAN_GETADDRINFO)", false, gearman_worker_add_server_GEARMAN_GETADDRINFO_TEST },
  1522. {"gearman_worker_add_server(GEARMAN_INVALID_ARGUMENT)", false, gearman_worker_add_server_GEARMAN_INVALID_ARGUMENT_TEST },
  1523. {"echo", 0, echo_test },
  1524. {"echo_multi", 0, echo_multi_test },
  1525. {"options", 0, option_test },
  1526. {"gearman_worker_add_function()", 0, gearman_worker_add_function_test },
  1527. {"gearman_worker_add_function() multi", 0, gearman_worker_add_function_multi_test },
  1528. {"gearman_worker_unregister_all()", 0, gearman_worker_unregister_all_test },
  1529. {"gearman_worker_work() with timout", 0, gearman_worker_work_with_TEST },
  1530. {"gearman_worker_work(GEARMAN_WORKER_NON_BLOCKING) with timout", 0, gearman_worker_work_with_GEARMAN_WORKER_NON_BLOCKING_TEST },
  1531. {"gearman_worker_context", 0, gearman_worker_context_test },
  1532. {"gearman_worker_failover", 0, gearman_worker_failover_test },
  1533. {"gearman_worker_check_options(GEARMAN_WORKER_GRAB_UNIQ)", 0, gearman_worker_check_options_GEARMAN_WORKER_GRAB_UNIQ },
  1534. {"gearman_worker_remove_options(GEARMAN_WORKER_GRAB_UNIQ)", 0, gearman_worker_remove_options_GEARMAN_WORKER_GRAB_UNIQ },
  1535. {"gearman_worker_add_options(GEARMAN_WORKER_GRAB_UNIQ)", 0, gearman_worker_add_options_GEARMAN_WORKER_GRAB_UNIQ },
  1536. {"gearman_worker_add_options(GEARMAN_WORKER_GRAB_UNIQ) worker_work()", 0, gearman_worker_add_options_GEARMAN_WORKER_GRAB_UNIQ_worker_work },
  1537. {"gearman_worker_set_timeout(2) with failover", 0, gearman_worker_set_timeout_FAILOVER_TEST },
  1538. {"gearman_return_t worker return coverage", 0, error_return_TEST },
  1539. {"gearman_return_t GEARMAN_FAIL worker coverage", 0, GEARMAN_FAIL_return_TEST },
  1540. {"gearman_return_t GEARMAN_ERROR worker coverage", 0, GEARMAN_ERROR_return_TEST },
  1541. {"gearman_return_t GEARMAN_ERROR worker always errors", 0, GEARMAN_ERROR_always_return_TEST },
  1542. {"gearman_return_t GEARMAN_ERROR worker always errors check retry", 0, GEARMAN_ERROR_check_retry_TEST },
  1543. {"gearman_client_run_tasks()", 0, gearman_client_run_tasks_increase_TEST },
  1544. {"gearman_client_run_tasks() GEARMAN_CLIENT_NON_BLOCKING", 0, gearman_client_run_tasks_increase_GEARMAN_CLIENT_NON_BLOCKING_TEST },
  1545. {"gearman_client_run_tasks() chunked", 0, gearman_client_run_tasks_increase_chunk_TEST },
  1546. {"gearman_client_job_status(is_known)", 0, gearman_client_job_status_is_known_TEST },
  1547. {"gearman_job_send_exception()", 0, gearman_job_send_exception_TEST },
  1548. {"gearman_job_send_exception(mass)", 0, gearman_job_send_exception_mass_TEST },
  1549. {"gearman_job_client()", 0, gearman_job_client_TEST },
  1550. {"job order", 0, job_order_TEST },
  1551. {"job background order", 0, job_order_background_TEST },
  1552. {"check worker's connection to multiple servers", 0, worker_connect_too_multiple_server_TEST },
  1553. {"echo_max", 0, echo_max_test },
  1554. {"abandoned_worker", 0, abandoned_worker_test },
  1555. {0, 0, 0}
  1556. };
  1557. test_st worker_defaults[] ={
  1558. {"gearman_worker_timeout()", 0, gearman_worker_timeout_default_test },
  1559. {0, 0, 0}
  1560. };
  1561. test_st gearman_worker_grab_job_TESTS[] ={
  1562. {"gearman_worker_grab_job(NO_JOBS)", 0, gearman_worker_grab_job_TEST },
  1563. {"gearman_worker_grab_job(GEARMAN_COULD_NOT_CONNECT)", 0, gearman_worker_grab_job_GEARMAN_COULD_NOT_CONNECT_TEST },
  1564. {"gearman_worker_grab_job(GEARMAN_NO_SERVERS)", 0, gearman_worker_grab_job_GEARMAN_NO_SERVERS_TEST },
  1565. {"gearman_worker_grab_job(GEARMAN_NO_REGISTERED_FUNCTIONS)", 0, gearman_worker_grab_job_GEARMAN_NO_REGISTERED_FUNCTIONS_TEST },
  1566. {"gearman_worker_grab_job(GEARMAN_NO_SERVERS + GEARMAN_NO_REGISTERED_FUNCTIONS)", 0, gearman_worker_grab_job_GEARMAN_NO_SERVERS_NO_FUNCTIONS_TEST },
  1567. {"gearman_worker_grab_job()", 0, gearman_worker_grab_job_GEARMAN_NO_SERVERS_NO_FUNCTIONS_TEST },
  1568. {0, 0, 0}
  1569. };
  1570. test_st gearman_worker_register_TESTS[] ={
  1571. {"gearman_worker_register()", 0, gearman_worker_register_TEST },
  1572. {"gearman_worker_register(GEARMAN_COULD_NOT_CONNECT)", 0, gearman_worker_register_GEARMAN_COULD_NOT_CONNECT_TEST },
  1573. {0, 0, 0}
  1574. };
  1575. test_st gearman_worker_st_NULL_invocation_TESTS[] ={
  1576. {"gearman_worker_free()", 0, gearman_worker_free_TEST },
  1577. {"gearman_worker_error()", 0, gearman_worker_error_TEST },
  1578. {"gearman_worker_error() no error", 0, gearman_worker_error_no_error_TEST },
  1579. {"gearman_worker_errno()", 0, gearman_worker_errno_TEST },
  1580. {"gearman_worker_errno() no error", 0, gearman_worker_errno_no_error_TEST },
  1581. {"gearman_worker_options()", 0, gearman_worker_options_TEST },
  1582. {"gearman_worker_set_log_fn()", 0, gearman_worker_set_log_fn_TEST },
  1583. {0, 0, 0}
  1584. };
  1585. test_st gearman_worker_set_identifier_TESTS[] ={
  1586. {"gearman_worker_set_identifier()", 0, gearman_worker_set_identifier_TEST },
  1587. {0, 0, 0}
  1588. };
  1589. collection_st collection[] ={
  1590. {"worker", 0, 0, worker_TESTS},
  1591. {"worker defaults", 0, 0, worker_defaults},
  1592. {"null gearman_worker_st invocation", 0, 0, gearman_worker_st_NULL_invocation_TESTS },
  1593. {"gearman_worker_grab_job()", 0, 0, gearman_worker_grab_job_TESTS },
  1594. {"gearman_worker_register()", 0, 0, gearman_worker_register_TESTS },
  1595. {"gearman_worker_set_identifier()", 0, 0, gearman_worker_set_identifier_TESTS},
  1596. {0, 0, 0, 0}
  1597. };
  1598. void get_world(libtest::Framework *world)
  1599. {
  1600. world->collections(collection);
  1601. world->create(world_create);
  1602. }