libuv-svace-build
109 строк · 3.2 Кб
1/* Copyright libuv 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
25#include <stdio.h>26#include <stdlib.h>27#include <string.h>28
29static uv_tcp_t server;30static uv_tcp_t client;31static uv_tcp_t incoming;32static int connect_cb_called;33static int close_cb_called;34static int connection_cb_called;35
36
37static void close_cb(uv_handle_t* handle) {38close_cb_called++;39}
40
41static void incoming_close_cb(uv_handle_t* handle) {42uv_buf_t buf;43int r = 1;44
45close_cb_called++;46
47buf = uv_buf_init("meow", 4);48while (r > 0)49r = uv_try_write((uv_stream_t*) &client, &buf, 1);50fprintf(stderr, "uv_try_write error: %d %s\n", r, uv_strerror(r));51ASSERT(r == UV_EPIPE || r == UV_ECONNABORTED || r == UV_ECONNRESET);52ASSERT_OK(client.write_queue_size);53}
54
55
56static void connect_cb(uv_connect_t* req, int status) {57ASSERT_OK(status);58connect_cb_called++;59}
60
61
62static void connection_cb(uv_stream_t* tcp, int status) {63ASSERT_OK(status);64
65ASSERT_OK(uv_tcp_init(tcp->loop, &incoming));66ASSERT_OK(uv_accept(tcp, (uv_stream_t*) &incoming));67
68connection_cb_called++;69uv_close((uv_handle_t*) &incoming, incoming_close_cb);70uv_close((uv_handle_t*) tcp, close_cb);71}
72
73
74static void start_server(void) {75struct sockaddr_in addr;76
77ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));78
79ASSERT_OK(uv_tcp_init(uv_default_loop(), &server));80ASSERT_OK(uv_tcp_bind(&server, (struct sockaddr*) &addr, 0));81ASSERT_OK(uv_listen((uv_stream_t*) &server, 128, connection_cb));82}
83
84
85TEST_IMPL(tcp_try_write_error) {86uv_connect_t connect_req;87struct sockaddr_in addr;88
89start_server();90
91ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));92
93ASSERT_OK(uv_tcp_init(uv_default_loop(), &client));94ASSERT_OK(uv_tcp_connect(&connect_req,95&client,96(struct sockaddr*) &addr,97connect_cb));98
99ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));100uv_close((uv_handle_t*) &client, close_cb);101ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));102
103ASSERT_EQ(1, connect_cb_called);104ASSERT_EQ(3, close_cb_called);105ASSERT_EQ(1, connection_cb_called);106
107MAKE_VALGRIND_HAPPY(uv_default_loop());108return 0;109}
110