test-ping-pong.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. static int completed_pingers = 0;
  26. #if defined(__CYGWIN__) || defined(__MSYS__) || defined(__MVS__)
  27. #define NUM_PINGS 100 /* fewer pings to avoid timeout */
  28. #else
  29. #define NUM_PINGS 1000
  30. #endif
  31. /* 64 bytes is enough for a pinger */
  32. #define BUFSIZE 10240
  33. static char PING[] = "PING\n";
  34. static int pinger_on_connect_count;
  35. typedef struct {
  36. int vectored_writes;
  37. int pongs;
  38. int state;
  39. union {
  40. uv_tcp_t tcp;
  41. uv_pipe_t pipe;
  42. } stream;
  43. uv_connect_t connect_req;
  44. char read_buffer[BUFSIZE];
  45. } pinger_t;
  46. static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
  47. buf->base = malloc(size);
  48. buf->len = size;
  49. }
  50. static void pinger_on_close(uv_handle_t* handle) {
  51. pinger_t* pinger = (pinger_t*)handle->data;
  52. ASSERT(NUM_PINGS == pinger->pongs);
  53. free(pinger);
  54. completed_pingers++;
  55. }
  56. static void pinger_after_write(uv_write_t *req, int status) {
  57. ASSERT(status == 0);
  58. free(req);
  59. }
  60. static void pinger_write_ping(pinger_t* pinger) {
  61. uv_write_t *req;
  62. uv_buf_t bufs[sizeof PING - 1];
  63. int i, nbufs;
  64. if (!pinger->vectored_writes) {
  65. /* Write a single buffer. */
  66. nbufs = 1;
  67. bufs[0] = uv_buf_init(PING, sizeof PING - 1);
  68. } else {
  69. /* Write multiple buffers, each with one byte in them. */
  70. nbufs = sizeof PING - 1;
  71. for (i = 0; i < nbufs; i++) {
  72. bufs[i] = uv_buf_init(&PING[i], 1);
  73. }
  74. }
  75. req = malloc(sizeof(*req));
  76. if (uv_write(req,
  77. (uv_stream_t*) &pinger->stream.tcp,
  78. bufs,
  79. nbufs,
  80. pinger_after_write)) {
  81. FATAL("uv_write failed");
  82. }
  83. puts("PING");
  84. }
  85. static void pinger_read_cb(uv_stream_t* stream,
  86. ssize_t nread,
  87. const uv_buf_t* buf) {
  88. ssize_t i;
  89. pinger_t* pinger;
  90. pinger = (pinger_t*)stream->data;
  91. if (nread < 0) {
  92. ASSERT(nread == UV_EOF);
  93. puts("got EOF");
  94. free(buf->base);
  95. uv_close((uv_handle_t*)(&pinger->stream.tcp), pinger_on_close);
  96. return;
  97. }
  98. /* Now we count the pings */
  99. for (i = 0; i < nread; i++) {
  100. ASSERT(buf->base[i] == PING[pinger->state]);
  101. pinger->state = (pinger->state + 1) % (sizeof(PING) - 1);
  102. if (pinger->state != 0)
  103. continue;
  104. printf("PONG %d\n", pinger->pongs);
  105. pinger->pongs++;
  106. if (pinger->pongs < NUM_PINGS) {
  107. pinger_write_ping(pinger);
  108. } else {
  109. uv_close((uv_handle_t*)(&pinger->stream.tcp), pinger_on_close);
  110. break;
  111. }
  112. }
  113. free(buf->base);
  114. }
  115. static void pinger_on_connect(uv_connect_t *req, int status) {
  116. pinger_t *pinger = (pinger_t*)req->handle->data;
  117. pinger_on_connect_count++;
  118. ASSERT(status == 0);
  119. ASSERT(1 == uv_is_readable(req->handle));
  120. ASSERT(1 == uv_is_writable(req->handle));
  121. ASSERT(0 == uv_is_closing((uv_handle_t *) req->handle));
  122. pinger_write_ping(pinger);
  123. uv_read_start((uv_stream_t*)(req->handle), alloc_cb, pinger_read_cb);
  124. }
  125. /* same ping-pong test, but using IPv6 connection */
  126. static void tcp_pinger_v6_new(int vectored_writes) {
  127. int r;
  128. struct sockaddr_in6 server_addr;
  129. pinger_t *pinger;
  130. ASSERT(0 ==uv_ip6_addr("::1", TEST_PORT, &server_addr));
  131. pinger = malloc(sizeof(*pinger));
  132. ASSERT(pinger != NULL);
  133. pinger->vectored_writes = vectored_writes;
  134. pinger->state = 0;
  135. pinger->pongs = 0;
  136. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  137. r = uv_tcp_init(uv_default_loop(), &pinger->stream.tcp);
  138. pinger->stream.tcp.data = pinger;
  139. ASSERT(!r);
  140. /* We are never doing multiple reads/connects at a time anyway, so these
  141. * handles can be pre-initialized. */
  142. r = uv_tcp_connect(&pinger->connect_req,
  143. &pinger->stream.tcp,
  144. (const struct sockaddr*) &server_addr,
  145. pinger_on_connect);
  146. ASSERT(!r);
  147. /* Synchronous connect callbacks are not allowed. */
  148. ASSERT(pinger_on_connect_count == 0);
  149. }
  150. static void tcp_pinger_new(int vectored_writes) {
  151. int r;
  152. struct sockaddr_in server_addr;
  153. pinger_t *pinger;
  154. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));
  155. pinger = malloc(sizeof(*pinger));
  156. ASSERT(pinger != NULL);
  157. pinger->vectored_writes = vectored_writes;
  158. pinger->state = 0;
  159. pinger->pongs = 0;
  160. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  161. r = uv_tcp_init(uv_default_loop(), &pinger->stream.tcp);
  162. pinger->stream.tcp.data = pinger;
  163. ASSERT(!r);
  164. /* We are never doing multiple reads/connects at a time anyway, so these
  165. * handles can be pre-initialized. */
  166. r = uv_tcp_connect(&pinger->connect_req,
  167. &pinger->stream.tcp,
  168. (const struct sockaddr*) &server_addr,
  169. pinger_on_connect);
  170. ASSERT(!r);
  171. /* Synchronous connect callbacks are not allowed. */
  172. ASSERT(pinger_on_connect_count == 0);
  173. }
  174. static void pipe_pinger_new(int vectored_writes) {
  175. int r;
  176. pinger_t *pinger;
  177. pinger = (pinger_t*)malloc(sizeof(*pinger));
  178. ASSERT(pinger != NULL);
  179. pinger->vectored_writes = vectored_writes;
  180. pinger->state = 0;
  181. pinger->pongs = 0;
  182. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  183. r = uv_pipe_init(uv_default_loop(), &pinger->stream.pipe, 0);
  184. pinger->stream.pipe.data = pinger;
  185. ASSERT(!r);
  186. /* We are never doing multiple reads/connects at a time anyway, so these
  187. * handles can be pre-initialized. */
  188. uv_pipe_connect(&pinger->connect_req, &pinger->stream.pipe, TEST_PIPENAME,
  189. pinger_on_connect);
  190. /* Synchronous connect callbacks are not allowed. */
  191. ASSERT(pinger_on_connect_count == 0);
  192. }
  193. static int run_ping_pong_test(void) {
  194. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  195. ASSERT(completed_pingers == 1);
  196. MAKE_VALGRIND_HAPPY();
  197. return 0;
  198. }
  199. TEST_IMPL(tcp_ping_pong) {
  200. tcp_pinger_new(0);
  201. return run_ping_pong_test();
  202. }
  203. TEST_IMPL(tcp_ping_pong_vec) {
  204. tcp_pinger_new(1);
  205. return run_ping_pong_test();
  206. }
  207. TEST_IMPL(tcp6_ping_pong) {
  208. if (!can_ipv6())
  209. RETURN_SKIP("IPv6 not supported");
  210. tcp_pinger_v6_new(0);
  211. return run_ping_pong_test();
  212. }
  213. TEST_IMPL(tcp6_ping_pong_vec) {
  214. if (!can_ipv6())
  215. RETURN_SKIP("IPv6 not supported");
  216. tcp_pinger_v6_new(1);
  217. return run_ping_pong_test();
  218. }
  219. TEST_IMPL(pipe_ping_pong) {
  220. pipe_pinger_new(0);
  221. return run_ping_pong_test();
  222. }
  223. TEST_IMPL(pipe_ping_pong_vec) {
  224. pipe_pinger_new(1);
  225. return run_ping_pong_test();
  226. }