Skip to content

Commit 6af51bf

Browse files
committed
backport: Extend background_psql() to be able to start asynchronously
This is a backport of ba08edb. Originally it was only applied to master, but I (Andres) am planning to fix a few bugs in BackgroundPsql, which would be somewhat harder with the behavioural differences across branches. It's also generally good for test infrastructure to behave similarly across branches, to avoid pain during backpatching. Discussion: https://postgr.es/m/ilcctzb5ju2gulvnadjmhgatnkxsdpac652byb2u3d3wqziyvx@fbuqcglker46 Michael's original commit message: This commit extends the constructor routine of BackgroundPsql.pm with a new "wait" parameter. If set to 0, the routine returns without waiting for psql to start, ready to consume input. background_psql() in Cluster.pm gains the same "wait" parameter. The default behavior is still to wait for psql to start. It becomes now possible to not wait, giving to TAP scripts the possibility to perform actions between a BackgroundPsql startup and its wait_connect() call. Author: Jacob Champion Discussion: https://postgr.es/m/CAOYmi+=60deN20WDyCoHCiecgivJxr=98s7s7-C8SkXwrCfHXg@mail.gmail.com
1 parent 3c562b5 commit 6af51bf

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,23 @@ use Test::More;
6868
6969
=over
7070
71-
=item PostgreSQL::Test::BackgroundPsql->new(interactive, @psql_params, timeout)
71+
=item PostgreSQL::Test::BackgroundPsql->new(interactive, @psql_params, timeout, wait)
7272
7373
Builds a new object of class C<PostgreSQL::Test::BackgroundPsql> for either
7474
an interactive or background session and starts it. If C<interactive> is
7575
true then a PTY will be attached. C<psql_params> should contain the full
7676
command to run psql with all desired parameters and a complete connection
7777
string. For C<interactive> sessions, IO::Pty is required.
7878
79+
This routine will not return until psql has started up and is ready to
80+
consume input. Set B<wait> to 0 to return immediately instead.
81+
7982
=cut
8083

8184
sub new
8285
{
8386
my $class = shift;
84-
my ($interactive, $psql_params, $timeout) = @_;
87+
my ($interactive, $psql_params, $timeout, $wait) = @_;
8588
my $psql = {
8689
'stdin' => '',
8790
'stdout' => '',
@@ -119,14 +122,25 @@ sub new
119122

120123
my $self = bless $psql, $class;
121124

122-
$self->_wait_connect();
125+
$wait = 1 unless defined($wait);
126+
if ($wait)
127+
{
128+
$self->wait_connect();
129+
}
123130

124131
return $self;
125132
}
126133

127-
# Internal routine for awaiting psql starting up and being ready to consume
128-
# input.
129-
sub _wait_connect
134+
=pod
135+
136+
=item $session->wait_connect
137+
138+
Returns once psql has started up and is ready to consume input. This is called
139+
automatically for clients unless requested otherwise in the constructor.
140+
141+
=cut
142+
143+
sub wait_connect
130144
{
131145
my ($self) = @_;
132146

@@ -187,7 +201,7 @@ sub reconnect_and_clear
187201
$self->{stdin} = '';
188202
$self->{stdout} = '';
189203

190-
$self->_wait_connect();
204+
$self->wait_connect();
191205
}
192206

193207
=pod

src/test/perl/PostgresNode.pm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,12 @@ connection.
17141714
17151715
If given, it must be an array reference containing additional parameters to B<psql>.
17161716
1717+
=item wait => 1
1718+
1719+
By default, this method will not return until connection has completed (or
1720+
failed). Set B<wait> to 0 to return immediately instead. (Clients can call the
1721+
session's C<wait_connect> method manually when needed.)
1722+
17171723
=back
17181724
17191725
=cut
@@ -1738,13 +1744,15 @@ sub background_psql
17381744
'-');
17391745

17401746
$params{on_error_stop} = 1 unless defined $params{on_error_stop};
1747+
$params{wait} = 1 unless defined $params{wait};
17411748
$timeout = $params{timeout} if defined $params{timeout};
17421749

17431750
push @psql_params, '-v', 'ON_ERROR_STOP=1' if $params{on_error_stop};
17441751
push @psql_params, @{ $params{extra_params} }
17451752
if defined $params{extra_params};
17461753

1747-
return PostgreSQL::Test::BackgroundPsql->new(0, \@psql_params, $timeout);
1754+
return PostgreSQL::Test::BackgroundPsql->new(0, \@psql_params, $timeout,
1755+
$params{wait});
17481756
}
17491757

17501758
=pod

0 commit comments

Comments
 (0)