Skip to content

Commit 664d757

Browse files
Refactor background psql TAP functions
This breaks out the background and interactive psql functionality into a new class, PostgreSQL::Test::BackgroundPsql. Sessions are still initiated via PostgreSQL::Test::Cluster, but once started they can be manipulated by the new helper functions which intend to make querying easier. A sample session for a command which can be expected to finish at a later time can be seen below. my $session = $node->background_psql('postgres'); $bsession->query_until(qr/start/, q( \echo start CREATE INDEX CONCURRENTLY idx ON t(a); )); $bsession->quit; Patch by Andres Freund with some additional hacking by me. Author: Andres Freund <andres@anarazel.de> Reviewed-by: Andrew Dunstan <andrew@dunslane.net> Discussion: https://postgr.es/m/20230130194350.zj5v467x4jgqt3d6@awork3.anarazel.de
1 parent 32bc0d0 commit 664d757

File tree

7 files changed

+388
-242
lines changed

7 files changed

+388
-242
lines changed

contrib/amcheck/t/003_cic_2pc.pl

+23-47
Original file line numberDiff line numberDiff line change
@@ -36,63 +36,46 @@
3636
# statements.
3737
#
3838

39-
my $main_in = '';
40-
my $main_out = '';
41-
my $main_timer = IPC::Run::timeout($PostgreSQL::Test::Utils::timeout_default);
42-
43-
my $main_h =
44-
$node->background_psql('postgres', \$main_in, \$main_out,
45-
$main_timer, on_error_stop => 1);
46-
$main_in .= q(
39+
my $main_h = $node->background_psql('postgres');
40+
41+
$main_h->query_safe(q(
4742
BEGIN;
4843
INSERT INTO tbl VALUES(0);
49-
\echo syncpoint1
50-
);
51-
pump $main_h until $main_out =~ /syncpoint1/ || $main_timer->is_expired;
52-
53-
my $cic_in = '';
54-
my $cic_out = '';
55-
my $cic_timer = IPC::Run::timeout($PostgreSQL::Test::Utils::timeout_default);
56-
my $cic_h =
57-
$node->background_psql('postgres', \$cic_in, \$cic_out,
58-
$cic_timer, on_error_stop => 1);
59-
$cic_in .= q(
44+
));
45+
46+
my $cic_h = $node->background_psql('postgres');
47+
48+
$cic_h->query_until(qr/start/, q(
6049
\echo start
6150
CREATE INDEX CONCURRENTLY idx ON tbl(i);
62-
);
63-
pump $cic_h until $cic_out =~ /start/ || $cic_timer->is_expired;
51+
));
6452

65-
$main_in .= q(
53+
$main_h->query_safe(q(
6654
PREPARE TRANSACTION 'a';
67-
);
55+
));
6856

69-
$main_in .= q(
57+
$main_h->query_safe(q(
7058
BEGIN;
7159
INSERT INTO tbl VALUES(0);
72-
\echo syncpoint2
73-
);
74-
pump $main_h until $main_out =~ /syncpoint2/ || $main_timer->is_expired;
60+
));
7561

7662
$node->safe_psql('postgres', q(COMMIT PREPARED 'a';));
7763

78-
$main_in .= q(
64+
$main_h->query_safe(q(
7965
PREPARE TRANSACTION 'b';
8066
BEGIN;
8167
INSERT INTO tbl VALUES(0);
82-
\echo syncpoint3
83-
);
84-
pump $main_h until $main_out =~ /syncpoint3/ || $main_timer->is_expired;
68+
));
8569

8670
$node->safe_psql('postgres', q(COMMIT PREPARED 'b';));
8771

88-
$main_in .= q(
72+
$main_h->query_safe(q(
8973
PREPARE TRANSACTION 'c';
9074
COMMIT PREPARED 'c';
91-
);
92-
$main_h->pump_nb;
75+
));
9376

94-
$main_h->finish;
95-
$cic_h->finish;
77+
$main_h->quit;
78+
$cic_h->quit;
9679

9780
$result = $node->psql('postgres', q(SELECT bt_index_check('idx',true)));
9881
is($result, '0', 'bt_index_check after overlapping 2PC');
@@ -113,22 +96,15 @@
11396
));
11497
$node->restart;
11598

116-
my $reindex_in = '';
117-
my $reindex_out = '';
118-
my $reindex_timer =
119-
IPC::Run::timeout($PostgreSQL::Test::Utils::timeout_default);
120-
my $reindex_h =
121-
$node->background_psql('postgres', \$reindex_in, \$reindex_out,
122-
$reindex_timer, on_error_stop => 1);
123-
$reindex_in .= q(
99+
my $reindex_h = $node->background_psql('postgres');
100+
$reindex_h->query_until(qr/start/, q(
124101
\echo start
125102
DROP INDEX CONCURRENTLY idx;
126103
CREATE INDEX CONCURRENTLY idx ON tbl(i);
127-
);
128-
pump $reindex_h until $reindex_out =~ /start/ || $reindex_timer->is_expired;
104+
));
129105

130106
$node->safe_psql('postgres', "COMMIT PREPARED 'spans_restart'");
131-
$reindex_h->finish;
107+
$reindex_h->quit;
132108
$result = $node->psql('postgres', q(SELECT bt_index_check('idx',true)));
133109
is($result, '0', 'bt_index_check after 2PC and restart');
134110

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

+7-21
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PostgreSQL::Test::Cluster;
88
use PostgreSQL::Test::Utils;
99
use Test::More;
10-
use IPC::Run qw(pump finish timer);
1110
use Data::Dumper;
1211

1312
# Do nothing unless Makefile has told us that the build is --with-readline.
@@ -92,14 +91,7 @@
9291
close $FH;
9392

9493
# fire up an interactive psql session
95-
my $in = '';
96-
my $out = '';
97-
98-
my $timer = timer($PostgreSQL::Test::Utils::timeout_default);
99-
100-
my $h = $node->interactive_psql('postgres', \$in, \$out, $timer);
101-
102-
like($out, qr/psql/, "print startup banner");
94+
my $h = $node->interactive_psql('postgres');
10395

10496
# Simple test case: type something and see if psql responds as expected
10597
sub check_completion
@@ -109,15 +101,12 @@ sub check_completion
109101
# report test failures from caller location
110102
local $Test::Builder::Level = $Test::Builder::Level + 1;
111103

112-
# reset output collector
113-
$out = "";
114104
# restart per-command timer
115-
$timer->start($PostgreSQL::Test::Utils::timeout_default);
116-
# send the data to be sent
117-
$in .= $send;
118-
# wait ...
119-
pump $h until ($out =~ $pattern || $timer->is_expired);
120-
my $okay = ($out =~ $pattern && !$timer->is_expired);
105+
$h->{timeout}->start($PostgreSQL::Test::Utils::timeout_default);
106+
107+
# send the data to be sent and wait for its result
108+
my $out = $h->query_until($pattern, $send);
109+
my $okay = ($out =~ $pattern && !$h->{timeout}->is_expired);
121110
ok($okay, $annotation);
122111
# for debugging, log actual output if it didn't match
123112
local $Data::Dumper::Terse = 1;
@@ -451,10 +440,7 @@ sub clear_line
451440
clear_line();
452441

453442
# send psql an explicit \q to shut it down, else pty won't close properly
454-
$timer->start($PostgreSQL::Test::Utils::timeout_default);
455-
$in .= "\\q\n";
456-
finish $h or die "psql returned $?";
457-
$timer->reset;
443+
$h->quit or die "psql returned $?";
458444

459445
# done
460446
$node->stop;

0 commit comments

Comments
 (0)