Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Fix bugs in test-udp-send-and-recv and benchmark-udp-packet-storm

  • Loading branch information...
piscisaureus committed Aug 24, 2011
1 parent 1870c18 commit 36c9b791d4b37de7312f623c7c6a736e46642760
Showing with 55 additions and 46 deletions.
  1. +5 −3 test/benchmark-udp-packet-storm.c
  2. +50 −43 test/test-udp-send-and-recv.c
@@ -46,6 +46,7 @@ static uv_buf_t bufs[5];
static int send_cb_called;
static int recv_cb_called;
static int close_cb_called;
static int stopping = 0;

typedef struct {
struct sockaddr_in addr;
@@ -63,8 +64,7 @@ static void send_cb(uv_udp_send_t* req, int status) {
sender_state_t* ss;
int r;

if (status == -1) {
ASSERT(uv_last_error().code == UV_EINTR); /* FIXME change error code */
if (stopping) {
return;
}

@@ -111,6 +111,8 @@ static void close_cb(uv_handle_t* handle) {
static void timeout_cb(uv_timer_t* timer, int status) {
int i;

stopping = 1;

for (i = 0; i < n_senders_; i++)
uv_close((uv_handle_t*)&senders[i], close_cb);

@@ -175,7 +177,7 @@ static int do_packet_storm(int n_senders, int n_receivers) {
req = malloc(sizeof(*req) + sizeof(*ss));

ss = (void*)(req + 1);
ss->addr = uv_ip4_addr("0.0.0.0", BASE_PORT + (i % n_receivers));
ss->addr = uv_ip4_addr("127.0.0.1", BASE_PORT + (i % n_receivers));

r = uv_udp_send(req, handle, bufs, ARRAY_SIZE(bufs), ss->addr, send_cb);
ASSERT(r == 0);
@@ -65,20 +65,24 @@ static void cl_recv_cb(uv_udp_t* handle,
CHECK_HANDLE(handle);
ASSERT(flags == 0);

if (nread < 0) {
ASSERT(0 && "unexpected error");
}

if (nread == 0) {
/* Returning unused buffer */
/* Don't count towards cl_recv_cb_called */
ASSERT(addr == NULL);
uv_close((uv_handle_t*)handle, close_cb);
}
else if (nread > 0) {
ASSERT(addr != NULL);
ASSERT(nread == 4);
ASSERT(!memcmp("PONG", buf.base, nread));
}
else {
ASSERT(0 && "unexpected error");
return;
}

ASSERT(addr != NULL);
ASSERT(nread == 4);
ASSERT(!memcmp("PONG", buf.base, nread));

cl_recv_cb_called++;

uv_close((uv_handle_t*) handle, close_cb);
}


@@ -101,7 +105,7 @@ static void sv_send_cb(uv_udp_send_t* req, int status) {
ASSERT(status == 0);
CHECK_HANDLE(req->handle);

uv_close((uv_handle_t*)req->handle, close_cb);
uv_close((uv_handle_t*) req->handle, close_cb);
free(req);

sv_send_cb_called++;
@@ -116,40 +120,43 @@ static void sv_recv_cb(uv_udp_t* handle,
uv_udp_send_t* req;
int r;

CHECK_HANDLE(handle);
ASSERT(flags == 0);

if (nread > 0) {
ASSERT(addr != NULL);
ASSERT(nread == 4);
ASSERT(!memcmp("PING", buf.base, nread));

/* FIXME? `uv_udp_recv_stop` does what it says: recv_cb is not called
* anymore. That's problematic because the read buffer won't be returned
* either... Not sure I like that but it's consistent with `uv_read_stop`.
*/
r = uv_udp_recv_stop(handle);
ASSERT(r == 0);

req = malloc(sizeof *req);
ASSERT(req != NULL);

buf = uv_buf_init("PONG", 4);

r = uv_udp_send(req,
handle,
&buf,
1,
*(struct sockaddr_in*)addr,
sv_send_cb);
ASSERT(r == 0);
if (nread < 0) {
ASSERT(0 && "unexpected error");
}
else if (nread == 0) {

if (nread == 0) {
/* Returning unused buffer */
/* Don't count towards sv_recv_cb_called */
ASSERT(addr == NULL);
return;
}
else {
ASSERT(0 && "unexpected error");
}

CHECK_HANDLE(handle);
ASSERT(flags == 0);

ASSERT(addr != NULL);
ASSERT(nread == 4);
ASSERT(!memcmp("PING", buf.base, nread));

/* FIXME? `uv_udp_recv_stop` does what it says: recv_cb is not called
* anymore. That's problematic because the read buffer won't be returned
* either... Not sure I like that but it's consistent with `uv_read_stop`.
*/
r = uv_udp_recv_stop(handle);
ASSERT(r == 0);

req = malloc(sizeof *req);
ASSERT(req != NULL);

buf = uv_buf_init("PONG", 4);

r = uv_udp_send(req,
handle,
&buf,
1,
*(struct sockaddr_in*)addr,
sv_send_cb);
ASSERT(r == 0);

sv_recv_cb_called++;
}
@@ -194,9 +201,9 @@ TEST_IMPL(udp_send_and_recv) {
uv_run();

ASSERT(cl_send_cb_called == 1);
ASSERT(cl_recv_cb_called == 2); /* dgram + EOF == 2 */
ASSERT(cl_recv_cb_called == 1);
ASSERT(sv_send_cb_called == 1);
ASSERT(sv_recv_cb_called == 1); /* dgram, no EOF == 1 */
ASSERT(sv_recv_cb_called == 1);
ASSERT(close_cb_called == 2);

return 0;

0 comments on commit 36c9b79

Please sign in to comment.
You can’t perform that action at this time.