libuv/test/test-callback-stack.c

204 lines
5.4 KiB
C
Raw Normal View History

/* 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-05-12 02:56:33 +00:00
#include "../uv.h"
2011-04-19 02:26:32 +00:00
#include "task.h"
static const char MESSAGE[] = "Failure is for the weak. Everyone dies alone.";
2011-05-12 02:56:33 +00:00
static uv_handle_t client;
static uv_req_t connect_req, write_req, timeout_req, shutdown_req;
static int nested = 0;
static int close_cb_called = 0;
static int connect_cb_called = 0;
static int write_cb_called = 0;
static int timeout_cb_called = 0;
static int bytes_received = 0;
static int shutdown_cb_called = 0;
2011-05-12 02:56:33 +00:00
static void close_cb(uv_handle_t* handle, int status) {
ASSERT(status == 0);
ASSERT(nested == 0 && "close_cb must be called from a fresh stack");
close_cb_called++;
}
2011-05-12 02:56:33 +00:00
static void shutdown_cb(uv_req_t* req, int status) {
ASSERT(status == 0);
ASSERT(nested == 0 && "shutdown_cb must be called from a fresh stack");
shutdown_cb_called++;
}
2011-05-13 14:15:02 +00:00
static void read_cb(uv_handle_t* handle, int nread, uv_buf_t buf) {
ASSERT(nested == 0 && "read_cb must be called from a fresh stack");
2011-05-04 20:56:50 +00:00
printf("Read. nread == %d\n", nread);
free(buf.base);
if (nread == 0) {
2011-05-12 02:56:33 +00:00
ASSERT(uv_last_error().code == UV_EAGAIN);
2011-05-04 20:56:50 +00:00
return;
} else if (nread == -1) {
2011-05-12 02:56:33 +00:00
ASSERT(uv_last_error().code == UV_EOF);
nested++;
2011-05-12 02:56:33 +00:00
if (uv_close(handle)) {
FATAL("uv_close failed");
}
nested--;
return;
}
bytes_received += nread;
/* 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 to call shutdown_cb immediately and *not* */
/* from a fresh stack. */
if (bytes_received == sizeof MESSAGE) {
nested++;
2011-05-12 02:56:33 +00:00
uv_req_init(&shutdown_req, handle, shutdown_cb);
2011-05-04 20:56:50 +00:00
puts("Shutdown");
2011-05-12 02:56:33 +00:00
if (uv_shutdown(&shutdown_req)) {
FATAL("uv_shutdown failed");
}
nested--;
}
}
2011-05-12 02:56:33 +00:00
static void timeout_cb(uv_req_t* req, int64_t skew, int status) {
ASSERT(status == 0);
ASSERT(nested == 0 && "timeout_cb must be called from a fresh stack");
2011-05-04 20:56:50 +00:00
puts("Timeout complete. Now read data...");
nested++;
2011-05-12 02:56:33 +00:00
if (uv_read_start(&client, read_cb)) {
FATAL("uv_read_start failed");
}
nested--;
timeout_cb_called++;
}
2011-05-12 02:56:33 +00:00
static void write_cb(uv_req_t* req, int status) {
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...");
/* 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 */
/* tempation for the backend to use dirty stack for calling read_cb. */
nested++;
2011-05-12 02:56:33 +00:00
uv_req_init(&timeout_req, NULL, timeout_cb);
if (uv_timeout(&timeout_req, 500)) {
FATAL("uv_timeout failed");
}
nested--;
write_cb_called++;
}
2011-05-12 02:56:33 +00:00
static void connect_cb(uv_req_t* req, int status) {
2011-05-13 14:15:02 +00:00
uv_buf_t buf;
2011-05-04 20:56:50 +00:00
puts("Connected. Write some data to echo server...");
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-05-12 02:56:33 +00:00
uv_req_init(&write_req, req->handle, write_cb);
2011-05-12 02:56:33 +00:00
if (uv_write(&write_req, &buf, 1)) {
FATAL("uv_write failed");
}
nested--;
connect_cb_called++;
}
2011-05-13 14:15:02 +00:00
static uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) {
uv_buf_t buf;
buf.len = size;
buf.base = (char*) malloc(size);
ASSERT(buf.base);
2011-05-03 00:35:11 +00:00
return buf;
}
TEST_IMPL(callback_stack) {
2011-05-12 02:56:33 +00:00
struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
2011-05-12 02:56:33 +00:00
uv_init(alloc_cb);
2011-04-07 02:51:59 +00:00
2011-05-12 02:56:33 +00:00
if (uv_tcp_init(&client, &close_cb, NULL)) {
FATAL("uv_tcp_init failed");
2011-04-15 17:27:09 +00:00
}
2011-05-04 20:56:50 +00:00
puts("Connecting...");
nested++;
2011-05-12 02:56:33 +00:00
uv_req_init(&connect_req, &client, connect_cb);
if (uv_connect(&connect_req, (struct sockaddr*) &addr)) {
FATAL("uv_connect failed");
2011-04-15 17:27:09 +00:00
}
nested--;
2011-05-12 02:56:33 +00:00
uv_run();
ASSERT(nested == 0);
ASSERT(connect_cb_called == 1 && "connect_cb must be called exactly once");
ASSERT(write_cb_called == 1 && "write_cb must be called exactly once");
ASSERT(timeout_cb_called == 1 && "timeout_cb must be called exactly once");
ASSERT(bytes_received == sizeof MESSAGE);
ASSERT(shutdown_cb_called == 1 && "shutdown_cb must be called exactly once");
ASSERT(close_cb_called == 1 && "close_cb must be called exactly once");
return 0;
2011-04-15 17:27:09 +00:00
}