11use strict;
22use warnings;
3- use PostgresNode ;
3+ use Cluster ;
44use TestLib;
55use Test::More tests => 3;
66use DBI;
77use DBD::Pg ' :async' ;
88
9- # ##############################################################################
10- # Aux routines
11- # ##############################################################################
12-
13- sub PostgresNode ::inet_connstr {
14- my ($self , $dbname ) = @_ ;
15- my $pgport = $self -> port;
16- my $pghost = ' 127.0.0.1' ;
17- my $pgdata = $self -> data_dir;
18-
19- if (!defined ($dbname ))
20- {
21- return " port=$pgport host=$pghost " ;
22- }
23- return " port=$pgport host=$pghost dbname=$dbname " ;
24- }
25-
26- # ##############################################################################
27- # Setup nodes
28- # ##############################################################################
29-
30- my $nnodes = 3;
31- my @nodes = ();
32- my $pgconf_common = qq(
33- listen_addresses = '127.0.0.1'
34- max_prepared_transactions = 200
35- max_connections = 200
36- max_worker_processes = 100
37- max_wal_senders = 10
38- max_replication_slots = 10
39- wal_level = logical
40- wal_sender_timeout = 0
41- shared_preload_libraries = 'raftable,multimaster'
42- multimaster.workers=10
43- multimaster.queue_size=10485760 # 10mb
44- ) ;
45-
46- # Init nodes
47- for (my $i =0; $i < $nnodes ; $i ++) {
48- push (@nodes , get_new_node(" node$i " ));
49- $nodes [$i ]-> init;
50- }
51-
52- # Collect conn info
53- my $mm_connstr = join (' , ' , map { " ${ \$ _->inet_connstr('postgres') }" } @nodes );
54-
55- # Configure and start nodes
56- for (my $i =0; $i < $nnodes ; $i ++) {
57- $nodes [$i ]-> append_conf(' postgresql.conf' , $pgconf_common );
58- $nodes [$i ]-> append_conf(' postgresql.conf' , qq(
59- #port = ${ \$ nodes[$i ]->port }
60- multimaster.node_id = @{[ $i + 1 ]}
61- multimaster.conn_strings = '$mm_connstr '
62- #multimaster.arbiter_port = ${ \$ nodes[0]->port }
63- ) );
64- $nodes [$i ]-> append_conf(' pg_hba.conf' , qq(
65- host replication all 127.0.0.1/32 trust
66- ) );
67- $nodes [$i ]-> start;
68- }
9+ my $cluster = new Cluster(3);
10+ $cluster -> init();
11+ $cluster -> configure();
12+ $cluster -> start();
6913
7014# ##############################################################################
7115# Wait until nodes are up
@@ -79,11 +23,11 @@ sub PostgresNode::inet_connstr {
7923# Replication check
8024# ##############################################################################
8125
82- $nodes [0] -> psql(' postgres' , "
26+ $cluster -> psql(0, ' postgres' , "
8327 create extension multimaster;
8428 create table if not exists t(k int primary key, v int);
8529 insert into t values(1, 10);" );
86- $nodes [1] -> psql(' postgres' , " select v from t where k=1;" , stdout => \$psql_out );
30+ $cluster -> psql(1, ' postgres' , " select v from t where k=1;" , stdout => \$psql_out );
8731is($psql_out , ' 10' , " Check replication while all nodes are up." );
8832
8933# ##############################################################################
@@ -97,15 +41,15 @@ sub PostgresNode::inet_connstr {
9741# ##############################################################################
9842
9943diag(" stopping node 2" );
100- $nodes [2]-> teardown_node;
44+ $cluster -> { nodes } -> [2]-> teardown_node;
10145
10246diag(" sleeping 15" );
10347sleep (15);
10448
10549diag(" inserting 2" );
106- $nodes [0] -> psql(' postgres' , " insert into t values(2, 20);" );
50+ $cluster -> psql(0, ' postgres' , " insert into t values(2, 20);" );
10751diag(" selecting" );
108- $nodes [1] -> psql(' postgres' , " select v from t where k=2;" , stdout => \$psql_out );
52+ $cluster -> psql(1, ' postgres' , " select v from t where k=2;" , stdout => \$psql_out );
10953diag(" selected" );
11054is($psql_out , ' 20' , " Check replication after node failure." );
11155
@@ -114,13 +58,13 @@ sub PostgresNode::inet_connstr {
11458# ##############################################################################
11559
11660diag(" starting node 2" );
117- $nodes [2]-> start;
61+ $cluster -> { nodes } -> [2]-> start;
11862diag(" sleeping 30" );
11963sleep (30); # XXX: here we can poll
12064diag(" inserting 3" );
121- $nodes [0] -> psql(' postgres' , " insert into t values(3, 30);" );
65+ $cluster -> psql(0, ' postgres' , " insert into t values(3, 30);" );
12266diag(" selecting" );
123- $nodes [2] -> psql(' postgres' , " select v from t where k=3;" , stdout => \$psql_out );
67+ $cluster -> psql(2, ' postgres' , " select v from t where k=3;" , stdout => \$psql_out );
12468diag(" selected" );
12569is($psql_out , ' 30' , " Check replication after failed node recovery." );
12670
0 commit comments