test-tcp-write-queue-order.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "uv.h"
  25. #include "task.h"
  26. #define REQ_COUNT 10000
  27. static uv_timer_t timer;
  28. static uv_tcp_t server;
  29. static uv_tcp_t client;
  30. static uv_tcp_t incoming;
  31. static int connect_cb_called;
  32. static int close_cb_called;
  33. static int connection_cb_called;
  34. static int write_callbacks;
  35. static int write_cancelled_callbacks;
  36. static int write_error_callbacks;
  37. static uv_write_t write_requests[REQ_COUNT];
  38. static void close_cb(uv_handle_t* handle) {
  39. close_cb_called++;
  40. }
  41. static void timer_cb(uv_timer_t* handle) {
  42. uv_close((uv_handle_t*) &client, close_cb);
  43. uv_close((uv_handle_t*) &server, close_cb);
  44. uv_close((uv_handle_t*) &incoming, close_cb);
  45. }
  46. static void write_cb(uv_write_t* req, int status) {
  47. if (status == 0)
  48. write_callbacks++;
  49. else if (status == UV_ECANCELED)
  50. write_cancelled_callbacks++;
  51. else
  52. write_error_callbacks++;
  53. }
  54. static void connect_cb(uv_connect_t* req, int status) {
  55. static char base[1024];
  56. int r;
  57. int i;
  58. uv_buf_t buf;
  59. ASSERT(status == 0);
  60. connect_cb_called++;
  61. buf = uv_buf_init(base, sizeof(base));
  62. for (i = 0; i < REQ_COUNT; i++) {
  63. r = uv_write(&write_requests[i],
  64. req->handle,
  65. &buf,
  66. 1,
  67. write_cb);
  68. ASSERT(r == 0);
  69. }
  70. }
  71. static void connection_cb(uv_stream_t* tcp, int status) {
  72. ASSERT(status == 0);
  73. ASSERT(0 == uv_tcp_init(tcp->loop, &incoming));
  74. ASSERT(0 == uv_accept(tcp, (uv_stream_t*) &incoming));
  75. ASSERT(0 == uv_timer_init(uv_default_loop(), &timer));
  76. ASSERT(0 == uv_timer_start(&timer, timer_cb, 1000, 0));
  77. connection_cb_called++;
  78. }
  79. static void start_server(void) {
  80. struct sockaddr_in addr;
  81. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
  82. ASSERT(0 == uv_tcp_init(uv_default_loop(), &server));
  83. ASSERT(0 == uv_tcp_bind(&server, (struct sockaddr*) &addr, 0));
  84. ASSERT(0 == uv_listen((uv_stream_t*) &server, 128, connection_cb));
  85. }
  86. TEST_IMPL(tcp_write_queue_order) {
  87. uv_connect_t connect_req;
  88. struct sockaddr_in addr;
  89. int buffer_size = 16 * 1024;
  90. start_server();
  91. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  92. ASSERT(0 == uv_tcp_init(uv_default_loop(), &client));
  93. ASSERT(0 == uv_tcp_connect(&connect_req,
  94. &client,
  95. (struct sockaddr*) &addr,
  96. connect_cb));
  97. ASSERT(0 == uv_send_buffer_size((uv_handle_t*) &client, &buffer_size));
  98. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  99. ASSERT(connect_cb_called == 1);
  100. ASSERT(connection_cb_called == 1);
  101. ASSERT(write_callbacks > 0);
  102. ASSERT(write_cancelled_callbacks > 0);
  103. ASSERT(write_callbacks +
  104. write_error_callbacks +
  105. write_cancelled_callbacks == REQ_COUNT);
  106. ASSERT(close_cb_called == 3);
  107. MAKE_VALGRIND_HAPPY();
  108. return 0;
  109. }