Skip to content

Commit 49b6f4a

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 31a242e commit 49b6f4a

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/PostgreSQL/Test/Cluster.pm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,12 @@ connection.
21912191
21922192
If given, it must be an array reference containing additional parameters to B<psql>.
21932193
2194+
=item wait => 1
2195+
2196+
By default, this method will not return until connection has completed (or
2197+
failed). Set B<wait> to 0 to return immediately instead. (Clients can call the
2198+
session's C<wait_connect> method manually when needed.)
2199+
21942200
=back
21952201
21962202
=cut
@@ -2214,13 +2220,15 @@ sub background_psql
22142220
'-');
22152221

22162222
$params{on_error_stop} = 1 unless defined $params{on_error_stop};
2223+
$params{wait} = 1 unless defined $params{wait};
22172224
$timeout = $params{timeout} if defined $params{timeout};
22182225

22192226
push @psql_params, '-v', 'ON_ERROR_STOP=1' if $params{on_error_stop};
22202227
push @psql_params, @{ $params{extra_params} }
22212228
if defined $params{extra_params};
22222229

2223-
return PostgreSQL::Test::BackgroundPsql->new(0, \@psql_params, $timeout);
2230+
return PostgreSQL::Test::BackgroundPsql->new(0, \@psql_params, $timeout,
2231+
$params{wait});
22242232
}
22252233

22262234
=pod

0 commit comments

Comments
 (0)