From dd9f2efeebf20cfa1def0ce92cb4e35a8b5c1580 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 10 Sep 2023 09:35:09 +0000 Subject: [PATCH 08/11] tests: use Time::HiRes `sleep' and `time' everywhere The time(2) syscall use by CORE::time is inaccurate[1]. It's also easier to read `sleep 0.01' rather than the longer `select' equivalent. [1] a6463151bd1db5b9 (httpdate: favor gettimeofday(2) over time(2) for correctness, 2023-06-01) --- t/active-unix-socket.t | 2 +- t/integration.t | 5 +++-- t/lib.perl | 4 ++-- t/reload-bad-config.t | 2 +- t/reopen-logs.t | 4 ++-- t/winch_ttin.t | 4 +--- 6 files changed, 10 insertions(+), 11 deletions(-) diff --git a/t/active-unix-socket.t b/t/active-unix-socket.t index 32cb0c2e..ff731b5f 100644 --- a/t/active-unix-socket.t +++ b/t/active-unix-socket.t @@ -86,7 +86,7 @@ is($pidf, $to_kill{u1}, 'pid file contents unchanged after 2nd start failure'); 'fail to connect to u1'); for (1..50) { # wait for init process to reap worker kill(0, $worker_pid) or last; - select(undef, undef, undef, 0.011); + sleep 0.011; } ok(!kill(0, $worker_pid), 'worker gone after parent dies'); } diff --git a/t/integration.t b/t/integration.t index eb40ffc7..80485e44 100644 --- a/t/integration.t +++ b/t/integration.t @@ -77,8 +77,9 @@ SKIP: { # Date header check eval { require HTTP::Date } or skip "HTTP::Date missing: $@", 1; $d[0] =~ s/^Date: //i or die 'BUG: did not strip date: prefix'; my $t = HTTP::Date::str2time($d[0]); - ok($t >= $t0 && $t > 0 && $t <= time, 'valid date') or - diag(explain([$t, $!, \@d])); + my $now = time; + ok($t >= ($t0 - 1) && $t > 0 && $t <= ($now + 1), 'valid date') or + diag(explain(["t=$t t0=$t0 now=$now", $!, \@d])); }; diff --git a/t/lib.perl b/t/lib.perl index 244972bc..9254b23b 100644 --- a/t/lib.perl +++ b/t/lib.perl @@ -6,7 +6,7 @@ use v5.14; use parent qw(Exporter); use autodie; use Test::More; -use Time::HiRes qw(sleep); +use Time::HiRes qw(sleep time); use IO::Socket::INET; use POSIX qw(dup2 _exit setpgid :signal_h SEEK_SET F_SETFD); use File::Temp 0.19 (); # 0.19 for ->newdir @@ -15,7 +15,7 @@ our ($tmpdir, $errfh, $err_log, $u_sock, $u_conf, $daemon_pid, our @EXPORT = qw(unicorn slurp tcp_server tcp_start unicorn $tmpdir $errfh $err_log $u_sock $u_conf $daemon_pid $pid_file SEEK_SET tcp_host_port which spawn check_stderr unix_start slurp_hdr - do_req stop_daemon); + do_req stop_daemon sleep time); my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!); $tmpdir = File::Temp->newdir("unicorn-$base-XXXX", TMPDIR => 1); diff --git a/t/reload-bad-config.t b/t/reload-bad-config.t index 543421da..c023b88c 100644 --- a/t/reload-bad-config.t +++ b/t/reload-bad-config.t @@ -38,7 +38,7 @@ my @l; for (1..1000) { @l = grep(/(?:done|error) reloading/, slurp($err_log)) and last; - select undef, undef, undef, 0.011; + sleep 0.011; } diag slurp($err_log) if $ENV{V}; ok(grep(/error reloading/, @l), 'got error reloading'); diff --git a/t/reopen-logs.t b/t/reopen-logs.t index 8a58c1b9..76a4dbdf 100644 --- a/t/reopen-logs.t +++ b/t/reopen-logs.t @@ -23,8 +23,8 @@ rename($out_log, "$out_log.rot"); $auto_reap->do_kill('USR1'); my $tries = 1000; -while (!-f $err_log && --$tries) { select undef, undef, undef, 0.01 }; -while (!-f $out_log && --$tries) { select undef, undef, undef, 0.01 }; +while (!-f $err_log && --$tries) { sleep 0.01 }; +while (!-f $out_log && --$tries) { sleep 0.01 }; ok(-f $out_log, 'stdout_path recreated after USR1'); ok(-f $err_log, 'stderr_path recreated after USR1'); diff --git a/t/winch_ttin.t b/t/winch_ttin.t index 509b118f..c5079599 100644 --- a/t/winch_ttin.t +++ b/t/winch_ttin.t @@ -43,9 +43,7 @@ ok(kill(0, $worker_pid), 'worker_pid is valid'); ok(kill('WINCH', $pid), 'SIGWINCH can be sent'); my $tries = 1000; -while (CORE::kill(0, $worker_pid) && --$tries) { - select undef, undef, undef, 0.01; -} +while (CORE::kill(0, $worker_pid) && --$tries) { sleep 0.01 } ok(!CORE::kill(0, $worker_pid), 'worker not running'); ok(kill('TTIN', $pid), 'SIGTTIN to restart worker');