burnin.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2010-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 <cerrno>
  43. #include <cstdio>
  44. #include <cstdlib>
  45. #include <cstring>
  46. #include <libgearman-1.0/gearman.h>
  47. #include <libtest/test.hpp>
  48. #include "libgearman/interface/task.hpp"
  49. #include "libgearman/client.hpp"
  50. using namespace org::gearmand;
  51. #include <tests/start_worker.h>
  52. #define DEFAULT_WORKER_NAME "burnin"
  53. #define HARD_CODED_EXCEPTION "my test exception"
  54. static gearman_return_t worker_fn(gearman_job_st* job, void*)
  55. {
  56. if (random() % 10)
  57. {
  58. if (random() % 3)
  59. {
  60. gearman_return_t ret;
  61. if (gearman_failed(ret= gearman_job_send_exception(job, test_literal_param(HARD_CODED_EXCEPTION))))
  62. {
  63. Error << "gearman_job_send_exception(" << gearman_strerror(ret) << ")";
  64. return GEARMAN_WORK_ERROR;
  65. }
  66. else
  67. {
  68. return ret;
  69. }
  70. }
  71. // We will pass back wrong responses from time to time.
  72. if (random() % 3)
  73. {
  74. return GEARMAN_WORK_FAIL;
  75. }
  76. }
  77. return GEARMAN_SUCCESS;
  78. }
  79. struct client_test_st {
  80. libgearman::Client _client;
  81. worker_handle_st *handle;
  82. client_test_st():
  83. _client(libtest::default_port()),
  84. handle(NULL)
  85. {
  86. gearman_function_t func_arg= gearman_function_create(worker_fn);
  87. handle= test_worker_start(libtest::default_port(), NULL, DEFAULT_WORKER_NAME, func_arg, NULL, gearman_worker_options_t());
  88. ASSERT_TRUE(handle->check());
  89. }
  90. ~client_test_st()
  91. {
  92. delete handle;
  93. }
  94. gearman_client_st* client()
  95. {
  96. return &_client;
  97. }
  98. };
  99. struct client_context_st {
  100. int latch;
  101. const size_t min_size;
  102. const size_t max_size;
  103. private:
  104. size_t _num_tasks;
  105. size_t _count;
  106. public:
  107. char *blob;
  108. client_context_st():
  109. latch(0),
  110. min_size(1024),
  111. max_size(1024 *2),
  112. _num_tasks(1),
  113. _count(20),
  114. blob(NULL)
  115. {
  116. blob= (char *)malloc(max_size);
  117. ASSERT_TRUE(blob);
  118. memset(blob, 'x', max_size);
  119. }
  120. void next()
  121. {
  122. _num_tasks= _num_tasks *2;
  123. _count= _count *2;
  124. }
  125. size_t count()
  126. {
  127. return _count;
  128. }
  129. size_t num_tasks() const
  130. {
  131. return _num_tasks;
  132. }
  133. ~client_context_st()
  134. {
  135. if (blob)
  136. {
  137. free(blob);
  138. }
  139. }
  140. };
  141. #ifndef __INTEL_COMPILER
  142. #pragma GCC diagnostic ignored "-Wold-style-cast"
  143. #endif
  144. static client_test_st *test_client_context= NULL;
  145. static test_return_t echo_TEST(void*)
  146. {
  147. gearman_client_st *client= test_client_context->client();
  148. fatal_assert(client);
  149. gearman_string_t value= { test_literal_param("This is my echo test") };
  150. if (GEARMAN_SUCCESS != gearman_client_echo(client, gearman_string_param(value)))
  151. {
  152. Error << gearman_client_error(client);
  153. }
  154. ASSERT_EQ(GEARMAN_SUCCESS, gearman_client_echo(client, gearman_string_param(value)));
  155. return TEST_SUCCESS;
  156. }
  157. static test_return_t burnin_TEST(void*)
  158. {
  159. gearman_client_st *client= test_client_context->client();
  160. fatal_assert(client);
  161. client_context_st *context= (client_context_st *)gearman_client_context(client);
  162. fatal_assert(context);
  163. // This sketchy, don't do this in your own code.
  164. ASSERT_TRUE(context->num_tasks() > 0);
  165. std::vector<gearman_task_st> tasks;
  166. try {
  167. tasks.resize(context->num_tasks());
  168. }
  169. catch (...)
  170. { }
  171. ASSERT_EQ(tasks.size(), context->num_tasks());
  172. ASSERT_EQ(gearman_client_echo(client, test_literal_param("echo_test")), GEARMAN_SUCCESS);
  173. size_t count= context->count();
  174. do
  175. {
  176. for (uint32_t x= 0; x < context->num_tasks(); x++)
  177. {
  178. size_t blob_size= 0;
  179. if (context->min_size == context->max_size)
  180. {
  181. blob_size= context->max_size;
  182. }
  183. else
  184. {
  185. blob_size= (size_t)rand();
  186. if (context->max_size > RAND_MAX)
  187. {
  188. blob_size*= (size_t)(rand() + 1);
  189. }
  190. blob_size= (blob_size % (context->max_size - context->min_size)) + context->min_size;
  191. }
  192. gearman_task_st *task_ptr;
  193. gearman_return_t ret;
  194. if (context->latch)
  195. {
  196. task_ptr= gearman_client_add_task_background(client, &(tasks[x]),
  197. NULL, DEFAULT_WORKER_NAME, NULL,
  198. (void *)context->blob, blob_size, &ret);
  199. }
  200. else
  201. {
  202. task_ptr= gearman_client_add_task(client, &(tasks[x]), NULL,
  203. DEFAULT_WORKER_NAME, NULL, (void *)context->blob, blob_size,
  204. &ret);
  205. }
  206. ASSERT_EQ(ret, GEARMAN_SUCCESS);
  207. ASSERT_TRUE(task_ptr);
  208. }
  209. gearman_return_t ret= gearman_client_run_tasks(client);
  210. do {
  211. for (uint32_t x= 0; x < context->num_tasks(); x++)
  212. {
  213. ASSERT_EQ(GEARMAN_TASK_STATE_FINISHED, tasks[x].impl()->state);
  214. if (tasks[x].impl()->error_code() == GEARMAN_UNKNOWN_STATE)
  215. {
  216. gearman_client_wait(client);
  217. }
  218. else if (tasks[x].impl()->error_code() == GEARMAN_WORK_FAIL)
  219. {
  220. if (gearman_task_has_exception(&tasks[x]))
  221. {
  222. ASSERT_TRUE(gearman_task_has_exception(&tasks[x]));
  223. gearman_string_t exception_string= gearman_task_exception(&tasks[x]);
  224. test_strcmp(HARD_CODED_EXCEPTION, gearman_c_str(exception_string));
  225. }
  226. else
  227. {
  228. #if 0
  229. Error << "error was " << gearman_task_error(&tasks[x]);
  230. #endif
  231. }
  232. }
  233. else if (tasks[x].impl()->error_code() != GEARMAN_SUCCESS)
  234. {
  235. ASSERT_EQ(GEARMAN_SUCCESS, tasks[x].impl()->error_code());
  236. }
  237. else
  238. {
  239. ASSERT_EQ(GEARMAN_SUCCESS, tasks[x].impl()->error_code());
  240. }
  241. }
  242. } while (client->impl()->new_tasks);
  243. test_zero(client->impl()->new_tasks);
  244. ASSERT_EQ(ret, GEARMAN_SUCCESS);
  245. for (uint32_t x= 0; x < context->num_tasks(); x++)
  246. {
  247. gearman_task_free(&(tasks[x]));
  248. }
  249. } while (--count);
  250. context->latch++;
  251. return TEST_SUCCESS;
  252. }
  253. static test_return_t burnin_setup(void* obj)
  254. {
  255. client_context_st* context= (client_context_st *)obj;
  256. context->next();
  257. test_client_context= new client_test_st;
  258. gearman_client_set_context(test_client_context->client(), context);
  259. return TEST_SUCCESS;
  260. }
  261. static test_return_t burnin_cleanup(void*)
  262. {
  263. delete test_client_context;
  264. test_client_context= NULL;
  265. return TEST_SUCCESS;
  266. }
  267. /*********************** World functions **************************************/
  268. static void *world_create(server_startup_st& servers, test_return_t&)
  269. {
  270. ASSERT_TRUE(server_startup(servers, "gearmand", libtest::default_port(), NULL));
  271. client_context_st *context= new client_context_st;
  272. return context;
  273. }
  274. static bool world_destroy(void *object)
  275. {
  276. client_context_st* context= (client_context_st*)object;
  277. delete context;
  278. return TEST_SUCCESS;
  279. }
  280. test_st burnin_TESTS[] ={
  281. {"burnin", 0, burnin_TEST },
  282. {"burnin", 0, burnin_TEST },
  283. {0, 0, 0}
  284. };
  285. test_st echo_TESTS[] ={
  286. {"echo", 0, echo_TEST },
  287. {0, 0, 0}
  288. };
  289. collection_st collection[] ={
  290. {"echo", burnin_setup, burnin_cleanup, echo_TESTS },
  291. {"burnin", burnin_setup, burnin_cleanup, burnin_TESTS },
  292. {0, 0, 0, 0}
  293. };
  294. void get_world(libtest::Framework *world)
  295. {
  296. world->collections(collection);
  297. world->create(world_create);
  298. world->destroy(world_destroy);
  299. }