PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test-ping-pong.c

http://github.com/joyent/libuv
C | 255 lines | 146 code | 74 blank | 35 comment | 18 complexity | ea8f71cc0c42a93b4290592d1b813e96 MD5 | raw file
Possible License(s): ISC, BSD-2-Clause
  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 <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h> /* strlen */
  26. static int completed_pingers = 0;
  27. #define NUM_PINGS 1000
  28. /* 64 bytes is enough for a pinger */
  29. #define BUFSIZE 10240
  30. static char PING[] = "PING\n";
  31. static int pinger_on_connect_count;
  32. typedef struct {
  33. int pongs;
  34. int state;
  35. union {
  36. uv_tcp_t tcp;
  37. uv_pipe_t pipe;
  38. } stream;
  39. uv_connect_t connect_req;
  40. char read_buffer[BUFSIZE];
  41. } pinger_t;
  42. void pinger_try_read(pinger_t* pinger);
  43. static uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) {
  44. return uv_buf_init(malloc(size), size);
  45. }
  46. static void pinger_on_close(uv_handle_t* handle) {
  47. pinger_t* pinger = (pinger_t*)handle->data;
  48. ASSERT(NUM_PINGS == pinger->pongs);
  49. free(pinger);
  50. completed_pingers++;
  51. }
  52. static void pinger_after_write(uv_write_t *req, int status) {
  53. ASSERT(status == 0);
  54. free(req);
  55. }
  56. static void pinger_write_ping(pinger_t* pinger) {
  57. uv_write_t *req;
  58. uv_buf_t buf;
  59. buf.base = (char*)&PING;
  60. buf.len = strlen(PING);
  61. req = malloc(sizeof(uv_write_t));
  62. if (uv_write(req, (uv_stream_t*)&pinger->stream.tcp, &buf, 1, pinger_after_write)) {
  63. FATAL("uv_write failed");
  64. }
  65. puts("PING");
  66. }
  67. static void pinger_read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) {
  68. ssize_t i;
  69. pinger_t* pinger;
  70. pinger = (pinger_t*)stream->data;
  71. if (nread < 0) {
  72. ASSERT(uv_last_error(uv_default_loop()).code == UV_EOF);
  73. puts("got EOF");
  74. free(buf.base);
  75. uv_close((uv_handle_t*)(&pinger->stream.tcp), pinger_on_close);
  76. return;
  77. }
  78. /* Now we count the pings */
  79. for (i = 0; i < nread; i++) {
  80. ASSERT(buf.base[i] == PING[pinger->state]);
  81. pinger->state = (pinger->state + 1) % (sizeof(PING) - 1);
  82. if (pinger->state != 0)
  83. continue;
  84. printf("PONG %d\n", pinger->pongs);
  85. pinger->pongs++;
  86. if (pinger->pongs < NUM_PINGS) {
  87. pinger_write_ping(pinger);
  88. } else {
  89. uv_close((uv_handle_t*)(&pinger->stream.tcp), pinger_on_close);
  90. break;
  91. }
  92. }
  93. free(buf.base);
  94. }
  95. static void pinger_on_connect(uv_connect_t *req, int status) {
  96. pinger_t *pinger = (pinger_t*)req->handle->data;
  97. pinger_on_connect_count++;
  98. ASSERT(status == 0);
  99. ASSERT(uv_is_readable(req->handle));
  100. ASSERT(uv_is_writable(req->handle));
  101. ASSERT(!uv_is_closing((uv_handle_t *)req->handle));
  102. pinger_write_ping(pinger);
  103. uv_read_start((uv_stream_t*)(req->handle), alloc_cb, pinger_read_cb);
  104. }
  105. /* same ping-pong test, but using IPv6 connection */
  106. static void tcp_pinger_v6_new() {
  107. int r;
  108. struct sockaddr_in6 server_addr = uv_ip6_addr("::1", TEST_PORT);
  109. pinger_t *pinger;
  110. pinger = (pinger_t*)malloc(sizeof(*pinger));
  111. pinger->state = 0;
  112. pinger->pongs = 0;
  113. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  114. r = uv_tcp_init(uv_default_loop(), &pinger->stream.tcp);
  115. pinger->stream.tcp.data = pinger;
  116. ASSERT(!r);
  117. /* We are never doing multiple reads/connects at a time anyway. */
  118. /* so these handles can be pre-initialized. */
  119. r = uv_tcp_connect6(&pinger->connect_req, &pinger->stream.tcp, server_addr,
  120. pinger_on_connect);
  121. ASSERT(!r);
  122. /* Synchronous connect callbacks are not allowed. */
  123. ASSERT(pinger_on_connect_count == 0);
  124. }
  125. static void tcp_pinger_new() {
  126. int r;
  127. struct sockaddr_in server_addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  128. pinger_t *pinger;
  129. pinger = (pinger_t*)malloc(sizeof(*pinger));
  130. pinger->state = 0;
  131. pinger->pongs = 0;
  132. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  133. r = uv_tcp_init(uv_default_loop(), &pinger->stream.tcp);
  134. pinger->stream.tcp.data = pinger;
  135. ASSERT(!r);
  136. /* We are never doing multiple reads/connects at a time anyway. */
  137. /* so these handles can be pre-initialized. */
  138. r = uv_tcp_connect(&pinger->connect_req, &pinger->stream.tcp, server_addr,
  139. pinger_on_connect);
  140. ASSERT(!r);
  141. /* Synchronous connect callbacks are not allowed. */
  142. ASSERT(pinger_on_connect_count == 0);
  143. }
  144. static void pipe_pinger_new() {
  145. int r;
  146. pinger_t *pinger;
  147. pinger = (pinger_t*)malloc(sizeof(*pinger));
  148. pinger->state = 0;
  149. pinger->pongs = 0;
  150. /* Try to connect to the server and do NUM_PINGS ping-pongs. */
  151. r = uv_pipe_init(uv_default_loop(), &pinger->stream.pipe, 0);
  152. pinger->stream.pipe.data = pinger;
  153. ASSERT(!r);
  154. /* We are never doing multiple reads/connects at a time anyway. */
  155. /* so these handles can be pre-initialized. */
  156. uv_pipe_connect(&pinger->connect_req, &pinger->stream.pipe, TEST_PIPENAME,
  157. pinger_on_connect);
  158. /* Synchronous connect callbacks are not allowed. */
  159. ASSERT(pinger_on_connect_count == 0);
  160. }
  161. TEST_IMPL(tcp_ping_pong) {
  162. tcp_pinger_new();
  163. uv_run(uv_default_loop());
  164. ASSERT(completed_pingers == 1);
  165. MAKE_VALGRIND_HAPPY();
  166. return 0;
  167. }
  168. TEST_IMPL(tcp_ping_pong_v6) {
  169. tcp_pinger_v6_new();
  170. uv_run(uv_default_loop());
  171. ASSERT(completed_pingers == 1);
  172. MAKE_VALGRIND_HAPPY();
  173. return 0;
  174. }
  175. TEST_IMPL(pipe_ping_pong) {
  176. pipe_pinger_new();
  177. uv_run(uv_default_loop());
  178. ASSERT(completed_pingers == 1);
  179. MAKE_VALGRIND_HAPPY();
  180. return 0;
  181. }