1
0

test-callback-stack.c 5.5 KB

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