basic.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include "gear_config.h"
  38. #include <libtest/test.hpp>
  39. using namespace libtest;
  40. #include <cstring>
  41. #include <cassert>
  42. #include <libgearman/gearman.h>
  43. #include <tests/basic.h>
  44. #include <tests/context.h>
  45. #include <tests/start_worker.h>
  46. #include "libgearman/client.hpp"
  47. #include "libgearman/worker.hpp"
  48. using namespace org::gearmand;
  49. #include "tests/workers/v2/called.h"
  50. #include "libgearman/interface/client.hpp"
  51. #ifndef __INTEL_COMPILER
  52. #pragma GCC diagnostic ignored "-Wold-style-cast"
  53. #endif
  54. test_return_t client_echo_fail_test(void *object)
  55. {
  56. Context *test= (Context *)object;
  57. ASSERT_TRUE(test);
  58. libgearman::Client client(in_port_t(20));
  59. gearman_return_t rc= gearman_client_echo(&client, test_literal_param("This should never work"));
  60. ASSERT_TRUE(gearman_failed(rc));
  61. return TEST_SUCCESS;
  62. }
  63. test_return_t client_echo_test(void *object)
  64. {
  65. Context *test= (Context *)object;
  66. ASSERT_TRUE(test);
  67. libgearman::Client client(test->port());
  68. ASSERT_EQ(gearman_client_echo(&client, test_literal_param("This is my echo test")), GEARMAN_SUCCESS);
  69. return TEST_SUCCESS;
  70. }
  71. test_return_t worker_echo_test(void *object)
  72. {
  73. Context *test= (Context *)object;
  74. ASSERT_TRUE(test);
  75. #if 0
  76. gearman_worker_st *worker= test->worker;
  77. ASSERT_TRUE(worker);
  78. #endif
  79. libgearman::Worker worker(test->port());
  80. ASSERT_EQ(gearman_worker_echo(&worker, test_literal_param("This is my echo test")),
  81. GEARMAN_SUCCESS);
  82. return TEST_SUCCESS;
  83. }
  84. test_return_t queue_clean(void *object)
  85. {
  86. Context *test= (Context *)object;
  87. ASSERT_TRUE(test);
  88. gearman_worker_st *worker= test->worker();
  89. ASSERT_TRUE(worker);
  90. gearman_worker_set_timeout(worker, 3000);
  91. Called called;
  92. gearman_function_t counter_function= gearman_function_create(called_worker);
  93. ASSERT_EQ(gearman_worker_define_function(worker,
  94. test->worker_function_name(), strlen(test->worker_function_name()),
  95. counter_function,
  96. 5, &called), GEARMAN_SUCCESS);
  97. // Clean out any jobs that might still be in the queue from failed tests.
  98. while (GEARMAN_SUCCESS == gearman_worker_work(worker)) {};
  99. return TEST_SUCCESS;
  100. }
  101. test_return_t queue_add(void *object)
  102. {
  103. Context *test= (Context *)object;
  104. ASSERT_TRUE(test);
  105. test->run_worker= false;
  106. libgearman::Client client(test->port());
  107. {
  108. gearman_return_t rc= gearman_client_echo(&client, test_literal_param("echo test message"));
  109. ASSERT_EQ(rc, GEARMAN_SUCCESS);
  110. }
  111. {
  112. gearman_job_handle_t job_handle= {};
  113. gearman_return_t ret= gearman_client_do_background(&client, test->worker_function_name(), NULL,
  114. test->worker_function_name(), strlen(test->worker_function_name()),
  115. job_handle);
  116. ASSERT_EQ(ret, GEARMAN_SUCCESS);
  117. ASSERT_TRUE(job_handle[0]);
  118. gearman_return_t rc;
  119. do {
  120. rc= gearman_client_job_status(&client, job_handle, NULL, NULL, NULL, NULL);
  121. ASSERT_TRUE(rc != GEARMAN_IN_PROGRESS);
  122. } while (gearman_continue(rc) and rc != GEARMAN_JOB_EXISTS); // We need to exit on these values since the job will never run
  123. ASSERT_TRUE(rc == GEARMAN_JOB_EXISTS or rc == GEARMAN_SUCCESS);
  124. }
  125. test->run_worker= true;
  126. return TEST_SUCCESS;
  127. }
  128. test_return_t queue_worker(void *object)
  129. {
  130. Context *test= (Context *)object;
  131. ASSERT_TRUE(test);
  132. // Fetch worker
  133. libgearman::Worker worker(test->port());
  134. // Test worker
  135. ASSERT_EQ(gearman_worker_echo(&worker, test_literal_param("This is my echo test")), GEARMAN_SUCCESS);
  136. // Setup job
  137. ASSERT_EQ(TEST_SUCCESS, queue_add(object));
  138. ASSERT_TRUE(test->run_worker);
  139. Called counter;
  140. gearman_function_t counter_function= gearman_function_create(called_worker);
  141. ASSERT_EQ(gearman_worker_define_function(&worker,
  142. test->worker_function_name(), strlen(test->worker_function_name()),
  143. counter_function,
  144. 0, &counter), GEARMAN_SUCCESS);
  145. gearman_return_t rc= gearman_worker_work(&worker);
  146. ASSERT_EQ(rc, GEARMAN_SUCCESS);
  147. ASSERT_TRUE(counter.count());
  148. return TEST_SUCCESS;
  149. }
  150. #define NUMBER_OF_WORKERS 10
  151. #define NUMBER_OF_JOBS 40
  152. #define JOB_SIZE 100
  153. test_return_t lp_734663(void *object)
  154. {
  155. Context *test= (Context *)object;
  156. ASSERT_TRUE(test);
  157. const char *worker_function_name= "drizzle_queue_test";
  158. uint8_t value[JOB_SIZE]= { 'x' };
  159. memset(&value, 'x', sizeof(value));
  160. libgearman::Client client(in_port_t(test->port()));
  161. ASSERT_EQ(gearman_client_echo(&client, value, sizeof(value)), GEARMAN_SUCCESS);
  162. for (uint32_t x= 0; x < NUMBER_OF_JOBS; x++)
  163. {
  164. gearman_job_handle_t job_handle= {};
  165. ASSERT_EQ(gearman_client_do_background(&client, worker_function_name, NULL, value, sizeof(value), job_handle), GEARMAN_SUCCESS);
  166. ASSERT_TRUE(job_handle[0]);
  167. }
  168. struct worker_handle_st *worker_handle[NUMBER_OF_WORKERS];
  169. Called called;
  170. gearman_function_t counter_function_fn= gearman_function_create(called_worker);
  171. for (uint32_t x= 0; x < NUMBER_OF_WORKERS; x++)
  172. {
  173. worker_handle[x]= test_worker_start(test->port(), NULL, worker_function_name, counter_function_fn, &called, gearman_worker_options_t());
  174. ASSERT_TRUE(worker_handle[x]);
  175. }
  176. while (called.count() < NUMBER_OF_JOBS)
  177. {
  178. libtest::dream(0, static_cast<long>(gearman_timeout(&client) *1000));
  179. }
  180. for (uint32_t x= 0; x < NUMBER_OF_WORKERS; x++)
  181. {
  182. worker_handle[x]->shutdown();
  183. }
  184. for (uint32_t x= 0; x < NUMBER_OF_WORKERS; x++)
  185. {
  186. delete worker_handle[x];
  187. }
  188. return TEST_SUCCESS;
  189. }