test-tcp-writealot.c 4.4 KB

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