burnin.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. static gearman_return_t worker_fn(gearman_job_st*, void*)
  54. {
  55. return GEARMAN_SUCCESS;
  56. }
  57. struct client_test_st {
  58. libgearman::Client _client;
  59. worker_handle_st *handle;
  60. client_test_st():
  61. _client(libtest::default_port()),
  62. handle(NULL)
  63. {
  64. gearman_function_t func_arg= gearman_function_create(worker_fn);
  65. handle= test_worker_start(libtest::default_port(), NULL, DEFAULT_WORKER_NAME, func_arg, NULL, gearman_worker_options_t());
  66. }
  67. ~client_test_st()
  68. {
  69. delete handle;
  70. }
  71. gearman_client_st* client()
  72. {
  73. return &_client;
  74. }
  75. };
  76. struct client_context_st {
  77. int latch;
  78. size_t min_size;
  79. size_t max_size;
  80. size_t num_tasks;
  81. size_t count;
  82. char *blob;
  83. client_context_st():
  84. latch(0),
  85. min_size(1024),
  86. max_size(1024 *2),
  87. num_tasks(20),
  88. count(2000),
  89. blob(NULL)
  90. { }
  91. ~client_context_st()
  92. {
  93. if (blob)
  94. {
  95. free(blob);
  96. }
  97. }
  98. };
  99. #ifndef __INTEL_COMPILER
  100. #pragma GCC diagnostic ignored "-Wold-style-cast"
  101. #endif
  102. static client_test_st *test_client_context= NULL;
  103. static test_return_t burnin_TEST(void*)
  104. {
  105. gearman_client_st *client= test_client_context->client();
  106. fatal_assert(client);
  107. client_context_st *context= (client_context_st *)gearman_client_context(client);
  108. fatal_assert(context);
  109. // This sketchy, don't do this in your own code.
  110. test_true(context->num_tasks > 0);
  111. std::vector<gearman_task_st> tasks;
  112. try {
  113. tasks.resize(context->num_tasks);
  114. }
  115. catch (...)
  116. { }
  117. ASSERT_EQ(tasks.size(), context->num_tasks);
  118. ASSERT_EQ(gearman_client_echo(client, test_literal_param("echo_test")), GEARMAN_SUCCESS);
  119. do
  120. {
  121. for (uint32_t x= 0; x < context->num_tasks; x++)
  122. {
  123. size_t blob_size= 0;
  124. if (context->min_size == context->max_size)
  125. {
  126. blob_size= context->max_size;
  127. }
  128. else
  129. {
  130. blob_size= (size_t)rand();
  131. if (context->max_size > RAND_MAX)
  132. {
  133. blob_size*= (size_t)(rand() + 1);
  134. }
  135. blob_size= (blob_size % (context->max_size - context->min_size)) + context->min_size;
  136. }
  137. gearman_task_st *task_ptr;
  138. gearman_return_t ret;
  139. if (context->latch)
  140. {
  141. task_ptr= gearman_client_add_task_background(client, &(tasks[x]),
  142. NULL, DEFAULT_WORKER_NAME, NULL,
  143. (void *)context->blob, blob_size, &ret);
  144. }
  145. else
  146. {
  147. task_ptr= gearman_client_add_task(client, &(tasks[x]), NULL,
  148. DEFAULT_WORKER_NAME, NULL, (void *)context->blob, blob_size,
  149. &ret);
  150. }
  151. ASSERT_EQ(ret, GEARMAN_SUCCESS);
  152. test_truth(task_ptr);
  153. }
  154. gearman_return_t ret= gearman_client_run_tasks(client);
  155. for (uint32_t x= 0; x < context->num_tasks; x++)
  156. {
  157. ASSERT_EQ(GEARMAN_TASK_STATE_FINISHED, tasks[x].impl()->state);
  158. ASSERT_EQ(GEARMAN_SUCCESS, tasks[x].impl()->result_rc);
  159. }
  160. test_zero(client->impl()->new_tasks);
  161. ASSERT_EQ(ret, GEARMAN_SUCCESS);
  162. for (uint32_t x= 0; x < context->num_tasks; x++)
  163. {
  164. gearman_task_free(&(tasks[x]));
  165. }
  166. } while (context->count--);
  167. context->latch++;
  168. return TEST_SUCCESS;
  169. }
  170. static test_return_t burnin_setup(void*)
  171. {
  172. test_client_context= new client_test_st;
  173. client_context_st *context= new client_context_st;
  174. context->blob= (char *)malloc(context->max_size);
  175. test_true(context->blob);
  176. memset(context->blob, 'x', context->max_size);
  177. gearman_client_set_context(test_client_context->client(), context);
  178. return TEST_SUCCESS;
  179. }
  180. static test_return_t burnin_cleanup(void*)
  181. {
  182. client_context_st *context= (struct client_context_st *)gearman_client_context(test_client_context->client());
  183. delete context;
  184. delete test_client_context;
  185. test_client_context= NULL;
  186. return TEST_SUCCESS;
  187. }
  188. /*********************** World functions **************************************/
  189. static void *world_create(server_startup_st& servers, test_return_t& error)
  190. {
  191. if (server_startup(servers, "gearmand", libtest::default_port(), NULL) == false)
  192. {
  193. error= TEST_SKIPPED;
  194. return NULL;
  195. }
  196. worker_handles_st *handle= new worker_handles_st;
  197. if (handle == NULL)
  198. {
  199. error= TEST_FAILURE;
  200. return NULL;
  201. }
  202. return handle;
  203. }
  204. static bool world_destroy(void *object)
  205. {
  206. worker_handles_st *handles= (worker_handles_st *)object;
  207. delete handles;
  208. return TEST_SUCCESS;
  209. }
  210. test_st burnin_TESTS[] ={
  211. {"burnin", 0, burnin_TEST },
  212. {0, 0, 0}
  213. };
  214. collection_st collection[] ={
  215. {"burnin", burnin_setup, burnin_cleanup, burnin_TESTS },
  216. {0, 0, 0, 0}
  217. };
  218. void get_world(libtest::Framework *world)
  219. {
  220. world->collections(collection);
  221. world->create(world_create);
  222. world->destroy(world_destroy);
  223. }