Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ unittest: prepare libhv
$(CC) -g -Wall -O0 -std=c99 -I. -Ibase -Iprotocol -Iutil -o bin/sendmail unittest/sendmail_test.c protocol/smtp.c base/hsocket.c base/htime.c util/base64.c
$(CC) -g -Wall -O0 -std=c99 -I. -Ibase -Issl -Ievent -o bin/hdns_test unittest/hdns_test.c -Llib -lhv -pthread
$(CC) -g -Wall -O0 -std=c99 -I. -Ibase -Issl -Ievent -o bin/hdns_benchmark unittest/hdns_benchmark.c -Llib -lhv -pthread
$(CC) -g -Wall -O0 -std=c99 -I. -Ibase -Issl -Ievent -o bin/tcp_eof_drain_test unittest/tcp_eof_drain_test.c -Llib -lhv -pthread
ifeq ($(WITH_EVPP), yes)
$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Issl -Ievent -Icpputil -Ievpp -o bin/tcpclient_dns_test unittest/tcpclient_dns_test.cpp -Llib -lhv -pthread
endif
Expand Down
10 changes: 10 additions & 0 deletions event/nio.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,16 @@ static void nio_read(hio_t* io) {
}
}
if (nread == 0 && (io->io_type & HIO_TYPE_SOCK_STREAM)) {
if (io->io_type == HIO_TYPE_TCP) {
// FIN stays readable, but the peer can still receive queued writes.
if (hio_read_stop(io) != 0) {
io->error = socket_errno();
}
else if (io->close && io->error == 0) {
// A graceful close is already draining with its own timeout.
return;
}
}
goto disconnect;
}
if (nread < len) {
Expand Down
1 change: 1 addition & 0 deletions scripts/unittest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ fi
if [ -x bin/lua_mqtt_test ]; then
bin/lua_mqtt_test || exit $?
fi
bin/tcp_eof_drain_test || exit $?
if [ -x bin/hdns_test ]; then
bin/hdns_test
fi
Expand Down
6 changes: 6 additions & 0 deletions unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ add_executable(hdns_benchmark hdns_benchmark.c)
target_include_directories(hdns_benchmark PRIVATE .. ../base ../ssl ../event)
target_link_libraries(hdns_benchmark ${HV_LIBRARIES})

# ------event: graceful TCP close------
add_executable(tcp_eof_drain_test tcp_eof_drain_test.c)
target_include_directories(tcp_eof_drain_test PRIVATE .. ../base ../ssl ../event)
target_link_libraries(tcp_eof_drain_test ${HV_LIBRARIES})

# ------redis------
if(WITH_EVPP AND WITH_REDIS)
add_executable(redis_protocol_test redis_protocol_test.cpp ../redis/RedisMessage.cpp)
Expand Down Expand Up @@ -219,6 +224,7 @@ add_custom_target(unittest DEPENDS
hmutex_test
connect_test
socketpair_test
tcp_eof_drain_test
base64
md5
sha1
Expand Down
84 changes: 84 additions & 0 deletions unittest/tcp_eof_drain_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "hloop.h"
#include "hsocket.h"

static char response[512 * 1024];
static size_t received;
static int server_closes;

static void require(int condition, const char* message) {
if (!condition) {
fprintf(stderr, "tcp_eof_drain_test: %s\n", message);
exit(EXIT_FAILURE);
}
}

static void on_read(hio_t* peer, void* data, int len) {
(void)peer;
require(received + len <= sizeof(response) && memcmp(response + received, data, len) == 0, "unexpected response bytes");
received += len;
}

static void on_server_close(hio_t* io) {
require(hio_error(io) == 0, "server closed with an error");
++server_closes;
}

static void on_peer_close(hio_t* peer) {
require(hio_error(peer) == 0, "peer closed with an error");
hloop_stop(hevent_loop(peer));
}

static void on_timeout(htimer_t* timer) {
(void)timer;
require(0, "response did not drain before timeout");
}

static int check_drain(int close_before_eof) {
hloop_t* loop = hloop_new(0);
int listener = Listen(0, "127.0.0.1");
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
require(listener >= 0 && getsockname(listener, (struct sockaddr*)&addr, &addrlen) == 0, "could not listen on loopback");
int client = socket(AF_INET, SOCK_STREAM, 0);
require(client >= 0 && so_rcvbuf(client, 4096) == 0, "could not set up client");
require(connect(client, (struct sockaddr*)&addr, addrlen) == 0, "could not connect client");
int server = accept(listener, NULL, NULL);
closesocket(listener);
require(server >= 0 && so_sndbuf(server, 4096) == 0, "could not set up server");

// Wait for the client's FIN before either endpoint starts processing events.
#ifdef OS_WIN
require(shutdown(client, SD_SEND) == 0, "client half-close failed");
#else
require(shutdown(client, SHUT_WR) == 0, "client half-close failed");
#endif
char byte;
require(recv(server, &byte, 1, MSG_PEEK) == 0, "server did not receive EOF");

hio_t* io = hio_get(loop, server);
hio_t* peer = hio_get(loop, client);
received = 0;
server_closes = 0;
hio_setcb_close(io, on_server_close);
hio_setcb_read(peer, on_read);
hio_setcb_close(peer, on_peer_close);
require(hio_read(io) == 0 && hio_read(peer) == 0, "could not watch both sockets");
require(hio_write(io, response, sizeof(response)) >= 0 && hio_write_bufsize(io) > 0, "response was not queued");
if (close_before_eof) hio_close(io);
htimer_add(loop, on_timeout, 10000, 1);
hloop_run(loop);
hloop_free(&loop);
if (received != sizeof(response) || server_closes != 1) {
fprintf(stderr, "%s: received %zu of %zu bytes, server closed %d times\n", close_before_eof ? "local close first" : "EOF first", received,
sizeof(response), server_closes);
return 1;
}
return 0;
}

int main(void) {
for (size_t i = 0; i < sizeof(response); ++i) response[i] = (char)(i % 251);
int errors = check_drain(0);
errors += check_drain(1);
return errors ? EXIT_FAILURE : EXIT_SUCCESS;
}
Loading