test-tcp-writealot.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #define WRITES 3
  26. #if defined(__arm__) /* Decrease the chunks so the test passes on arm CI bots */
  27. #define CHUNKS_PER_WRITE 2048
  28. #else
  29. #define CHUNKS_PER_WRITE 4096
  30. #endif
  31. #define CHUNK_SIZE 10024 /* 10 kb */
  32. #define TOTAL_BYTES (WRITES * CHUNKS_PER_WRITE * CHUNK_SIZE)
  33. static char* send_buffer;
  34. static int shutdown_cb_called = 0;
  35. static int connect_cb_called = 0;
  36. static int write_cb_called = 0;
  37. static int close_cb_called = 0;
  38. static size_t bytes_sent = 0;
  39. static size_t bytes_sent_done = 0;
  40. static size_t bytes_received_done = 0;
  41. static uv_connect_t connect_req;
  42. static uv_shutdown_t shutdown_req;
  43. static uv_write_t write_reqs[WRITES];
  44. static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
  45. buf->base = malloc(size);
  46. buf->len = size;
  47. }
  48. static void close_cb(uv_handle_t* handle) {
  49. ASSERT(handle != NULL);
  50. close_cb_called++;
  51. }
  52. static void shutdown_cb(uv_shutdown_t* req, int status) {
  53. uv_tcp_t* tcp;
  54. ASSERT(req == &shutdown_req);
  55. ASSERT(status == 0);
  56. tcp = (uv_tcp_t*)(req->handle);
  57. /* The write buffer should be empty by now. */
  58. ASSERT(tcp->write_queue_size == 0);
  59. /* Now we wait for the EOF */
  60. shutdown_cb_called++;
  61. /* We should have had all the writes called already. */
  62. ASSERT(write_cb_called == WRITES);
  63. }
  64. static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
  65. ASSERT(tcp != NULL);
  66. if (nread >= 0) {
  67. bytes_received_done += nread;
  68. }
  69. else {
  70. ASSERT(nread == UV_EOF);
  71. printf("GOT EOF\n");
  72. uv_close((uv_handle_t*)tcp, close_cb);
  73. }
  74. free(buf->base);
  75. }
  76. static void write_cb(uv_write_t* req, int status) {
  77. ASSERT(req != NULL);
  78. if (status) {
  79. fprintf(stderr, "uv_write error: %s\n", uv_strerror(status));
  80. ASSERT(0);
  81. }
  82. bytes_sent_done += CHUNKS_PER_WRITE * CHUNK_SIZE;
  83. write_cb_called++;
  84. }
  85. static void connect_cb(uv_connect_t* req, int status) {
  86. uv_buf_t send_bufs[CHUNKS_PER_WRITE];
  87. uv_stream_t* stream;
  88. int i, j, r;
  89. ASSERT(req == &connect_req);
  90. ASSERT(status == 0);
  91. stream = req->handle;
  92. connect_cb_called++;
  93. /* Write a lot of data */
  94. for (i = 0; i < WRITES; i++) {
  95. uv_write_t* write_req = write_reqs + i;
  96. for (j = 0; j < CHUNKS_PER_WRITE; j++) {
  97. send_bufs[j] = uv_buf_init(send_buffer + bytes_sent, CHUNK_SIZE);
  98. bytes_sent += CHUNK_SIZE;
  99. }
  100. r = uv_write(write_req, stream, send_bufs, CHUNKS_PER_WRITE, write_cb);
  101. ASSERT(r == 0);
  102. }
  103. /* Shutdown on drain. */
  104. r = uv_shutdown(&shutdown_req, stream, shutdown_cb);
  105. ASSERT(r == 0);
  106. /* Start reading */
  107. r = uv_read_start(stream, alloc_cb, read_cb);
  108. ASSERT(r == 0);
  109. }
  110. TEST_IMPL(tcp_writealot) {
  111. struct sockaddr_in addr;
  112. uv_tcp_t client;
  113. int r;
  114. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  115. send_buffer = calloc(1, TOTAL_BYTES);
  116. ASSERT(send_buffer != NULL);
  117. r = uv_tcp_init(uv_default_loop(), &client);
  118. ASSERT(r == 0);
  119. r = uv_tcp_connect(&connect_req,
  120. &client,
  121. (const struct sockaddr*) &addr,
  122. connect_cb);
  123. ASSERT(r == 0);
  124. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  125. ASSERT(shutdown_cb_called == 1);
  126. ASSERT(connect_cb_called == 1);
  127. ASSERT(write_cb_called == WRITES);
  128. ASSERT(close_cb_called == 1);
  129. ASSERT(bytes_sent == TOTAL_BYTES);
  130. ASSERT(bytes_sent_done == TOTAL_BYTES);
  131. ASSERT(bytes_received_done == TOTAL_BYTES);
  132. free(send_buffer);
  133. MAKE_VALGRIND_HAPPY();
  134. return 0;
  135. }