Skip to content

Commit

Permalink
Add TimescaleNode subclasses for TAP Testing
Browse files Browse the repository at this point in the history
Use subclassing to inherit from `PostgresNode` and create a hierarchy
containing `AccessNode` and `DataNode` to simplify creating tests with
multiple nodes.

Also, two new functions are added:

`TimescaleNode::create`: Creates the new node by calling
`get_new_node`, `init` and `start` in that order.

`AccessNode::add_data_node`: Adds a new data node to the access node.

Also rewrite the test to use the new hierarchy.
  • Loading branch information
mkindahl committed May 21, 2021
1 parent 551ac56 commit 58b1eb8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 56 deletions.
21 changes: 21 additions & 0 deletions test/perl/AccessNode.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This file and its contents are licensed under the Timescale License.
# Please see the included NOTICE for copyright information and
# LICENSE-TIMESCALE for a copy of the license.

package AccessNode;
use parent qw(TimescaleNode);
use strict;
use warnings;

sub add_data_node
{
my ($self, $dn) = @_;
my $name = $dn->name;
my $host = $dn->host;
my $port = $dn->port;
$self->safe_psql('postgres',
"SELECT add_data_node('$name', host => '$host', port => $port)");
return $self;
}

1;
10 changes: 10 additions & 0 deletions test/perl/DataNode.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file and its contents are licensed under the Timescale License.
# Please see the included NOTICE for copyright information and
# LICENSE-TIMESCALE for a copy of the license.

package DataNode;
use parent qw(TimescaleNode);
use strict;
use warnings;

1;
30 changes: 7 additions & 23 deletions test/perl/TimescaleNode.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,21 @@
# routines for setup.

package TimescaleNode;
use parent ("PostgresNode");
use parent qw(PostgresNode);
use TestLib qw(slurp_file);
use strict;
use warnings;

use Carp 'verbose';
$SIG{__DIE__} = \&Carp::confess;

use Exporter 'import';
use vars qw(@EXPORT @EXPORT_OK);
@EXPORT = qw(
get_new_ts_node
);
@EXPORT_OK = qw(
);

#
# Get a new TS-enabled PostgreSQL instance
#
# It's not created yet, but ready to restore from backup,
# initdb, etc.
#
sub get_new_ts_node
sub create
{
my ($name, $class) = @_;

$class //= 'TimescaleNode';

my $self = PostgresNode::get_new_node($name);
$self = bless $self, $class;

my ($class, $name, %kwargs) = @_;
my $self = $class->get_new_node($name);
$self->init(%kwargs);
$self->start(%kwargs);
$self->safe_psql('postgres', 'CREATE EXTENSION timescaledb');
return $self;
}

Expand Down
48 changes: 15 additions & 33 deletions tsl/test/t/001_simple_multinode.pl
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,22 @@
# test a simple multi node cluster creation and basic operations
use strict;
use warnings;
use TimescaleNode qw(get_new_ts_node);
use AccessNode;
use DataNode;
use TestLib;
use Test::More tests => 9;

#Initialize all the multi-node instances
my @nodes = ();
foreach my $nodename ('an', 'dn1', 'dn2')
{
my $node = get_new_ts_node($nodename);
$node->init;
$node->start;
# set up the access node
if ($node->name eq 'an')
{
$node->safe_psql('postgres', "CREATE DATABASE an");
$node->safe_psql('an', "CREATE EXTENSION timescaledb");
}
push @nodes, $node;
}
my $an = AccessNode->create('an');
my $dn1 = DataNode->create('dn1');
my $dn2 = DataNode->create('dn2');

#Add the data nodes from the access node
foreach my $i (1 .. 2)
{
my $host = $nodes[$i]->host();
my $port = $nodes[$i]->port();

$nodes[0]->safe_psql('an',
"SELECT add_data_node('dn$i', database => 'dn$i', host => '$host', port => $port)"
);
}
$an->add_data_node($dn1);
$an->add_data_node($dn2);

#Create a distributed hypertable and insert a few rows
$nodes[0]->safe_psql(
'an',
$an->safe_psql(
'postgres',
qq[
CREATE TABLE test(time timestamp NOT NULL, device int, temp float);
SELECT create_distributed_hypertable('test', 'time', 'device', 3);
Expand All @@ -49,23 +31,23 @@
my $query = q[SELECT * from show_chunks('test');];

#Query Access node
$nodes[0]->psql_is(
'an', $query, q[_timescaledb_internal._dist_hyper_1_1_chunk
$an->psql_is(
'postgres', $query, q[_timescaledb_internal._dist_hyper_1_1_chunk
_timescaledb_internal._dist_hyper_1_2_chunk
_timescaledb_internal._dist_hyper_1_3_chunk
_timescaledb_internal._dist_hyper_1_4_chunk], 'AN shows correct set of chunks'
);

#Query datanode1
$nodes[1]->psql_is(
'dn1',
$dn1->psql_is(
'postgres',
$query,
"_timescaledb_internal._dist_hyper_1_1_chunk\n_timescaledb_internal._dist_hyper_1_3_chunk\n_timescaledb_internal._dist_hyper_1_4_chunk",
'DN1 shows correct set of chunks');

#Query datanode2
$nodes[2]->psql_is(
'dn2', $query,
$dn2->psql_is(
'postgres', $query,
"_timescaledb_internal._dist_hyper_1_2_chunk",
'DN2 shows correct set of chunks');

Expand Down

0 comments on commit 58b1eb8

Please sign in to comment.