libuv-svace-build
209 строк · 5.5 Кб
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/*
23* TODO: Add explanation of why we want on_close to be called from fresh
24* stack.
25*/
26
27#include "uv.h"28#include "task.h"29
30
31static const char MESSAGE[] = "Failure is for the weak. Everyone dies alone.";32
33static uv_tcp_t client;34static uv_timer_t timer;35static uv_connect_t connect_req;36static uv_write_t write_req;37static uv_shutdown_t shutdown_req;38
39static int nested = 0;40static int close_cb_called = 0;41static int connect_cb_called = 0;42static int write_cb_called = 0;43static int timer_cb_called = 0;44static int bytes_received = 0;45static int shutdown_cb_called = 0;46
47
48static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {49buf->len = size;50buf->base = malloc(size);51ASSERT_NOT_NULL(buf->base);52}
53
54
55static void close_cb(uv_handle_t* handle) {56ASSERT(nested == 0 && "close_cb must be called from a fresh stack");57
58close_cb_called++;59}
60
61
62static void shutdown_cb(uv_shutdown_t* req, int status) {63ASSERT_OK(status);64ASSERT(nested == 0 && "shutdown_cb must be called from a fresh stack");65
66shutdown_cb_called++;67}
68
69
70static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {71ASSERT(nested == 0 && "read_cb must be called from a fresh stack");72
73printf("Read. nread == %d\n", (int)nread);74free(buf->base);75
76if (nread == 0) {77return;78
79} else if (nread < 0) {80ASSERT_EQ(nread, UV_EOF);81
82nested++;83uv_close((uv_handle_t*)tcp, close_cb);84nested--;85
86return;87}88
89bytes_received += nread;90
91/* We call shutdown here because when bytes_received == sizeof MESSAGE there92* will be no more data sent nor received, so here it would be possible for a
93* backend to call shutdown_cb immediately and *not* from a fresh stack. */
94if (bytes_received == sizeof MESSAGE) {95nested++;96
97puts("Shutdown");98
99if (uv_shutdown(&shutdown_req, (uv_stream_t*)tcp, shutdown_cb)) {100FATAL("uv_shutdown failed");101}102nested--;103}104}
105
106
107static void timer_cb(uv_timer_t* handle) {108ASSERT_PTR_EQ(handle, &timer);109ASSERT(nested == 0 && "timer_cb must be called from a fresh stack");110
111puts("Timeout complete. Now read data...");112
113nested++;114if (uv_read_start((uv_stream_t*)&client, alloc_cb, read_cb)) {115FATAL("uv_read_start failed");116}117nested--;118
119timer_cb_called++;120
121uv_close((uv_handle_t*)handle, close_cb);122}
123
124
125static void write_cb(uv_write_t* req, int status) {126int r;127
128ASSERT_OK(status);129ASSERT(nested == 0 && "write_cb must be called from a fresh stack");130
131puts("Data written. 500ms timeout...");132
133/* After the data has been sent, we're going to wait for a while, then start134* reading. This makes us certain that the message has been echoed back to
135* our receive buffer when we start reading. This maximizes the temptation
136* for the backend to use dirty stack for calling read_cb. */
137nested++;138r = uv_timer_init(uv_default_loop(), &timer);139ASSERT_OK(r);140r = uv_timer_start(&timer, timer_cb, 500, 0);141ASSERT_OK(r);142nested--;143
144write_cb_called++;145}
146
147
148static void connect_cb(uv_connect_t* req, int status) {149uv_buf_t buf;150
151puts("Connected. Write some data to echo server...");152
153ASSERT_OK(status);154ASSERT(nested == 0 && "connect_cb must be called from a fresh stack");155
156nested++;157
158buf.base = (char*) &MESSAGE;159buf.len = sizeof MESSAGE;160
161if (uv_write(&write_req, (uv_stream_t*)req->handle, &buf, 1, write_cb)) {162FATAL("uv_write failed");163}164
165nested--;166
167connect_cb_called++;168}
169
170
171TEST_IMPL(callback_stack) {172struct sockaddr_in addr;173
174ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));175
176if (uv_tcp_init(uv_default_loop(), &client)) {177FATAL("uv_tcp_init failed");178}179
180puts("Connecting...");181
182nested++;183
184if (uv_tcp_connect(&connect_req,185&client,186(const struct sockaddr*) &addr,187connect_cb)) {188FATAL("uv_tcp_connect failed");189}190nested--;191
192uv_run(uv_default_loop(), UV_RUN_DEFAULT);193
194ASSERT_OK(nested);195ASSERT_NE(connect_cb_called == 1 && \196"connect_cb must be called exactly once", 0);197ASSERT_NE(write_cb_called == 1 && "write_cb must be called exactly once",1980);199ASSERT_NE(timer_cb_called == 1 && "timer_cb must be called exactly once",2000);201ASSERT_EQ(bytes_received, sizeof MESSAGE);202ASSERT_NE(shutdown_cb_called == 1 && \203"shutdown_cb must be called exactly once", 0);204ASSERT_NE(close_cb_called == 2 && "close_cb must be called exactly twice",2050);206
207MAKE_VALGRIND_HAPPY(uv_default_loop());208return 0;209}
210