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