Skip to content

Commit ba08edb

Browse files
committed
Extend Cluster.pm's background_psql() to be able to start asynchronously
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 87f81a5 commit ba08edb

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

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

+21-7
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

+9-1
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,12 @@ connection.
22862286
22872287
If given, it must be an array reference containing additional parameters to B<psql>.
22882288
2289+
=item wait => 1
2290+
2291+
By default, this method will not return until connection has completed (or
2292+
failed). Set B<wait> to 0 to return immediately instead. (Clients can call the
2293+
session's C<wait_connect> method manually when needed.)
2294+
22892295
=back
22902296
22912297
=cut
@@ -2316,13 +2322,15 @@ sub background_psql
23162322
'-XAtq', '-d', $psql_connstr, '-f', '-');
23172323

23182324
$params{on_error_stop} = 1 unless defined $params{on_error_stop};
2325+
$params{wait} = 1 unless defined $params{wait};
23192326
$timeout = $params{timeout} if defined $params{timeout};
23202327

23212328
push @psql_params, '-v', 'ON_ERROR_STOP=1' if $params{on_error_stop};
23222329
push @psql_params, @{ $params{extra_params} }
23232330
if defined $params{extra_params};
23242331

2325-
return PostgreSQL::Test::BackgroundPsql->new(0, \@psql_params, $timeout);
2332+
return PostgreSQL::Test::BackgroundPsql->new(0, \@psql_params, $timeout,
2333+
$params{wait});
23262334
}
23272335

23282336
=pod

0 commit comments

Comments
 (0)