libuv-svace-build
187 строк · 5.1 Кб
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#include "uv.h"23#include "task.h"24#include <stdio.h>25#include <stdlib.h>26
27static uv_timer_t timer;28static uv_tcp_t tcp;29static uv_connect_t connect_req;30static uv_write_t write_req;31static uv_shutdown_t shutdown_req;32static uv_buf_t qbuf;33static int got_q;34static int got_eof;35static int called_connect_cb;36static int called_shutdown_cb;37static int called_tcp_close_cb;38static int called_timer_close_cb;39static int called_timer_cb;40
41
42static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {43buf->base = malloc(size);44buf->len = size;45}
46
47
48static void read_cb(uv_stream_t* t, ssize_t nread, const uv_buf_t* buf) {49ASSERT_PTR_EQ((uv_tcp_t*)t, &tcp);50
51if (nread == 0) {52free(buf->base);53return;54}55
56if (!got_q) {57ASSERT_EQ(1, nread);58ASSERT(!got_eof);59ASSERT_EQ(buf->base[0], 'Q');60free(buf->base);61got_q = 1;62puts("got Q");63} else {64ASSERT_EQ(nread, UV_EOF);65if (buf->base) {66free(buf->base);67}68got_eof = 1;69puts("got EOF");70}71}
72
73
74static void shutdown_cb(uv_shutdown_t *req, int status) {75ASSERT_PTR_EQ(req, &shutdown_req);76
77ASSERT_EQ(1, called_connect_cb);78ASSERT(!got_eof);79ASSERT_OK(called_tcp_close_cb);80ASSERT_OK(called_timer_close_cb);81ASSERT_OK(called_timer_cb);82
83called_shutdown_cb++;84}
85
86
87static void connect_cb(uv_connect_t *req, int status) {88ASSERT_OK(status);89ASSERT_PTR_EQ(req, &connect_req);90
91/* Start reading from our connection so we can receive the EOF. */92ASSERT_OK(uv_read_start((uv_stream_t*)&tcp, alloc_cb, read_cb));93
94/* Check error handling. */95ASSERT_EQ(UV_EALREADY, uv_read_start((uv_stream_t*)&tcp, alloc_cb, read_cb));96ASSERT_EQ(UV_EINVAL, uv_read_start(NULL, alloc_cb, read_cb));97ASSERT_EQ(UV_EINVAL, uv_read_start((uv_stream_t*)&tcp, NULL, read_cb));98ASSERT_EQ(UV_EINVAL, uv_read_start((uv_stream_t*)&tcp, alloc_cb, NULL));99
100/*101* Write the letter 'Q' to gracefully kill the echo-server. This will not
102* effect our connection.
103*/
104uv_write(&write_req, (uv_stream_t*) &tcp, &qbuf, 1, NULL);105
106/* Shutdown our end of the connection. */107uv_shutdown(&shutdown_req, (uv_stream_t*) &tcp, shutdown_cb);108
109called_connect_cb++;110ASSERT_OK(called_shutdown_cb);111}
112
113
114static void tcp_close_cb(uv_handle_t* handle) {115ASSERT_PTR_EQ(handle, (uv_handle_t*) &tcp);116
117ASSERT_EQ(1, called_connect_cb);118ASSERT(got_q);119ASSERT(got_eof);120ASSERT_EQ(1, called_timer_cb);121
122called_tcp_close_cb++;123}
124
125
126static void timer_close_cb(uv_handle_t* handle) {127ASSERT_PTR_EQ(handle, (uv_handle_t*) &timer);128called_timer_close_cb++;129}
130
131
132static void timer_cb(uv_timer_t* handle) {133ASSERT_PTR_EQ(handle, &timer);134uv_close((uv_handle_t*) handle, timer_close_cb);135
136/*137* The most important assert of the test: we have not received
138* tcp_close_cb yet.
139*/
140ASSERT_OK(called_tcp_close_cb);141uv_close((uv_handle_t*) &tcp, tcp_close_cb);142
143called_timer_cb++;144}
145
146
147/*
148* This test has a client which connects to the echo_server and immediately
149* issues a shutdown. The echo-server, in response, will also shutdown their
150* connection. We check, with a timer, that libuv is not automatically
151* calling uv_close when the client receives the EOF from echo-server.
152*/
153TEST_IMPL(shutdown_eof) {154struct sockaddr_in server_addr;155int r;156
157qbuf.base = "Q";158qbuf.len = 1;159
160r = uv_timer_init(uv_default_loop(), &timer);161ASSERT_OK(r);162
163uv_timer_start(&timer, timer_cb, 100, 0);164
165ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));166r = uv_tcp_init(uv_default_loop(), &tcp);167ASSERT(!r);168
169r = uv_tcp_connect(&connect_req,170&tcp,171(const struct sockaddr*) &server_addr,172connect_cb);173ASSERT(!r);174
175uv_run(uv_default_loop(), UV_RUN_DEFAULT);176
177ASSERT_EQ(1, called_connect_cb);178ASSERT_EQ(1, called_shutdown_cb);179ASSERT(got_eof);180ASSERT(got_q);181ASSERT_EQ(1, called_tcp_close_cb);182ASSERT_EQ(1, called_timer_close_cb);183ASSERT_EQ(1, called_timer_cb);184
185MAKE_VALGRIND_HAPPY(uv_default_loop());186return 0;187}
188
189