misc: Fix tst-select timeout handling (BZ#27648)

Instead of polling the stderr, create two pipes and fork to check
if child timeout as expected similar to tst-pselect.c.  Also lower
the timeout value.

Checked on x86_64-linux-gnu.
This commit is contained in:
Adhemerval Zanella 2021-03-25 16:57:45 -03:00
parent 850dbf24ee
commit 1b53b5d970

View File

@ -16,54 +16,79 @@
License along with the GNU C Library; if not, see License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */ <https://www.gnu.org/licenses/>. */
#include <time.h>
#include <errno.h> #include <errno.h>
#include <stdbool.h> #include <support/capture_subprocess.h>
#include <sys/select.h>
#include <support/check.h> #include <support/check.h>
#include <support/xtime.h>
#include <support/timespec.h> #include <support/timespec.h>
#include <support/xunistd.h>
#include <support/xtime.h>
#define TST_SELECT_TIMEOUT 1 struct child_args
#define TST_SELECT_FD_ERR 2
static int
test_select_timeout (bool zero_tmo)
{ {
const int fds = TST_SELECT_FD_ERR; int fds[2][2];
int timeout = TST_SELECT_TIMEOUT; struct timeval tmo;
struct timeval to = { 0, 0 }; };
struct timespec ts;
static void
do_test_child (void *clousure)
{
struct child_args *args = (struct child_args *) clousure;
close (args->fds[0][1]);
close (args->fds[1][0]);
fd_set rfds; fd_set rfds;
FD_ZERO (&rfds); FD_ZERO (&rfds);
FD_SET (fds, &rfds); FD_SET (args->fds[0][0], &rfds);
if (zero_tmo) struct timespec ts = xclock_now (CLOCK_REALTIME);
timeout = 0; ts = timespec_add (ts, (struct timespec) { args->tmo.tv_sec, 0 });
to.tv_sec = timeout; int r = select (args->fds[0][0] + 1, &rfds, NULL, NULL, &args->tmo);
ts = xclock_now (CLOCK_REALTIME); TEST_COMPARE (r, 0);
ts = timespec_add (ts, (struct timespec) { timeout, 0 });
/* Wait for timeout. */
int ret = select (fds + 1, &rfds, NULL, NULL, &to);
if (ret == -1)
FAIL_EXIT1 ("select failed: %m\n");
TEST_TIMESPEC_NOW_OR_AFTER (CLOCK_REALTIME, ts); TEST_TIMESPEC_NOW_OR_AFTER (CLOCK_REALTIME, ts);
return 0; xwrite (args->fds[1][1], "foo", 3);
} }
static int static int
do_test (void) do_test (void)
{ {
/* Check if select exits immediately. */ struct child_args args;
test_select_timeout (true);
/* Check if select exits after specified timeout. */ xpipe (args.fds[0]);
test_select_timeout (false); xpipe (args.fds[1]);
/* The child select should timeout and write on its pipe end. */
args.tmo = (struct timeval) { .tv_sec = 0, .tv_usec = 250000 };
{
struct support_capture_subprocess result;
result = support_capture_subprocess (do_test_child, &args);
support_capture_subprocess_check (&result, "tst-select-child", 0,
sc_allow_none);
}
/* Same as before, but simulating polling. */
args.tmo = (struct timeval) { .tv_sec = 0, .tv_usec = 0 };
{
struct support_capture_subprocess result;
result = support_capture_subprocess (do_test_child, &args);
support_capture_subprocess_check (&result, "tst-select-child", 0,
sc_allow_none);
}
xclose (args.fds[0][0]);
xclose (args.fds[1][1]);
{
fd_set rfds;
FD_ZERO (&rfds);
FD_SET (args.fds[1][0], &rfds);
int r = select (args.fds[1][0] + 1, &rfds, NULL, NULL, &args.tmo);
TEST_COMPARE (r, 1);
}
return 0; return 0;
} }