regression.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. * 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 "config.h"
  39. #if defined(NDEBUG)
  40. # undef NDEBUG
  41. #endif
  42. #include <cassert>
  43. #include <cstdio>
  44. #include <cstdlib>
  45. #include <cstring>
  46. #define GEARMAN_CORE
  47. #include <libgearman/common.h>
  48. #include <libgearman/packet.hpp>
  49. #include <libgearman/universal.hpp>
  50. #include <libtest/test.h>
  51. #include <libtest/server.h>
  52. #include <libtest/worker.h>
  53. #define WORKER_TEST_PORT 32123
  54. typedef struct
  55. {
  56. pid_t gearmand_pid;
  57. } regression_st;
  58. void *create(void *object);
  59. void destroy(void *object);
  60. test_return_t pre(void *object);
  61. test_return_t post(void *object);
  62. test_return_t flush(void);
  63. static test_return_t bug372074_test(void *)
  64. {
  65. gearman_universal_st universal;
  66. const void *args[1];
  67. size_t args_size[1];
  68. gearman_universal_initialize(universal);
  69. for (uint32_t x= 0; x < 2; x++)
  70. {
  71. gearman_packet_st packet;
  72. gearman_connection_st *con_ptr;
  73. test_truth(con_ptr= gearman_connection_create(universal, NULL));
  74. con_ptr->set_host(NULL, WORKER_TEST_PORT);
  75. args[0]= "testUnregisterFunction";
  76. args_size[0]= strlen("testUnregisterFunction");
  77. test_truth(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  78. GEARMAN_COMMAND_SET_CLIENT_ID,
  79. args, args_size, 1)));
  80. test_truth(gearman_success(con_ptr->send(packet, true)));
  81. gearman_packet_free(&packet);
  82. args[0]= "reverse";
  83. args_size[0]= strlen("reverse");
  84. test_truth(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST, GEARMAN_COMMAND_CAN_DO,
  85. args, args_size, 1)));
  86. test_truth(gearman_success(con_ptr->send(packet, true)));
  87. gearman_packet_free(&packet);
  88. test_truth(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  89. GEARMAN_COMMAND_CANT_DO,
  90. args, args_size, 1)));
  91. test_truth(gearman_success(con_ptr->send(packet, true)));
  92. gearman_packet_free(&packet);
  93. delete con_ptr;
  94. test_truth(con_ptr= gearman_connection_create(universal, NULL));
  95. con_ptr->set_host(NULL, WORKER_TEST_PORT);
  96. args[0]= "testUnregisterFunction";
  97. args_size[0]= strlen("testUnregisterFunction");
  98. test_truth(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  99. GEARMAN_COMMAND_SET_CLIENT_ID,
  100. args, args_size, 1)));
  101. test_truth(gearman_success(con_ptr->send(packet, true)));
  102. gearman_packet_free(&packet);
  103. args[0]= "digest";
  104. args_size[0]= strlen("digest");
  105. test_truth(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  106. GEARMAN_COMMAND_CAN_DO,
  107. args, args_size, 1)));
  108. test_truth(gearman_success(con_ptr->send(packet, true)));
  109. gearman_packet_free(&packet);
  110. args[0]= "reverse";
  111. args_size[0]= strlen("reverse");
  112. test_truth(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  113. GEARMAN_COMMAND_CAN_DO,
  114. args, args_size, 1)));
  115. test_truth(gearman_success(con_ptr->send(packet, true)));
  116. gearman_packet_free(&packet);
  117. test_truth(gearman_success(gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  118. GEARMAN_COMMAND_RESET_ABILITIES,
  119. NULL, NULL, 0)));
  120. test_truth(gearman_success(con_ptr->send(packet, true)));
  121. gearman_packet_free(&packet);
  122. delete con_ptr;
  123. }
  124. gearman_universal_free(universal);
  125. return TEST_SUCCESS;
  126. }
  127. test_st worker_tests[] ={
  128. {"bug372074", 0, bug372074_test },
  129. {0, 0, 0}
  130. };
  131. collection_st collection[] ={
  132. {"worker_tests", 0, 0, worker_tests },
  133. {0, 0, 0, 0}
  134. };
  135. #ifndef __INTEL_COMPILER
  136. #pragma GCC diagnostic ignored "-Wold-style-cast"
  137. #endif
  138. static void *world_create(test_return_t *error)
  139. {
  140. regression_st *test;
  141. test= (regression_st *)malloc(sizeof(regression_st));
  142. test->gearmand_pid= test_gearmand_start(WORKER_TEST_PORT, 0, NULL);
  143. if (test->gearmand_pid == -1)
  144. {
  145. *error= TEST_FAILURE;
  146. return NULL;
  147. }
  148. *error= TEST_SUCCESS;
  149. return (void *)test;
  150. }
  151. static test_return_t world_destroy(void *object)
  152. {
  153. regression_st *test= (regression_st *)object;
  154. test_gearmand_stop(test->gearmand_pid);
  155. free(test);
  156. return TEST_SUCCESS;
  157. }
  158. void get_world(world_st *world)
  159. {
  160. world->collections= collection;
  161. world->create= world_create;
  162. world->destroy= world_destroy;
  163. }