libuv-svace-build
109 строк · 2.9 Кб
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 int connection_cb_called = 0;28static int close_cb_called = 0;29static int connect_cb_called = 0;30static uv_tcp_t server;31static uv_tcp_t client;32
33
34static void close_cb(uv_handle_t* handle) {35ASSERT_NOT_NULL(handle);36close_cb_called++;37}
38
39
40static void connection_cb(uv_stream_t* tcp, int status) {41ASSERT_OK(status);42uv_close((uv_handle_t*)&server, close_cb);43connection_cb_called++;44}
45
46
47static void start_server(void) {48struct sockaddr_in addr;49int r;50
51ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));52
53r = uv_tcp_init(uv_default_loop(), &server);54ASSERT_OK(r);55
56r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);57ASSERT_OK(r);58
59r = uv_listen((uv_stream_t*)&server, 128, connection_cb);60ASSERT_OK(r);61
62r = uv_listen((uv_stream_t*)&server, 128, connection_cb);63ASSERT_OK(r);64}
65
66
67static void connect_cb(uv_connect_t* req, int status) {68ASSERT_NOT_NULL(req);69ASSERT_OK(status);70free(req);71uv_close((uv_handle_t*)&client, close_cb);72connect_cb_called++;73}
74
75
76static void client_connect(void) {77struct sockaddr_in addr;78uv_connect_t* connect_req = malloc(sizeof *connect_req);79int r;80
81ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));82ASSERT_NOT_NULL(connect_req);83
84r = uv_tcp_init(uv_default_loop(), &client);85ASSERT_OK(r);86
87r = uv_tcp_connect(connect_req,88&client,89(const struct sockaddr*) &addr,90connect_cb);91ASSERT_OK(r);92}
93
94
95
96TEST_IMPL(multiple_listen) {97start_server();98
99client_connect();100
101uv_run(uv_default_loop(), UV_RUN_DEFAULT);102
103ASSERT_EQ(1, connection_cb_called);104ASSERT_EQ(1, connect_cb_called);105ASSERT_EQ(2, close_cb_called);106
107MAKE_VALGRIND_HAPPY(uv_default_loop());108return 0;109}
110