@@ -89,8 +89,7 @@ sub new
8989 ' stdin' => ' ' ,
9090 ' stdout' => ' ' ,
9191 ' stderr' => ' ' ,
92- ' query_timer_restart' => undef ,
93- ' query_cnt' => 1,
92+ ' query_timer_restart' => undef
9493 };
9594 my $run ;
9695
@@ -152,25 +151,11 @@ sub wait_connect
152151 # connection failures are caught here, relieving callers of the need to
153152 # handle those. (Right now, we have no particularly good handling for
154153 # errors anyway, but that might be added later.)
155- #
156- # See query() for details about why/how the banner is used.
157154 my $banner = " background_psql: ready" ;
158- my $banner_match = qr / (^|\n )$banner \r ?\n / ;
159- $self -> {stdin } .= " \\ echo $banner \n\\ warn $banner \n " ;
155+ $self -> {stdin } .= " \\ echo $banner \n " ;
160156 $self -> {run }-> pump()
161- until ($self -> {stdout } =~ / $banner_match /
162- && $self -> {stderr } =~ / $banner \r ?\n / )
163- || $self -> {timeout }-> is_expired;
164-
165- note " connect output:\n " ,
166- explain {
167- stdout => $self -> {stdout },
168- stderr => $self -> {stderr },
169- };
170-
171- # clear out banners
172- $self -> {stdout } = ' ' ;
173- $self -> {stderr } = ' ' ;
157+ until $self -> {stdout } =~ / $banner / || $self -> {timeout }-> is_expired;
158+ $self -> {stdout } = ' ' ; # clear out banner
174159
175160 die " psql startup timed out" if $self -> {timeout }-> is_expired;
176161}
@@ -237,57 +222,27 @@ sub query
237222 my ($self , $query ) = @_ ;
238223 my $ret ;
239224 my $output ;
240- my $query_cnt = $self -> {query_cnt }++;
241-
242225 local $Test::Builder::Level = $Test::Builder::Level + 1;
243226
244- note " issuing query $query_cnt via background psql: $query " ;
227+ note " issuing query via background psql: $query " ;
245228
246229 $self -> {timeout }-> start() if (defined ($self -> {query_timer_restart }));
247230
248231 # Feed the query to psql's stdin, followed by \n (so psql processes the
249232 # line), by a ; (so that psql issues the query, if it doesn't include a ;
250- # itself), and a separator echoed both with \echo and \warn, that we can
251- # wait on.
252- #
253- # To avoid somehow confusing the separator from separately issued queries,
254- # and to make it easier to debug, we include a per-psql query counter in
255- # the separator.
256- #
257- # We need both \echo (printing to stdout) and \warn (printing to stderr),
258- # because on windows we can get data on stdout before seeing data on
259- # stderr (or vice versa), even if psql printed them in the opposite
260- # order. We therefore wait on both.
261- #
262- # We need to match for the newline, because we try to remove it below, and
263- # it's possible to consume just the input *without* the newline. In
264- # interactive psql we emit \r\n, so we need to allow for that. Also need
265- # to be careful that we don't e.g. match the echoed \echo command, rather
266- # than its output.
267- my $banner = " background_psql: QUERY_SEPARATOR $query_cnt :" ;
268- my $banner_match = qr / (^|\n )$banner \r ?\n / ;
269- $self -> {stdin } .= " $query \n ;\n\\ echo $banner \n\\ warn $banner \n " ;
270- pump_until(
271- $self -> {run }, $self -> {timeout },
272- \$self -> {stdout }, qr /$banner_match / );
273- pump_until(
274- $self -> {run }, $self -> {timeout },
275- \$self -> {stderr }, qr /$banner_match / );
233+ # itself), and a separator echoed with \echo, that we can wait on.
234+ my $banner = " background_psql: QUERY_SEPARATOR" ;
235+ $self -> {stdin } .= " $query \n ;\n\\ echo $banner \n " ;
276236
277- die " psql query timed out" if $self -> {timeout }-> is_expired;
278-
279- note " results query $query_cnt :\n " ,
280- explain {
281- stdout => $self -> {stdout },
282- stderr => $self -> {stderr },
283- };
237+ pump_until($self -> {run }, $self -> {timeout }, \$self -> {stdout }, qr /$banner / );
284238
285- # Remove banner from stdout and stderr, our caller doesn't care. The
286- # first newline is optional, as there would not be one if consuming an
287- # empty query result.
239+ die " psql query timed out" if $self -> {timeout }-> is_expired;
288240 $output = $self -> {stdout };
289- $output =~ s / $banner_match// ;
290- $self -> {stderr } =~ s / $banner_match// ;
241+
242+ # Remove banner again, our caller doesn't care. The first newline is
243+ # optional, as there would not be one if consuming an empty query
244+ # result.
245+ $output =~ s /\n ?$banner\n $// s ;
291246
292247 # clear out output for the next query
293248 $self -> {stdout } = ' ' ;
0 commit comments