Skip to content

Commit f2698ea

Browse files
committed
Introduce PG_TEST_TIMEOUT_DEFAULT for TAP suite non-elapsing timeouts.
Slow hosts may avoid load-induced, spurious failures by setting environment variable PG_TEST_TIMEOUT_DEFAULT to some number of seconds greater than 180. Developers may see faster failures by setting that environment variable to some lesser number of seconds. In tests, write $PostgreSQL::Test::Utils::timeout_default wherever the convention has been to write 180. This change raises the default for some briefer timeouts. Back-patch to v10 (all supported versions). Discussion: https://postgr.es/m/[email protected]
1 parent 9240589 commit f2698ea

20 files changed

+75
-65
lines changed

contrib/amcheck/t/002_cic.pl

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
#
1919
$node = PostgreSQL::Test::Cluster->new('CIC_test');
2020
$node->init;
21-
$node->append_conf('postgresql.conf', 'lock_timeout = 180000');
21+
$node->append_conf('postgresql.conf',
22+
'lock_timeout = ' . (1000 * $PostgreSQL::Test::Utils::timeout_default));
2223
$node->start;
2324
$node->safe_psql('postgres', q(CREATE EXTENSION amcheck));
2425
$node->safe_psql('postgres', q(CREATE TABLE tbl(i int)));

contrib/amcheck/t/003_cic_2pc.pl

+8-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
$node = PostgreSQL::Test::Cluster->new('CIC_2PC_test');
2323
$node->init;
2424
$node->append_conf('postgresql.conf', 'max_prepared_transactions = 10');
25-
$node->append_conf('postgresql.conf', 'lock_timeout = 180000');
25+
$node->append_conf('postgresql.conf',
26+
'lock_timeout = ' . (1000 * $PostgreSQL::Test::Utils::timeout_default));
2627
$node->start;
2728
$node->safe_psql('postgres', q(CREATE EXTENSION amcheck));
2829
$node->safe_psql('postgres', q(CREATE TABLE tbl(i int)));
@@ -38,7 +39,7 @@
3839

3940
my $main_in = '';
4041
my $main_out = '';
41-
my $main_timer = IPC::Run::timeout(180);
42+
my $main_timer = IPC::Run::timeout($PostgreSQL::Test::Utils::timeout_default);
4243

4344
my $main_h =
4445
$node->background_psql('postgres', \$main_in, \$main_out,
@@ -52,7 +53,7 @@
5253

5354
my $cic_in = '';
5455
my $cic_out = '';
55-
my $cic_timer = IPC::Run::timeout(180);
56+
my $cic_timer = IPC::Run::timeout($PostgreSQL::Test::Utils::timeout_default);
5657
my $cic_h =
5758
$node->background_psql('postgres', \$cic_in, \$cic_out,
5859
$cic_timer, on_error_stop => 1);
@@ -113,9 +114,10 @@
113114
));
114115
$node->restart;
115116

116-
my $reindex_in = '';
117-
my $reindex_out = '';
118-
my $reindex_timer = IPC::Run::timeout(180);
117+
my $reindex_in = '';
118+
my $reindex_out = '';
119+
my $reindex_timer =
120+
IPC::Run::timeout($PostgreSQL::Test::Utils::timeout_default);
119121
my $reindex_h =
120122
$node->background_psql('postgres', \$reindex_in, \$reindex_out,
121123
$reindex_timer, on_error_stop => 1);

src/bin/pg_ctl/t/004_logrotate.pl

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ sub check_log_pattern
3939
my $node = shift;
4040
my $lfname = fetch_file_name($logfiles, $format);
4141

42-
my $max_attempts = 180 * 10;
42+
my $max_attempts = 10 * $PostgreSQL::Test::Utils::timeout_default;
4343

4444
my $logcontents;
4545
for (my $attempts = 0; $attempts < $max_attempts; $attempts++)
@@ -78,7 +78,7 @@ sub check_log_pattern
7878
$node->psql('postgres', 'SELECT 1/0');
7979

8080
# might need to retry if logging collector process is slow...
81-
my $max_attempts = 180 * 10;
81+
my $max_attempts = 10 * $PostgreSQL::Test::Utils::timeout_default;
8282

8383
my $current_logfiles;
8484
for (my $attempts = 0; $attempts < $max_attempts; $attempts++)

src/bin/pg_dump/t/002_pg_dump.pl

+2-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@
295295
'--no-sync',
296296
"--file=$tempdir/only_dump_test_table.sql",
297297
'--table=dump_test.test_table',
298-
'--lock-wait-timeout=1000000',
298+
'--lock-wait-timeout='
299+
. (1000 * $PostgreSQL::Test::Utils::timeout_default),
299300
'postgres',
300301
],
301302
},

src/bin/psql/t/010_tab_completion.pl

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
my $in = '';
9595
my $out = '';
9696

97-
my $timer = timer(5);
97+
my $timer = timer($PostgreSQL::Test::Utils::timeout_default);
9898

9999
my $h = $node->interactive_psql('postgres', \$in, \$out, $timer);
100100

@@ -111,7 +111,7 @@ sub check_completion
111111
# reset output collector
112112
$out = "";
113113
# restart per-command timer
114-
$timer->start(5);
114+
$timer->start($PostgreSQL::Test::Utils::timeout_default);
115115
# send the data to be sent
116116
$in .= $send;
117117
# wait ...
@@ -442,7 +442,7 @@ sub clear_line
442442
clear_query();
443443

444444
# send psql an explicit \q to shut it down, else pty won't close properly
445-
$timer->start(5);
445+
$timer->start($PostgreSQL::Test::Utils::timeout_default);
446446
$in .= "\\q\n";
447447
finish $h or die "psql returned $?";
448448
$timer->reset;

src/bin/psql/t/020_cancel.pl

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@
4646
my $psql_pid;
4747
until (-s "$tempdir/psql.pid" and ($psql_pid = PostgreSQL::Test::Utils::slurp_file("$tempdir/psql.pid")) =~ /^\d+\n/s)
4848
{
49-
($count++ < 180 * 100) or die "pid file did not appear";
49+
($count++ < 100 * $PostgreSQL::Test::Utils::timeout_default)
50+
or die "pid file did not appear";
5051
usleep(10_000)
5152
}
5253

5354
# Send sleep command and wait until the server has registered it
54-
$stdin = "select pg_sleep(180);\n";
55+
$stdin = "select pg_sleep($PostgreSQL::Test::Utils::timeout_default);\n";
5556
pump $h while length $stdin;
5657
$node->poll_query_until('postgres', q{SELECT (SELECT count(*) FROM pg_stat_activity WHERE query ~ '^select pg_sleep') > 0;})
5758
or die "timed out";

src/bin/scripts/t/080_pg_isready.pl

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
$node->init;
1919
$node->start;
2020

21-
# use a long timeout for the benefit of very slow buildfarm machines
22-
$node->command_ok([qw(pg_isready --timeout=60)],
21+
$node->command_ok(
22+
[ 'pg_isready', "--timeout=$PostgreSQL::Test::Utils::timeout_default" ],
2323
'succeeds with server running');
2424

2525
done_testing();

src/test/perl/PostgreSQL/Test/Cluster.pm

+16-13
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ PostgreSQL::Test::Cluster - class representing PostgreSQL server instance
3636
my ($stdout, $stderr, $timed_out);
3737
my $cmdret = $node->psql('postgres', 'SELECT pg_sleep(600)',
3838
stdout => \$stdout, stderr => \$stderr,
39-
timeout => 180, timed_out => \$timed_out,
39+
timeout => $PostgreSQL::Test::Utils::timeout_default,
40+
timed_out => \$timed_out,
4041
extra_params => ['--single-transaction'],
4142
on_error_die => 1)
4243
print "Sleep timed out" if $timed_out;
@@ -1723,7 +1724,8 @@ e.g.
17231724
my ($stdout, $stderr, $timed_out);
17241725
my $cmdret = $node->psql('postgres', 'SELECT pg_sleep(600)',
17251726
stdout => \$stdout, stderr => \$stderr,
1726-
timeout => 180, timed_out => \$timed_out,
1727+
timeout => $PostgreSQL::Test::Utils::timeout_default,
1728+
timed_out => \$timed_out,
17271729
extra_params => ['--single-transaction'])
17281730
17291731
will set $cmdret to undef and $timed_out to a true value.
@@ -1897,7 +1899,8 @@ scalar reference. This allows the caller to act on other parts of the system
18971899
while idling this backend.
18981900
18991901
The specified timer object is attached to the harness, as well. It's caller's
1900-
responsibility to select the timeout length, and to restart the timer after
1902+
responsibility to set the timeout length (usually
1903+
$PostgreSQL::Test::Utils::timeout_default), and to restart the timer after
19011904
each command if the timeout is per-command.
19021905
19031906
psql is invoked in tuples-only unaligned mode with reading of B<.psqlrc>
@@ -1985,9 +1988,10 @@ The process's stdin is sourced from the $stdin scalar reference,
19851988
and its stdout and stderr go to the $stdout scalar reference.
19861989
ptys are used so that psql thinks it's being called interactively.
19871990
1988-
The specified timer object is attached to the harness, as well.
1989-
It's caller's responsibility to select the timeout length, and to
1990-
restart the timer after each command if the timeout is per-command.
1991+
The specified timer object is attached to the harness, as well. It's caller's
1992+
responsibility to set the timeout length (usually
1993+
$PostgreSQL::Test::Utils::timeout_default), and to restart the timer after
1994+
each command if the timeout is per-command.
19911995
19921996
psql is invoked in tuples-only unaligned mode with reading of B<.psqlrc>
19931997
disabled. That may be overridden by passing extra psql parameters.
@@ -2303,7 +2307,7 @@ sub connect_fails
23032307
Run B<$query> repeatedly, until it returns the B<$expected> result
23042308
('t', or SQL boolean true, by default).
23052309
Continues polling if B<psql> returns an error result.
2306-
Times out after 180 seconds.
2310+
Times out after $PostgreSQL::Test::Utils::timeout_default seconds.
23072311
Returns 1 if successful, 0 if timed out.
23082312
23092313
=cut
@@ -2321,7 +2325,7 @@ sub poll_query_until
23212325
'-d', $self->connstr($dbname)
23222326
];
23232327
my ($stdout, $stderr);
2324-
my $max_attempts = 180 * 10;
2328+
my $max_attempts = 10 * $PostgreSQL::Test::Utils::timeout_default;
23252329
my $attempts = 0;
23262330

23272331
while ($attempts < $max_attempts)
@@ -2343,8 +2347,8 @@ sub poll_query_until
23432347
$attempts++;
23442348
}
23452349

2346-
# The query result didn't change in 180 seconds. Give up. Print the
2347-
# output from the last attempt, hopefully that's useful for debugging.
2350+
# Give up. Print the output from the last attempt, hopefully that's useful
2351+
# for debugging.
23482352
diag qq(poll_query_until timed out executing this query:
23492353
$query
23502354
expecting this output:
@@ -2657,7 +2661,7 @@ sub wait_for_slot_catchup
26572661
26582662
Waits for the contents of the server log file, starting at the given offset, to
26592663
match the supplied regular expression. Checks the entire log if no offset is
2660-
given. Times out after 180 seconds.
2664+
given. Times out after $PostgreSQL::Test::Utils::timeout_default seconds.
26612665
26622666
If successful, returns the length of the entire log file, in bytes.
26632667
@@ -2668,7 +2672,7 @@ sub wait_for_log
26682672
my ($self, $regexp, $offset) = @_;
26692673
$offset = 0 unless defined $offset;
26702674

2671-
my $max_attempts = 180 * 10;
2675+
my $max_attempts = 10 * $PostgreSQL::Test::Utils::timeout_default;
26722676
my $attempts = 0;
26732677

26742678
while ($attempts < $max_attempts)
@@ -2683,7 +2687,6 @@ sub wait_for_log
26832687
$attempts++;
26842688
}
26852689

2686-
# The logs didn't match within 180 seconds. Give up.
26872690
croak "timed out waiting for match: $regexp";
26882691
}
26892692

src/test/perl/PostgreSQL/Test/Utils.pm

+6-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ our @EXPORT = qw(
9191
$use_unix_sockets
9292
);
9393

94-
our ($windows_os, $is_msys2, $use_unix_sockets, $tmp_check, $log_path,
95-
$test_logfile);
94+
our ($windows_os, $is_msys2, $use_unix_sockets, $timeout_default,
95+
$tmp_check, $log_path, $test_logfile);
9696

9797
BEGIN
9898
{
@@ -157,6 +157,10 @@ BEGIN
157157
# supported, but it can be overridden if desired.
158158
$use_unix_sockets =
159159
(!$windows_os || defined $ENV{PG_TEST_USE_UNIX_SOCKETS});
160+
161+
$timeout_default = $ENV{PG_TEST_TIMEOUT_DEFAULT};
162+
$timeout_default = 180
163+
if not defined $timeout_default or $timeout_default eq '';
160164
}
161165

162166
=pod

src/test/perl/README

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ tmp_check/log/ to get more info. Files named 'regress_log_XXX' are log
2323
output from the perl test scripts themselves, and should be examined first.
2424
Other files are postmaster logs, and may be helpful as additional data.
2525

26+
The tests default to a timeout of 180 seconds for many individual operations.
27+
Slow hosts may avoid load-induced, spurious failures by setting environment
28+
variable PG_TEST_TIMEOUT_DEFAULT to some number of seconds greater than 180.
29+
Developers may see faster failures by setting that environment variable to
30+
some lesser number of seconds.
31+
2632
Data directories will also be left behind for analysis when a test fails;
2733
they are named according to the test filename. But if the environment
2834
variable PG_TEST_NOCLEAN is set, data directories will be retained

src/test/recovery/t/003_recovery_targets.pl

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ sub test_recovery_standby
172172
$node_standby->logfile, 'start'
173173
]);
174174

175-
# wait up to 180s for postgres to terminate
176-
foreach my $i (0 .. 1800)
175+
# wait for postgres to terminate
176+
foreach my $i (0 .. 10 * $PostgreSQL::Test::Utils::timeout_default)
177177
{
178178
last if !-f $node_standby->data_dir . '/postmaster.pid';
179179
usleep(100_000);

src/test/recovery/t/006_logical_decoding.pl

+4-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@
107107
);
108108

109109
my $stdout_recv = $node_primary->pg_recvlogical_upto(
110-
'postgres', 'test_slot', $endpos, 180,
110+
'postgres', 'test_slot', $endpos,
111+
$PostgreSQL::Test::Utils::timeout_default,
111112
'include-xids' => '0',
112113
'skip-empty-xacts' => '1');
113114
chomp($stdout_recv);
@@ -119,7 +120,8 @@
119120
) or die "slot never became inactive";
120121

121122
$stdout_recv = $node_primary->pg_recvlogical_upto(
122-
'postgres', 'test_slot', $endpos, 180,
123+
'postgres', 'test_slot', $endpos,
124+
$PostgreSQL::Test::Utils::timeout_default,
123125
'include-xids' => '0',
124126
'skip-empty-xacts' => '1');
125127
chomp($stdout_recv);

src/test/recovery/t/010_logical_decoding_timelines.pl

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
($ret, $stdout, $stderr) = $node_replica->psql(
158158
'postgres',
159159
"SELECT data FROM pg_logical_slot_peek_changes('before_basebackup', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');",
160-
timeout => 180);
160+
timeout => $PostgreSQL::Test::Utils::timeout_default);
161161
is($ret, 0, 'replay from slot before_basebackup succeeds');
162162

163163
my $final_expected_output_bb = q(BEGIN
@@ -186,7 +186,7 @@ BEGIN
186186

187187
$stdout = $node_replica->pg_recvlogical_upto(
188188
'postgres', 'before_basebackup',
189-
$endpos, 180,
189+
$endpos, $PostgreSQL::Test::Utils::timeout_default,
190190
'include-xids' => '0',
191191
'skip-empty-xacts' => '1');
192192

src/test/recovery/t/013_crash_restart.pl

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@
1818
use Test::More;
1919
use Config;
2020

21-
# To avoid hanging while expecting some specific input from a psql
22-
# instance being driven by us, add a timeout high enough that it
23-
# should never trigger even on very slow machines, unless something
24-
# is really wrong.
25-
my $psql_timeout = IPC::Run::timer(60);
21+
my $psql_timeout = IPC::Run::timer($PostgreSQL::Test::Utils::timeout_default);
2622

2723
my $node = PostgreSQL::Test::Cluster->new('primary');
2824
$node->init(allows_streaming => 1);

src/test/recovery/t/017_shm.pl

+7-6
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ sub log_ipcs
132132
\$stdout,
133133
'2>',
134134
\$stderr,
135-
IPC::Run::timeout(900)); # five times the poll_query_until timeout
135+
IPC::Run::timeout(5 * $PostgreSQL::Test::Utils::timeout_default));
136136
ok( $gnat->poll_query_until(
137137
'postgres',
138138
"SELECT 1 FROM pg_stat_activity WHERE query = '$slow_query'", '1'),
@@ -143,10 +143,11 @@ sub log_ipcs
143143
unlink($gnat->data_dir . '/postmaster.pid');
144144
$gnat->rotate_logfile; # on Windows, can't open old log for writing
145145
log_ipcs();
146-
# Reject ordinary startup. Retry for the same reasons poll_start() does.
146+
# Reject ordinary startup. Retry for the same reasons poll_start() does,
147+
# every 0.1s for at least $PostgreSQL::Test::Utils::timeout_default seconds.
147148
my $pre_existing_msg = qr/pre-existing shared memory block/;
148149
{
149-
my $max_attempts = 180 * 10; # Retry every 0.1s for at least 180s.
150+
my $max_attempts = 10 * $PostgreSQL::Test::Utils::timeout_default;
150151
my $attempts = 0;
151152
while ($attempts < $max_attempts)
152153
{
@@ -193,7 +194,7 @@ sub poll_start
193194
{
194195
my ($node) = @_;
195196

196-
my $max_attempts = 180 * 10;
197+
my $max_attempts = 10 * $PostgreSQL::Test::Utils::timeout_default;
197198
my $attempts = 0;
198199

199200
while ($attempts < $max_attempts)
@@ -209,8 +210,8 @@ sub poll_start
209210
$attempts++;
210211
}
211212

212-
# No success within 180 seconds. Try one last time without fail_ok, which
213-
# will BAIL_OUT unless it succeeds.
213+
# Try one last time without fail_ok, which will BAIL_OUT unless it
214+
# succeeds.
214215
$node->start && return 1;
215216
return 0;
216217
}

src/test/recovery/t/019_replslot_limit.pl

+3-3
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@
291291
SELECT pg_switch_wal();
292292
CHECKPOINT;
293293
SELECT 'finished';",
294-
timeout => '60'));
294+
timeout => $PostgreSQL::Test::Utils::timeout_default));
295295
is($result[1], 'finished', 'check if checkpoint command is not blocked');
296296

297297
$node_primary2->stop;
@@ -362,7 +362,7 @@
362362
kill 'STOP', $senderpid, $receiverpid;
363363
advance_wal($node_primary3, 2);
364364

365-
my $max_attempts = 180;
365+
my $max_attempts = $PostgreSQL::Test::Utils::timeout_default;
366366
while ($max_attempts-- >= 0)
367367
{
368368
if (find_in_log(
@@ -385,7 +385,7 @@
385385
"lost")
386386
or die "timed out waiting for slot to be lost";
387387

388-
$max_attempts = 180;
388+
$max_attempts = $PostgreSQL::Test::Utils::timeout_default;
389389
while ($max_attempts-- >= 0)
390390
{
391391
if (find_in_log(

0 commit comments

Comments
 (0)