2011-04-18 18:10:18 +00:00
|
|
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-04-19 15:01:45 +00:00
|
|
|
/*
|
2011-04-19 02:26:32 +00:00
|
|
|
* TODO: Add explanation of why we want on_close to be called from fresh
|
2011-04-15 17:27:09 +00:00
|
|
|
* stack.
|
|
|
|
|
*/
|
2011-04-05 02:24:03 +00:00
|
|
|
|
2011-07-06 22:28:18 +00:00
|
|
|
#include "uv.h"
|
2011-04-19 02:26:32 +00:00
|
|
|
#include "task.h"
|
|
|
|
|
|
|
|
|
|
|
2011-05-08 01:27:05 +00:00
|
|
|
static const char MESSAGE[] = "Failure is for the weak. Everyone dies alone.";
|
2011-05-04 15:10:33 +00:00
|
|
|
|
2011-06-02 01:19:47 +00:00
|
|
|
static uv_tcp_t client;
|
|
|
|
|
static uv_timer_t timer;
|
2011-07-13 20:04:51 +00:00
|
|
|
static uv_connect_t connect_req;
|
|
|
|
|
static uv_write_t write_req;
|
|
|
|
|
static uv_shutdown_t shutdown_req;
|
2011-05-04 15:10:33 +00:00
|
|
|
|
2011-05-08 01:27:05 +00:00
|
|
|
static int nested = 0;
|
|
|
|
|
static int close_cb_called = 0;
|
|
|
|
|
static int connect_cb_called = 0;
|
|
|
|
|
static int write_cb_called = 0;
|
2011-05-16 23:17:48 +00:00
|
|
|
static int timer_cb_called = 0;
|
2011-05-08 01:27:05 +00:00
|
|
|
static int bytes_received = 0;
|
|
|
|
|
static int shutdown_cb_called = 0;
|
2011-04-05 02:24:03 +00:00
|
|
|
|
|
|
|
|
|
2013-08-31 13:48:23 +00:00
|
|
|
static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
|
|
|
|
|
buf->len = size;
|
|
|
|
|
buf->base = malloc(size);
|
|
|
|
|
ASSERT(buf->base != NULL);
|
2011-06-02 04:04:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-06-08 10:02:42 +00:00
|
|
|
static void close_cb(uv_handle_t* handle) {
|
2011-05-04 15:10:33 +00:00
|
|
|
ASSERT(nested == 0 && "close_cb must be called from a fresh stack");
|
|
|
|
|
|
2011-04-05 02:24:03 +00:00
|
|
|
close_cb_called++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-07-13 20:04:51 +00:00
|
|
|
static void shutdown_cb(uv_shutdown_t* req, int status) {
|
2011-05-04 15:10:33 +00:00
|
|
|
ASSERT(status == 0);
|
|
|
|
|
ASSERT(nested == 0 && "shutdown_cb must be called from a fresh stack");
|
|
|
|
|
|
|
|
|
|
shutdown_cb_called++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-08-31 00:15:08 +00:00
|
|
|
static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
|
2011-05-04 15:10:33 +00:00
|
|
|
ASSERT(nested == 0 && "read_cb must be called from a fresh stack");
|
|
|
|
|
|
2011-07-08 01:25:52 +00:00
|
|
|
printf("Read. nread == %d\n", (int)nread);
|
2013-08-31 00:15:08 +00:00
|
|
|
free(buf->base);
|
2011-05-04 20:56:50 +00:00
|
|
|
|
|
|
|
|
if (nread == 0) {
|
|
|
|
|
return;
|
|
|
|
|
|
2013-06-06 21:10:50 +00:00
|
|
|
} else if (nread < 0) {
|
|
|
|
|
ASSERT(nread == UV_EOF);
|
2011-05-04 15:10:33 +00:00
|
|
|
|
|
|
|
|
nested++;
|
2011-07-21 01:03:08 +00:00
|
|
|
uv_close((uv_handle_t*)tcp, close_cb);
|
2011-05-04 15:10:33 +00:00
|
|
|
nested--;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bytes_received += nread;
|
|
|
|
|
|
2018-05-29 04:34:03 +00:00
|
|
|
/* We call shutdown here because when bytes_received == sizeof MESSAGE there
|
|
|
|
|
* will be no more data sent nor received, so here it would be possible for a
|
|
|
|
|
* backend to call shutdown_cb immediately and *not* from a fresh stack. */
|
2011-05-04 15:10:33 +00:00
|
|
|
if (bytes_received == sizeof MESSAGE) {
|
|
|
|
|
nested++;
|
2011-05-04 20:56:50 +00:00
|
|
|
|
|
|
|
|
puts("Shutdown");
|
|
|
|
|
|
2011-07-13 20:04:51 +00:00
|
|
|
if (uv_shutdown(&shutdown_req, (uv_stream_t*)tcp, shutdown_cb)) {
|
2011-05-12 02:56:33 +00:00
|
|
|
FATAL("uv_shutdown failed");
|
2011-05-04 15:10:33 +00:00
|
|
|
}
|
|
|
|
|
nested--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-03-10 23:34:40 +00:00
|
|
|
static void timer_cb(uv_timer_t* handle) {
|
2011-06-17 16:39:18 +00:00
|
|
|
ASSERT(handle == &timer);
|
2011-05-16 23:17:48 +00:00
|
|
|
ASSERT(nested == 0 && "timer_cb must be called from a fresh stack");
|
2011-05-04 15:10:33 +00:00
|
|
|
|
2011-05-04 20:56:50 +00:00
|
|
|
puts("Timeout complete. Now read data...");
|
|
|
|
|
|
2011-05-04 15:10:33 +00:00
|
|
|
nested++;
|
2011-07-01 00:39:27 +00:00
|
|
|
if (uv_read_start((uv_stream_t*)&client, alloc_cb, read_cb)) {
|
2011-05-12 02:56:33 +00:00
|
|
|
FATAL("uv_read_start failed");
|
2011-05-04 15:10:33 +00:00
|
|
|
}
|
|
|
|
|
nested--;
|
|
|
|
|
|
2011-05-16 23:17:48 +00:00
|
|
|
timer_cb_called++;
|
|
|
|
|
|
2011-07-21 01:03:08 +00:00
|
|
|
uv_close((uv_handle_t*)handle, close_cb);
|
2011-05-04 15:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-07-13 20:04:51 +00:00
|
|
|
static void write_cb(uv_write_t* req, int status) {
|
2011-05-16 23:17:48 +00:00
|
|
|
int r;
|
|
|
|
|
|
2011-05-04 15:10:33 +00:00
|
|
|
ASSERT(status == 0);
|
|
|
|
|
ASSERT(nested == 0 && "write_cb must be called from a fresh stack");
|
|
|
|
|
|
2011-05-04 20:56:50 +00:00
|
|
|
puts("Data written. 500ms timeout...");
|
|
|
|
|
|
2018-05-29 04:34:03 +00:00
|
|
|
/* After the data has been sent, we're going to wait for a while, then start
|
|
|
|
|
* reading. This makes us certain that the message has been echoed back to
|
|
|
|
|
* our receive buffer when we start reading. This maximizes the temptation
|
|
|
|
|
* for the backend to use dirty stack for calling read_cb. */
|
2011-05-04 15:10:33 +00:00
|
|
|
nested++;
|
2011-08-31 02:18:29 +00:00
|
|
|
r = uv_timer_init(uv_default_loop(), &timer);
|
2011-05-16 23:17:48 +00:00
|
|
|
ASSERT(r == 0);
|
|
|
|
|
r = uv_timer_start(&timer, timer_cb, 500, 0);
|
|
|
|
|
ASSERT(r == 0);
|
2011-05-04 15:10:33 +00:00
|
|
|
nested--;
|
|
|
|
|
|
|
|
|
|
write_cb_called++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-07-13 20:04:51 +00:00
|
|
|
static void connect_cb(uv_connect_t* req, int status) {
|
2011-05-13 14:15:02 +00:00
|
|
|
uv_buf_t buf;
|
2011-05-04 15:10:33 +00:00
|
|
|
|
2011-05-04 20:56:50 +00:00
|
|
|
puts("Connected. Write some data to echo server...");
|
|
|
|
|
|
2011-05-04 15:10:33 +00:00
|
|
|
ASSERT(status == 0);
|
|
|
|
|
ASSERT(nested == 0 && "connect_cb must be called from a fresh stack");
|
|
|
|
|
|
|
|
|
|
nested++;
|
|
|
|
|
|
|
|
|
|
buf.base = (char*) &MESSAGE;
|
|
|
|
|
buf.len = sizeof MESSAGE;
|
|
|
|
|
|
2011-07-13 20:04:51 +00:00
|
|
|
if (uv_write(&write_req, (uv_stream_t*)req->handle, &buf, 1, write_cb)) {
|
2011-05-12 02:56:33 +00:00
|
|
|
FATAL("uv_write failed");
|
2011-05-04 15:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nested--;
|
|
|
|
|
|
|
|
|
|
connect_cb_called++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEST_IMPL(callback_stack) {
|
2013-08-31 01:00:34 +00:00
|
|
|
struct sockaddr_in addr;
|
|
|
|
|
|
|
|
|
|
ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
|
2011-04-05 02:24:03 +00:00
|
|
|
|
2011-08-31 02:18:29 +00:00
|
|
|
if (uv_tcp_init(uv_default_loop(), &client)) {
|
2011-05-12 02:56:33 +00:00
|
|
|
FATAL("uv_tcp_init failed");
|
2011-04-15 17:27:09 +00:00
|
|
|
}
|
2011-04-05 02:24:03 +00:00
|
|
|
|
2011-05-04 20:56:50 +00:00
|
|
|
puts("Connecting...");
|
|
|
|
|
|
2011-04-05 02:24:03 +00:00
|
|
|
nested++;
|
2011-07-08 01:25:52 +00:00
|
|
|
|
2013-09-03 21:12:07 +00:00
|
|
|
if (uv_tcp_connect(&connect_req,
|
|
|
|
|
&client,
|
|
|
|
|
(const struct sockaddr*) &addr,
|
|
|
|
|
connect_cb)) {
|
2011-07-01 00:39:27 +00:00
|
|
|
FATAL("uv_tcp_connect failed");
|
2011-04-15 17:27:09 +00:00
|
|
|
}
|
2011-04-05 02:24:03 +00:00
|
|
|
nested--;
|
|
|
|
|
|
2013-01-16 22:20:03 +00:00
|
|
|
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
2011-04-05 02:24:03 +00:00
|
|
|
|
2011-04-18 07:29:13 +00:00
|
|
|
ASSERT(nested == 0);
|
2011-05-04 15:10:33 +00:00
|
|
|
ASSERT(connect_cb_called == 1 && "connect_cb must be called exactly once");
|
|
|
|
|
ASSERT(write_cb_called == 1 && "write_cb must be called exactly once");
|
2011-05-16 23:17:48 +00:00
|
|
|
ASSERT(timer_cb_called == 1 && "timer_cb must be called exactly once");
|
2011-05-04 15:10:33 +00:00
|
|
|
ASSERT(bytes_received == sizeof MESSAGE);
|
|
|
|
|
ASSERT(shutdown_cb_called == 1 && "shutdown_cb must be called exactly once");
|
2011-05-16 23:17:48 +00:00
|
|
|
ASSERT(close_cb_called == 2 && "close_cb must be called exactly twice");
|
2011-04-05 02:24:03 +00:00
|
|
|
|
2012-10-16 15:20:32 +00:00
|
|
|
MAKE_VALGRIND_HAPPY();
|
2011-04-05 02:24:03 +00:00
|
|
|
return 0;
|
2011-04-15 17:27:09 +00:00
|
|
|
}
|