1
0
mirror of https://github.com/postgres/postgres.git synced 2025-05-05 09:19:17 +03:00

Make test_target_session_attrs more robust against connection failure.

Feed the desired command to psql via "-c" not stdin, else Perl
may complain that it can't push stdin to an already-failed psql
process, as seen in intermittent buildfarm failures.

Make some minor cosmetic improvements while at it.

Before commit ee28cacf6 we had no tests here that expected failure
to connect, so there seems no need for a back-patch.

Discussion: https://postgr.es/m/CALDaNm2mo8YED=M2ZJKGf1U3F3mw6SaQuLXWCK8rZP6sECYcrA@mail.gmail.com
This commit is contained in:
Tom Lane 2021-03-03 13:51:43 -05:00
parent f06b1c5982
commit 3769e11a31

View File

@ -68,8 +68,8 @@ is($node_standby_2->psql('postgres', 'INSERT INTO tab_int VALUES (1)'),
# Tests for connection parameter target_session_attrs # Tests for connection parameter target_session_attrs
note "testing connection parameter \"target_session_attrs\""; note "testing connection parameter \"target_session_attrs\"";
# Routine designed to run tests on the connection parameter # Attempt to connect to $node1, then $node2, using target_session_attrs=$mode.
# target_session_attrs with multiple nodes. # Expect to connect to $target_node (undef for failure) with given $status.
sub test_target_session_attrs sub test_target_session_attrs
{ {
my $node1 = shift; my $node1 = shift;
@ -84,7 +84,8 @@ sub test_target_session_attrs
my $node2_host = $node2->host; my $node2_host = $node2->host;
my $node2_port = $node2->port; my $node2_port = $node2->port;
my $node2_name = $node2->name; my $node2_name = $node2->name;
my $target_port = undef;
$target_port = $target_node->port if (defined $target_node);
my $target_name = undef; my $target_name = undef;
$target_name = $target_node->name if (defined $target_node); $target_name = $target_node->name if (defined $target_node);
@ -93,16 +94,18 @@ sub test_target_session_attrs
$connstr .= "port=$node1_port,$node2_port "; $connstr .= "port=$node1_port,$node2_port ";
$connstr .= "target_session_attrs=$mode"; $connstr .= "target_session_attrs=$mode";
# The client used for the connection does not matter, only the backend # Attempt to connect, and if successful, get the server port number
# point does. # we connected to. Note we must pass the SQL command via the command
# line not stdin, else Perl may spit up trying to write to stdin of
# an already-failed psql process.
my ($ret, $stdout, $stderr) = my ($ret, $stdout, $stderr) =
$node1->psql('postgres', 'SHOW port;', $node1->psql('postgres', undef,
extra_params => [ '-d', $connstr ]); extra_params => [ '-d', $connstr, '-c', 'SHOW port;' ]);
if ($status == 0) if ($status == 0)
{ {
is( $status == $ret && $stdout eq $target_node->port, is( $status == $ret && $stdout eq $target_port,
1, 1,
"connect to node $target_name if mode \"$mode\" and $node1_name,$node2_name listed" "connect to node $target_name with mode \"$mode\" and $node1_name,$node2_name listed"
); );
} }
else else
@ -112,9 +115,9 @@ sub test_target_session_attrs
print "stdout = $stdout\n"; print "stdout = $stdout\n";
print "stderr = $stderr\n"; print "stderr = $stderr\n";
is( $status == $ret, is( $status == $ret && !defined $target_node,
1, 1,
"fail to connect to any nodes if mode \"$mode\" and $node1_name,$node2_name listed" "fail to connect with mode \"$mode\" and $node1_name,$node2_name listed"
); );
} }