libuv-svace-build
135 строк · 3.9 Кб
1/* Copyright libuv project and 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_tcp_t tcp;28static uv_connect_t connect_req;29static uv_shutdown_t shutdown_req;30static uv_buf_t qbuf;31static int got_q;32static int got_eof;33static int called_connect_cb;34static int called_shutdown_cb;35static int called_tcp_close_cb;36
37
38static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {39buf->base = malloc(size);40buf->len = size;41}
42
43
44static void shutdown_cb(uv_shutdown_t *req, int status) {45ASSERT_PTR_EQ(req, &shutdown_req);46
47ASSERT_EQ(1, called_connect_cb);48ASSERT_OK(called_tcp_close_cb);49}
50
51
52static void read_cb(uv_stream_t* t, ssize_t nread, const uv_buf_t* buf) {53ASSERT_PTR_EQ((uv_tcp_t*)t, &tcp);54
55if (nread == 0) {56free(buf->base);57return;58}59
60if (!got_q) {61ASSERT_EQ(4, nread);62ASSERT_OK(got_eof);63ASSERT_MEM_EQ(buf->base, "QQSS", 4);64free(buf->base);65got_q = 1;66puts("got QQSS");67/* Shutdown our end of the connection simultaneously */68uv_shutdown(&shutdown_req, (uv_stream_t*) &tcp, shutdown_cb);69called_shutdown_cb++;70} else {71ASSERT_EQ(nread, UV_EOF);72if (buf->base) {73free(buf->base);74}75got_eof = 1;76puts("got EOF");77}78}
79
80
81static void connect_cb(uv_connect_t *req, int status) {82ASSERT_OK(status);83ASSERT_PTR_EQ(req, &connect_req);84
85/* Start reading from our connection so we can receive the EOF. */86ASSERT_OK(uv_read_start((uv_stream_t*)&tcp, alloc_cb, read_cb));87
88/* Check error handling. */89ASSERT_EQ(UV_EALREADY, uv_read_start((uv_stream_t*)&tcp, alloc_cb, read_cb));90ASSERT_EQ(UV_EINVAL, uv_read_start(NULL, alloc_cb, read_cb));91ASSERT_EQ(UV_EINVAL, uv_read_start((uv_stream_t*)&tcp, NULL, read_cb));92ASSERT_EQ(UV_EINVAL, uv_read_start((uv_stream_t*)&tcp, alloc_cb, NULL));93
94/*95* Write the letter 'Q' and 'QSS` to gracefully kill the echo-server. This
96* will not effect our connection.
97*/
98ASSERT_EQ(qbuf.len, uv_try_write((uv_stream_t*) &tcp, &qbuf, 1));99
100called_connect_cb++;101ASSERT_OK(called_shutdown_cb);102}
103
104
105/*
106* This test has a client which connects to the echo_server and immediately
107* issues a shutdown. We check that this does not cause libuv to hang.
108*/
109TEST_IMPL(shutdown_simultaneous) {110struct sockaddr_in server_addr;111int r;112
113qbuf.base = "QQSS";114qbuf.len = 4;115
116ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));117r = uv_tcp_init(uv_default_loop(), &tcp);118ASSERT_OK(r);119
120r = uv_tcp_connect(&connect_req,121&tcp,122(const struct sockaddr*) &server_addr,123connect_cb);124ASSERT_OK(r);125
126uv_run(uv_default_loop(), UV_RUN_DEFAULT);127
128ASSERT_EQ(1, called_connect_cb);129ASSERT_EQ(1, called_shutdown_cb);130ASSERT_EQ(1, got_eof);131ASSERT_EQ(1, got_q);132
133MAKE_VALGRIND_HAPPY(uv_default_loop());134return 0;135}
136