1
0

test-tcp-writealot.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
  41. buf->base = malloc(size);
  42. buf->len = size;
  43. }
  44. static void close_cb(uv_handle_t* handle) {
  45. ASSERT(handle != NULL);
  46. close_cb_called++;
  47. }
  48. static void shutdown_cb(uv_shutdown_t* req, int status) {
  49. uv_tcp_t* tcp;
  50. ASSERT(req == &shutdown_req);
  51. ASSERT(status == 0);
  52. tcp = (uv_tcp_t*)(req->handle);
  53. /* The write buffer should be empty by now. */
  54. ASSERT(tcp->write_queue_size == 0);
  55. /* Now we wait for the EOF */
  56. shutdown_cb_called++;
  57. /* We should have had all the writes called already. */
  58. ASSERT(write_cb_called == WRITES);
  59. }
  60. static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
  61. ASSERT(tcp != NULL);
  62. if (nread >= 0) {
  63. bytes_received_done += nread;
  64. }
  65. else {
  66. ASSERT(nread == UV_EOF);
  67. printf("GOT EOF\n");
  68. uv_close((uv_handle_t*)tcp, close_cb);
  69. }
  70. free(buf->base);
  71. }
  72. static void write_cb(uv_write_t* req, int status) {
  73. ASSERT(req != NULL);
  74. if (status) {
  75. fprintf(stderr, "uv_write error: %s\n", uv_strerror(status));
  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;
  108. uv_tcp_t client;
  109. int r;
  110. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  111. send_buffer = calloc(1, TOTAL_BYTES);
  112. ASSERT(send_buffer != NULL);
  113. r = uv_tcp_init(uv_default_loop(), &client);
  114. ASSERT(r == 0);
  115. r = uv_tcp_connect(&connect_req,
  116. &client,
  117. (const struct sockaddr*) &addr,
  118. connect_cb);
  119. ASSERT(r == 0);
  120. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  121. ASSERT(shutdown_cb_called == 1);
  122. ASSERT(connect_cb_called == 1);
  123. ASSERT(write_cb_called == WRITES);
  124. ASSERT(close_cb_called == 1);
  125. ASSERT(bytes_sent == TOTAL_BYTES);
  126. ASSERT(bytes_sent_done == TOTAL_BYTES);
  127. ASSERT(bytes_received_done == TOTAL_BYTES);
  128. free(send_buffer);
  129. MAKE_VALGRIND_HAPPY();
  130. return 0;
  131. }