mirror of
https://github.com/postgres/postgres.git
synced 2025-05-05 09:19:17 +03:00
Make PostgresNode easily subclassable
This module becomes much more useful if we allow it to be used as base class for external projects. To achieve this, change the exported get_new_node function into a class method instead, and use the standard Perl idiom of accepting the class as first argument. This method works as expected for subclasses. The standalone function is kept for backwards compatibility, though it could be removed in pg11. Author: Chap Flackman, based on an earlier patch from Craig Ringer Discussion: https://postgr.es/m/CAMsr+YF8kO+4+K-_U4PtN==2FndJ+5Bn6A19XHhMiBykEwv0wA@mail.gmail.com
This commit is contained in:
parent
9915de6c1c
commit
54dacc7466
@ -9,7 +9,7 @@ PostgresNode - class representing PostgreSQL server instance
|
|||||||
|
|
||||||
use PostgresNode;
|
use PostgresNode;
|
||||||
|
|
||||||
my $node = get_new_node('mynode');
|
my $node = PostgresNode->get_new_node('mynode');
|
||||||
|
|
||||||
# Create a data directory with initdb
|
# Create a data directory with initdb
|
||||||
$node->init();
|
$node->init();
|
||||||
@ -855,20 +855,24 @@ sub _update_pid
|
|||||||
|
|
||||||
=pod
|
=pod
|
||||||
|
|
||||||
=item get_new_node(node_name)
|
=item PostgresNode->get_new_node(node_name)
|
||||||
|
|
||||||
Build a new PostgresNode object, assigning a free port number. Standalone
|
Build a new object of class C<PostgresNode> (or of a subclass, if you have
|
||||||
function that's automatically imported.
|
one), assigning a free port number. Remembers the node, to prevent its port
|
||||||
|
number from being reused for another node, and to ensure that it gets
|
||||||
|
shut down when the test script exits.
|
||||||
|
|
||||||
Remembers the node, to prevent its port number from being reused for another
|
You should generally use this instead of C<PostgresNode::new(...)>.
|
||||||
node, and to ensure that it gets shut down when the test script exits.
|
|
||||||
|
|
||||||
You should generally use this instead of PostgresNode::new(...).
|
For backwards compatibility, it is also exported as a standalone function,
|
||||||
|
which can only create objects of class C<PostgresNode>.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
sub get_new_node
|
sub get_new_node
|
||||||
{
|
{
|
||||||
|
my $class = 'PostgresNode';
|
||||||
|
$class = shift if 1 < scalar @_;
|
||||||
my $name = shift;
|
my $name = shift;
|
||||||
my $found = 0;
|
my $found = 0;
|
||||||
my $port = $last_port_assigned;
|
my $port = $last_port_assigned;
|
||||||
@ -913,7 +917,7 @@ sub get_new_node
|
|||||||
print "# Found free port $port\n";
|
print "# Found free port $port\n";
|
||||||
|
|
||||||
# Lock port number found by creating a new node
|
# Lock port number found by creating a new node
|
||||||
my $node = new PostgresNode($name, $test_pghost, $port);
|
my $node = $class->new($name, $test_pghost, $port);
|
||||||
|
|
||||||
# Add node to list of nodes
|
# Add node to list of nodes
|
||||||
push(@all_nodes, $node);
|
push(@all_nodes, $node);
|
||||||
|
@ -48,7 +48,7 @@ Each test script should begin with:
|
|||||||
then it will generally need to set up one or more nodes, run commands
|
then it will generally need to set up one or more nodes, run commands
|
||||||
against them and evaluate the results. For example:
|
against them and evaluate the results. For example:
|
||||||
|
|
||||||
my $node = get_new_node('master');
|
my $node = PostgresNode->get_new_node('master');
|
||||||
$node->init;
|
$node->init;
|
||||||
$node->start;
|
$node->start;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user