test-callback-stack.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /*
  22. * TODO: Add explanation of why we want on_close to be called from fresh
  23. * stack.
  24. */
  25. #include "uv.h"
  26. #include "task.h"
  27. static const char MESSAGE[] = "Failure is for the weak. Everyone dies alone.";
  28. static uv_tcp_t client;
  29. static uv_timer_t timer;
  30. static uv_connect_t connect_req;
  31. static uv_write_t write_req;
  32. static uv_shutdown_t shutdown_req;
  33. static int nested = 0;
  34. static int close_cb_called = 0;
  35. static int connect_cb_called = 0;
  36. static int write_cb_called = 0;
  37. static int timer_cb_called = 0;
  38. static int bytes_received = 0;
  39. static int shutdown_cb_called = 0;
  40. static uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) {
  41. uv_buf_t buf;
  42. buf.len = size;
  43. buf.base = (char*) malloc(size);
  44. ASSERT(buf.base);
  45. return buf;
  46. }
  47. static void close_cb(uv_handle_t* handle) {
  48. ASSERT(nested == 0 && "close_cb must be called from a fresh stack");
  49. close_cb_called++;
  50. }
  51. static void shutdown_cb(uv_shutdown_t* req, int status) {
  52. ASSERT(status == 0);
  53. ASSERT(nested == 0 && "shutdown_cb must be called from a fresh stack");
  54. shutdown_cb_called++;
  55. }
  56. static void read_cb(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) {
  57. ASSERT(nested == 0 && "read_cb must be called from a fresh stack");
  58. printf("Read. nread == %d\n", (int)nread);
  59. free(buf.base);
  60. if (nread == 0) {
  61. ASSERT(uv_last_error(uv_default_loop()).code == UV_EAGAIN);
  62. return;
  63. } else if (nread == -1) {
  64. ASSERT(uv_last_error(uv_default_loop()).code == UV_EOF);
  65. nested++;
  66. uv_close((uv_handle_t*)tcp, close_cb);
  67. nested--;
  68. return;
  69. }
  70. bytes_received += nread;
  71. /* We call shutdown here because when bytes_received == sizeof MESSAGE */
  72. /* there will be no more data sent nor received, so here it would be */
  73. /* possible for a backend to to call shutdown_cb immediately and *not* */
  74. /* from a fresh stack. */
  75. if (bytes_received == sizeof MESSAGE) {
  76. nested++;
  77. puts("Shutdown");
  78. if (uv_shutdown(&shutdown_req, (uv_stream_t*)tcp, shutdown_cb)) {
  79. FATAL("uv_shutdown failed");
  80. }
  81. nested--;
  82. }
  83. }
  84. static void timer_cb(uv_timer_t* handle, int status) {
  85. ASSERT(handle == &timer);
  86. ASSERT(status == 0);
  87. ASSERT(nested == 0 && "timer_cb must be called from a fresh stack");
  88. puts("Timeout complete. Now read data...");
  89. nested++;
  90. if (uv_read_start((uv_stream_t*)&client, alloc_cb, read_cb)) {
  91. FATAL("uv_read_start failed");
  92. }
  93. nested--;
  94. timer_cb_called++;
  95. uv_close((uv_handle_t*)handle, close_cb);
  96. }
  97. static void write_cb(uv_write_t* req, int status) {
  98. int r;
  99. ASSERT(status == 0);
  100. ASSERT(nested == 0 && "write_cb must be called from a fresh stack");
  101. puts("Data written. 500ms timeout...");
  102. /* After the data has been sent, we're going to wait for a while, then */
  103. /* start reading. This makes us certain that the message has been echoed */
  104. /* back to our receive buffer when we start reading. This maximizes the */
  105. /* temptation for the backend to use dirty stack for calling read_cb. */
  106. nested++;
  107. r = uv_timer_init(uv_default_loop(), &timer);
  108. ASSERT(r == 0);
  109. r = uv_timer_start(&timer, timer_cb, 500, 0);
  110. ASSERT(r == 0);
  111. nested--;
  112. write_cb_called++;
  113. }
  114. static void connect_cb(uv_connect_t* req, int status) {
  115. uv_buf_t buf;
  116. puts("Connected. Write some data to echo server...");
  117. ASSERT(status == 0);
  118. ASSERT(nested == 0 && "connect_cb must be called from a fresh stack");
  119. nested++;
  120. buf.base = (char*) &MESSAGE;
  121. buf.len = sizeof MESSAGE;
  122. if (uv_write(&write_req, (uv_stream_t*)req->handle, &buf, 1, write_cb)) {
  123. FATAL("uv_write failed");
  124. }
  125. nested--;
  126. connect_cb_called++;
  127. }
  128. TEST_IMPL(callback_stack) {
  129. struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  130. if (uv_tcp_init(uv_default_loop(), &client)) {
  131. FATAL("uv_tcp_init failed");
  132. }
  133. puts("Connecting...");
  134. nested++;
  135. if (uv_tcp_connect(&connect_req, &client, addr, connect_cb)) {
  136. FATAL("uv_tcp_connect failed");
  137. }
  138. nested--;
  139. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  140. ASSERT(nested == 0);
  141. ASSERT(connect_cb_called == 1 && "connect_cb must be called exactly once");
  142. ASSERT(write_cb_called == 1 && "write_cb must be called exactly once");
  143. ASSERT(timer_cb_called == 1 && "timer_cb must be called exactly once");
  144. ASSERT(bytes_received == sizeof MESSAGE);
  145. ASSERT(shutdown_cb_called == 1 && "shutdown_cb must be called exactly once");
  146. ASSERT(close_cb_called == 2 && "close_cb must be called exactly twice");
  147. MAKE_VALGRIND_HAPPY();
  148. return 0;
  149. }