1
0

test-tcp-try-write.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include <string.h>
  26. #define MAX_BYTES 1024 * 1024
  27. #ifdef _WIN32
  28. TEST_IMPL(tcp_try_write) {
  29. MAKE_VALGRIND_HAPPY();
  30. return 0;
  31. }
  32. #else /* !_WIN32 */
  33. static uv_tcp_t server;
  34. static uv_tcp_t client;
  35. static uv_tcp_t incoming;
  36. static int connect_cb_called;
  37. static int close_cb_called;
  38. static int connection_cb_called;
  39. static int bytes_read;
  40. static int bytes_written;
  41. static void close_cb(uv_handle_t* handle) {
  42. close_cb_called++;
  43. }
  44. static void connect_cb(uv_connect_t* req, int status) {
  45. static char zeroes[1024];
  46. int r;
  47. uv_buf_t buf;
  48. ASSERT(status == 0);
  49. connect_cb_called++;
  50. do {
  51. buf = uv_buf_init(zeroes, sizeof(zeroes));
  52. r = uv_try_write((uv_stream_t*) &client, &buf, 1);
  53. ASSERT(r >= 0);
  54. bytes_written += r;
  55. /* Partial write */
  56. if (r != (int) sizeof(zeroes))
  57. break;
  58. } while (1);
  59. uv_close((uv_handle_t*) &client, close_cb);
  60. }
  61. static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
  62. static char base[1024];
  63. buf->base = base;
  64. buf->len = sizeof(base);
  65. }
  66. static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
  67. if (nread < 0) {
  68. uv_close((uv_handle_t*) tcp, close_cb);
  69. uv_close((uv_handle_t*) &server, close_cb);
  70. return;
  71. }
  72. bytes_read += nread;
  73. }
  74. static void connection_cb(uv_stream_t* tcp, int status) {
  75. ASSERT(status == 0);
  76. ASSERT(0 == uv_tcp_init(tcp->loop, &incoming));
  77. ASSERT(0 == uv_accept(tcp, (uv_stream_t*) &incoming));
  78. connection_cb_called++;
  79. ASSERT(0 == uv_read_start((uv_stream_t*) &incoming, alloc_cb, read_cb));
  80. }
  81. static void start_server(void) {
  82. struct sockaddr_in addr;
  83. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
  84. ASSERT(0 == uv_tcp_init(uv_default_loop(), &server));
  85. ASSERT(0 == uv_tcp_bind(&server, (struct sockaddr*) &addr, 0));
  86. ASSERT(0 == uv_listen((uv_stream_t*) &server, 128, connection_cb));
  87. }
  88. TEST_IMPL(tcp_try_write) {
  89. uv_connect_t connect_req;
  90. struct sockaddr_in addr;
  91. start_server();
  92. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  93. ASSERT(0 == uv_tcp_init(uv_default_loop(), &client));
  94. ASSERT(0 == uv_tcp_connect(&connect_req,
  95. &client,
  96. (struct sockaddr*) &addr,
  97. connect_cb));
  98. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  99. ASSERT(connect_cb_called == 1);
  100. ASSERT(close_cb_called == 3);
  101. ASSERT(connection_cb_called == 1);
  102. ASSERT(bytes_read == bytes_written);
  103. ASSERT(bytes_written > 0);
  104. MAKE_VALGRIND_HAPPY();
  105. return 0;
  106. }
  107. #endif /* !_WIN32 */