protocol.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2013 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 <cstdio>
  41. #include <cstdlib>
  42. #include <cstring>
  43. #define GEARMAN_CORE
  44. #include "libgearman/common.h"
  45. #include "libgearman/packet.hpp"
  46. #include "libgearman/universal.hpp"
  47. #include "libgearman/protocol/echo.h"
  48. #include "libgearman/protocol/work_exception.h"
  49. #include "tests/regression.h"
  50. #include <typeinfo>
  51. #ifndef __INTEL_COMPILER
  52. #pragma GCC diagnostic ignored "-Wold-style-cast"
  53. #endif
  54. static void error_logger(const char* message, gearman_verbose_t, void*)
  55. {
  56. Error << message;
  57. }
  58. static test_return_t GEARMAN_COMMAND_ECHO_REQ_TEST(void *)
  59. {
  60. vchar_t data;
  61. vchar::make(data, 2048);
  62. gearman_universal_st universal;
  63. gearman_set_log_fn(universal, error_logger, NULL, GEARMAN_VERBOSE_ERROR);
  64. universal.ssl(libtest::is_ssl());
  65. gearman_packet_st message;
  66. gearman_string_t workload= { vchar_param(data) };
  67. ASSERT_EQ(GEARMAN_SUCCESS, libgearman::protocol::echo(universal, message, workload));
  68. gearman_connection_st *connection1;
  69. ASSERT_TRUE(connection1= gearman_connection_create(universal, GEARMAN_DEFAULT_TCP_HOST, libtest::default_port()));
  70. ASSERT_TRUE(connection1);
  71. ASSERT_EQ(GEARMAN_SUCCESS, connection1->send_packet(message, true));
  72. size_t length;
  73. gearman_return_t ret;
  74. gearman_packet_st recv_message;
  75. ASSERT_TRUE(connection1->receiving(recv_message, ret, true));
  76. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  77. ASSERT_TRUE(recv_message.value(length));
  78. ASSERT_EQ(length, data.size());
  79. delete connection1;
  80. gearman_packet_free(&recv_message);
  81. gearman_packet_free(&message);
  82. gearman_universal_free(universal);
  83. return TEST_SUCCESS;
  84. }
  85. #if 0
  86. static test_return_t GEARMAN_COMMAND_WORK_EXCEPTION_TEST(void *)
  87. {
  88. vchar_t handle;
  89. vchar::make(handle, GEARMAN_JOB_HANDLE_SIZE);
  90. handle.resize(GEARMAN_JOB_HANDLE_SIZE +1); // Add a null
  91. vchar_t exception;
  92. vchar::make(exception, 2048);
  93. gearman_universal_st universal;
  94. gearman_set_log_fn(universal, error_logger, NULL, GEARMAN_VERBOSE_ERROR);
  95. universal.ssl(libtest::is_ssl());
  96. gearman_packet_st message;
  97. gearman_string_t handle_string= { &handle[0], GEARMAN_JOB_HANDLE_SIZE };
  98. gearman_string_t exception_string= { vchar_param(exception) };
  99. Error << "handle: " << &handle[0] << " size: " << handle.size() << " size:" << exception.size();
  100. ASSERT_EQ(GEARMAN_SUCCESS, libgearman::protocol::work_exception(universal, message, handle_string, exception_string));
  101. gearman_connection_st *connection1;
  102. ASSERT_TRUE(connection1= gearman_connection_create(universal, GEARMAN_DEFAULT_TCP_HOST, libtest::default_port()));
  103. ASSERT_TRUE(connection1);
  104. ASSERT_EQ(GEARMAN_SUCCESS, connection1->send_packet(message, true));
  105. size_t length;
  106. gearman_return_t ret;
  107. gearman_packet_st recv_message;
  108. ASSERT_TRUE(connection1->receiving(recv_message, ret, true));
  109. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  110. ASSERT_TRUE(recv_message.value(length));
  111. ASSERT_EQ(length, exception.size());
  112. delete connection1;
  113. gearman_packet_free(&recv_message);
  114. gearman_packet_free(&message);
  115. gearman_universal_free(universal);
  116. return TEST_SUCCESS;
  117. }
  118. #endif
  119. static test_return_t GEARMAN_COMMAND_ECHO_REQ_overrun_TEST(void *)
  120. {
  121. vchar_t data;
  122. vchar::make(data, 2048);
  123. gearman_universal_st universal;
  124. gearman_set_log_fn(universal, error_logger, NULL, GEARMAN_VERBOSE_ERROR);
  125. universal.ssl(libtest::is_ssl());
  126. gearman_packet_st message;
  127. gearman_string_t workload= { vchar_param(data) };
  128. ASSERT_EQ(GEARMAN_SUCCESS, libgearman::protocol::echo(universal, message, workload));
  129. gearman_connection_st *connection1;
  130. ASSERT_TRUE(connection1= gearman_connection_create(universal, GEARMAN_DEFAULT_TCP_HOST, libtest::default_port()));
  131. ASSERT_TRUE(typeid(decltype(*connection1)).name()==typeid(gearman_connection_st).name());
  132. for (size_t x= 0; x < 1000; ++x)
  133. {
  134. ASSERT_EQ(GEARMAN_SUCCESS, connection1->send_packet(message, true));
  135. }
  136. size_t length;
  137. gearman_return_t ret;
  138. gearman_packet_st recv_message;
  139. ASSERT_TRUE(connection1->receiving(recv_message, ret, true));
  140. ASSERT_EQ(GEARMAN_SUCCESS, ret);
  141. ASSERT_TRUE(recv_message.value(length));
  142. ASSERT_EQ(length, data.size());
  143. delete connection1;
  144. gearman_packet_free(&recv_message);
  145. gearman_packet_free(&message);
  146. gearman_universal_free(universal);
  147. return TEST_SUCCESS;
  148. }
  149. test_st GEARMAN_COMMAND_ECHO_REQ_TESTS[] ={
  150. {"GEARMAN_COMMAND_ECHO_REQ check", 0, GEARMAN_COMMAND_ECHO_REQ_TEST },
  151. {"GEARMAN_COMMAND_ECHO_REQ overrun", 0, GEARMAN_COMMAND_ECHO_REQ_overrun_TEST },
  152. {0, 0, 0}
  153. };
  154. test_st GEARMAN_COMMAND_WORK_EXCEPTION_TESTS[] ={
  155. #if 0
  156. {"GEARMAN_COMMAND_WORK_EXCEPTION check", 0, GEARMAN_COMMAND_WORK_EXCEPTION_TEST },
  157. #endif
  158. {0, 0, 0}
  159. };
  160. collection_st collection[] ={
  161. {"GEARMAN_COMMAND_ECHO_REQ", 0, 0, GEARMAN_COMMAND_ECHO_REQ_TESTS},
  162. {"GEARMAN_COMMAND_WORK_EXCEPTION", 0, 0, GEARMAN_COMMAND_WORK_EXCEPTION_TESTS},
  163. {0, 0, 0, 0}
  164. };
  165. static void *world_create(server_startup_st& servers, test_return_t&)
  166. {
  167. /**
  168. We start up everything before we allocate so that we don't have to track memory in the forked process.
  169. */
  170. SKIP_UNLESS(server_startup(servers, "gearmand", libtest::default_port(), NULL));
  171. return NULL;
  172. }
  173. void get_world(libtest::Framework *world)
  174. {
  175. world->collections(collection);
  176. world->create(world_create);
  177. }