Skip to content

Commit

Permalink
test/regress: cover a polling of invalid fd
Browse files Browse the repository at this point in the history
Test that an event's callback is called if the fd is closed prior to being
polled for activity.

azat: make it run only for poll backend/method, and do not close fd
twice
  • Loading branch information
thentenaar authored and azat committed Nov 4, 2016
1 parent 675974c commit cb0df5c
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions test/regress.c
Original file line number Diff line number Diff line change
Expand Up @@ -2836,15 +2836,47 @@ test_event_pending(void *ptr)
}
}

#ifndef _WIN32
/* You can't do this test on windows, since dup2 doesn't work on sockets */

static void
dfd_cb(evutil_socket_t fd, short e, void *data)
{
*(int*)data = (int)e;
}

static void
test_event_closed_fd_poll(void *arg)
{
struct timeval tv;
struct event *e;
struct basic_test_data *data = (struct basic_test_data *)arg;
int i = 0;

if (strcmp(event_base_get_method(data->base), "poll")) {
tinytest_set_test_skipped_();
return;
}

e = event_new(data->base, data->pair[0], EV_READ, dfd_cb, &i);
tt_assert(e);

tv.tv_sec = 0;
tv.tv_usec = 500 * 1000;
event_add(e, &tv);
tt_assert(event_pending(e, EV_READ, NULL));
close(data->pair[0]);
data->pair[0] = -1; /** avoids double-close */
event_base_loop(data->base, EVLOOP_ONCE);
tt_int_op(i, ==, EV_READ);

end:
if (e) {
event_del(e);
event_free(e);
}
}

#ifndef _WIN32
/* You can't do this test on windows, since dup2 doesn't work on sockets */

/* Regression test for our workaround for a fun epoll/linux related bug
* where fd2 = dup(fd1); add(fd2); close(fd2); dup2(fd1,fd2); add(fd2)
* will get you an EEXIST */
Expand Down Expand Up @@ -3381,6 +3413,9 @@ struct testcase_t main_testcases[] = {
{ "event_once_never", test_event_once_never, TT_ISOLATED, &basic_setup, NULL },
{ "event_pending", test_event_pending, TT_ISOLATED, &basic_setup,
NULL },
{ "event_closed_fd_poll", test_event_closed_fd_poll, TT_ISOLATED, &basic_setup,
NULL },

#ifndef _WIN32
{ "dup_fd", test_dup_fd, TT_ISOLATED, &basic_setup, NULL },
#endif
Expand Down

0 comments on commit cb0df5c

Please sign in to comment.