Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3b7bbee

Browse files
committed
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
1 parent 51865a0 commit 3b7bbee

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/test/perl/PostgresNode.pm

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ PostgresNode - class representing PostgreSQL server instance
99
1010
use PostgresNode;
1111
12-
my $node = get_new_node('mynode');
12+
my $node = PostgresNode->get_new_node('mynode');
1313
1414
# Create a data directory with initdb
1515
$node->init();
@@ -845,20 +845,24 @@ sub _update_pid
845845

846846
=pod
847847
848-
=item get_new_node(node_name)
848+
=item PostgresNode->get_new_node(node_name)
849849
850-
Build a new PostgresNode object, assigning a free port number. Standalone
851-
function that's automatically imported.
850+
Build a new object of class C<PostgresNode> (or of a subclass, if you have
851+
one), assigning a free port number. Remembers the node, to prevent its port
852+
number from being reused for another node, and to ensure that it gets
853+
shut down when the test script exits.
852854
853-
Remembers the node, to prevent its port number from being reused for another
854-
node, and to ensure that it gets shut down when the test script exits.
855+
You should generally use this instead of C<PostgresNode::new(...)>.
855856
856-
You should generally use this instead of PostgresNode::new(...).
857+
For backwards compatibility, it is also exported as a standalone function,
858+
which can only create objects of class C<PostgresNode>.
857859
858860
=cut
859861

860862
sub get_new_node
861863
{
864+
my $class = 'PostgresNode';
865+
$class = shift if 1 < scalar @_;
862866
my $name = shift;
863867
my $found = 0;
864868
my $port = $last_port_assigned;
@@ -903,7 +907,7 @@ sub get_new_node
903907
print "# Found free port $port\n";
904908

905909
# Lock port number found by creating a new node
906-
my $node = new PostgresNode($name, $test_pghost, $port);
910+
my $node = $class->new($name, $test_pghost, $port);
907911

908912
# Add node to list of nodes
909913
push(@all_nodes, $node);

src/test/perl/README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Each test script should begin with:
4242
then it will generally need to set up one or more nodes, run commands
4343
against them and evaluate the results. For example:
4444

45-
my $node = get_new_node('master');
45+
my $node = PostgresNode->get_new_node('master');
4646
$node->init;
4747
$node->start;
4848

0 commit comments

Comments
 (0)