multi_client_test.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2013 Keyur Govande
  6. * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  7. * Copyright (C) 2008 Brian Aker, Eric Day
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are
  12. * met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following disclaimer
  19. * in the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * The names of its contributors may not be used to endorse or
  23. * promote products derived from this software without specific prior
  24. * written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. */
  39. #include "gear_config.h"
  40. #include <libtest/test.hpp>
  41. using namespace libtest;
  42. #include <cassert>
  43. #include <cstdio>
  44. #include <cstdlib>
  45. #include <cstring>
  46. #include <ctime>
  47. #include <iostream>
  48. #include <vector>
  49. #define GEARMAN_CORE
  50. #include <libgearman-1.0/gearman.h>
  51. #ifndef __INTEL_COMPILER
  52. #pragma GCC diagnostic ignored "-Wold-style-cast"
  53. #endif
  54. #include "tests/runner.h"
  55. #ifndef __INTEL_COMPILER
  56. #pragma GCC diagnostic ignored "-Wold-style-cast"
  57. #endif
  58. #include "tests/libgearman-1.0/multi_client_test.h"
  59. #define SECOND_SERVER_PORT_OFFSET 2
  60. static const char *server_argv[]= { "--exceptions", 0 };
  61. static test_return_t multi_client_test(void *object)
  62. {
  63. multi_client_test_st *test_client= (multi_client_test_st*)object;
  64. ASSERT_TRUE(test_client);
  65. server_startup_st& server_container= test_client->server_container();
  66. // make a client that connects to both servers.
  67. gearman_client_st *client_to_both= test_client->connected_to_both_client();
  68. ASSERT_TRUE(client_to_both);
  69. (void)gearman_client_set_context(client_to_both, const_cast<char *>("nothing"));
  70. gearman_string_t value= { test_literal_param("background_test") };
  71. const char *worker_function= (const char *)gearman_client_context(client_to_both);
  72. ASSERT_TRUE(worker_function);
  73. // queue up first job
  74. const char* unique_1= "unique_1";
  75. gearman_job_handle_t job_handle;
  76. test_compare(GEARMAN_SUCCESS,
  77. gearman_client_do_background(client_to_both, worker_function, unique_1, gearman_string_param(value), job_handle));
  78. {
  79. gearman_return_t ret;
  80. bool is_known;
  81. test_compare(GEARMAN_SUCCESS,
  82. (ret= gearman_client_job_status(client_to_both, job_handle, &is_known, NULL, NULL, NULL)));
  83. ASSERT_TRUE(is_known);
  84. }
  85. // all OK. now shut down the gearmand server 1
  86. in_port_t gearmand_port_1= test_client->port(0);
  87. in_port_t gearmand_port_2= test_client->port(1);
  88. ASSERT_TRUE(server_container.shutdown(0));
  89. // Try to queue up a job again
  90. const char* unique_2= "unique_2";
  91. test_compare(GEARMAN_LOST_CONNECTION,
  92. gearman_client_do_background(client_to_both, worker_function, unique_2, gearman_string_param(value), job_handle));
  93. const char* unique_3= "unique_3";
  94. test_compare(GEARMAN_SUCCESS,
  95. gearman_client_do_background(client_to_both, worker_function, unique_3, gearman_string_param(value), job_handle));
  96. // The job unique_2 is truly lost, but unique_3 will end up at the gearmand server 2
  97. gearman_client_st *client_to_1= test_client->connected_to_1_client();
  98. gearman_client_st *client_to_2= test_client->connected_to_2_client();
  99. // First confirming that cannot connect to server 1
  100. {
  101. gearman_status_t unique_3_status= gearman_client_unique_status(client_to_1, unique_3, strlen(unique_3));
  102. test_compare(GEARMAN_COULD_NOT_CONNECT,
  103. gearman_status_return(unique_3_status));
  104. }
  105. // Next, proving that the job is present on server 2.
  106. {
  107. gearman_status_t unique_3_status= gearman_client_unique_status(client_to_2, unique_3, strlen(unique_3));
  108. test_compare(GEARMAN_SUCCESS,
  109. gearman_status_return(unique_3_status));
  110. ASSERT_TRUE(gearman_status_is_known(unique_3_status));
  111. }
  112. // Bring server 1 back up
  113. server_container.shutdown();
  114. ASSERT_TRUE(server_startup(server_container, "gearmand", gearmand_port_1, server_argv));
  115. ASSERT_TRUE(server_startup(server_container, "gearmand", gearmand_port_2, server_argv));
  116. // Try adding in a new job.
  117. const char* unique_4= "unique_4";
  118. test_compare(GEARMAN_SUCCESS,
  119. gearman_client_do_background(client_to_both, worker_function, unique_4, gearman_string_param(value), job_handle));
  120. // Next, proving that the job is present on server 1, but we got a timeout instead!
  121. {
  122. gearman_status_t unique_4_status= gearman_client_unique_status(client_to_both, unique_4, strlen(unique_4));
  123. test_compare(GEARMAN_SUCCESS,
  124. gearman_status_return(unique_4_status));
  125. ASSERT_TRUE(gearman_status_is_known(unique_4_status));
  126. }
  127. return TEST_SUCCESS;
  128. }
  129. static void *world_create(server_startup_st& servers, test_return_t&)
  130. {
  131. multi_client_test_st *test= new multi_client_test_st(servers, 1000); // setting a default timeout
  132. ASSERT_TRUE(test);
  133. test->push_port(libtest::get_free_port());
  134. test->push_port(libtest::get_free_port());
  135. ASSERT_TRUE(server_startup(servers, "gearmand", test->port(0), server_argv));
  136. ASSERT_TRUE(server_startup(servers, "gearmand", test->port(1), server_argv));
  137. test->add_server("127.0.0.1", test->port(0),
  138. "127.0.0.1", test->port(1));
  139. return (void *)test;
  140. }
  141. static bool world_destroy(void *object)
  142. {
  143. multi_client_test_st *test= (multi_client_test_st *)object;
  144. delete test;
  145. return TEST_SUCCESS;
  146. }
  147. test_st multi_client_TESTS[] ={
  148. {"multi_client_test", 0, multi_client_test },
  149. {0, 0, 0}
  150. };
  151. collection_st collection[] ={
  152. {"multi_client", 0, 0, multi_client_TESTS},
  153. {0, 0, 0, 0}
  154. };
  155. void get_world(libtest::Framework *world)
  156. {
  157. world->collections(collection);
  158. world->create(world_create);
  159. world->destroy(world_destroy);
  160. }