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

This commit is contained in:
Bert Belder 2011-08-24 05:16:34 +02:00
parent 1870c18f3c
commit 36c9b791d4
2 changed files with 52 additions and 43 deletions

View File

@ -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);

View File

@ -65,20 +65,24 @@ static void cl_recv_cb(uv_udp_t* handle,
CHECK_HANDLE(handle);
ASSERT(flags == 0);
if (nread == 0) {
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 {
if (nread < 0) {
ASSERT(0 && "unexpected error");
}
if (nread == 0) {
/* Returning unused buffer */
/* Don't count towards cl_recv_cb_called */
ASSERT(addr == NULL);
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;
if (nread < 0) {
ASSERT(0 && "unexpected error");
}
if (nread == 0) {
/* Returning unused buffer */
/* Don't count towards sv_recv_cb_called */
ASSERT(addr == NULL);
return;
}
CHECK_HANDLE(handle);
ASSERT(flags == 0);
if (nread > 0) {
ASSERT(addr != NULL);
ASSERT(nread == 4);
ASSERT(!memcmp("PING", buf.base, nread));
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);
/* 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);
req = malloc(sizeof *req);
ASSERT(req != NULL);
buf = uv_buf_init("PONG", 4);
buf = uv_buf_init("PONG", 4);
r = uv_udp_send(req,
handle,
&buf,
1,
*(struct sockaddr_in*)addr,
sv_send_cb);
ASSERT(r == 0);
}
else if (nread == 0) {
ASSERT(addr == NULL);
}
else {
ASSERT(0 && "unexpected error");
}
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;