|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | + |
| 4 | +use PostgresNode; |
| 5 | +use TestLib; |
| 6 | +use Test::More tests => 4; |
| 7 | + |
| 8 | +sub genstr |
| 9 | +{ |
| 10 | + my $len = shift; |
| 11 | + my @chars = ("A".."Z", "a".."z"); |
| 12 | + my $string; |
| 13 | + $string .= $chars[rand @chars] for 1..$len; |
| 14 | + return $string; |
| 15 | +} |
| 16 | + |
| 17 | +sub create_nodes |
| 18 | +{ |
| 19 | + my $nodenum = shift; |
| 20 | + my $nodesref = {}; |
| 21 | + my @cfg = (); |
| 22 | + for (my $i = 0; $i < $nodenum; $i++) |
| 23 | + { |
| 24 | + $nodesref->{$i} = get_new_node(); |
| 25 | + my $extranode = get_new_node(); # just to find an extra port for raft |
| 26 | + push @cfg, "$i:127.0.0.1:${\$extranode->port}" |
| 27 | + } |
| 28 | + return $nodesref, join(',', @cfg); |
| 29 | +} |
| 30 | + |
| 31 | +sub init_nodes |
| 32 | +{ |
| 33 | + my ($nodesref, $cfg) = @_; |
| 34 | + print("cfg = $cfg\n"); |
| 35 | + while (my ($id, $node) = each(%$nodesref)) |
| 36 | + { |
| 37 | + $node->init; |
| 38 | + $node->append_conf("postgresql.conf", qq( |
| 39 | +shared_preload_libraries = raftable |
| 40 | +raftable.id = $id |
| 41 | +raftable.peers = '$cfg' |
| 42 | +)); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +sub start_nodes |
| 47 | +{ |
| 48 | + my $nodesref = shift; |
| 49 | + while (my ($id, $node) = each(%$nodesref)) |
| 50 | + { |
| 51 | + $node->start; |
| 52 | + $node->psql('postgres', "create extension raftable;"); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +my ($nodesref, $cfg) = create_nodes(3); |
| 57 | +init_nodes($nodesref, $cfg); |
| 58 | +start_nodes($nodesref); |
| 59 | + |
| 60 | +my $able = $nodesref->{0}; |
| 61 | +my $baker = $nodesref->{1}; |
| 62 | +my $charlie = $nodesref->{2}; |
| 63 | + |
| 64 | +my %tests = ( |
| 65 | + hello => genstr(1), |
| 66 | + and => genstr(100), |
| 67 | + goodbye => genstr(1000), |
| 68 | + world => genstr(3000), |
| 69 | +); |
| 70 | + |
| 71 | +$able->psql('postgres', "select raftable('hello', '$tests{hello}');"); |
| 72 | +$baker->psql('postgres', "select raftable('and', '$tests{and}');"); |
| 73 | +$charlie->psql('postgres', "select raftable('goodbye', '$tests{goodbye}');"); |
| 74 | +#$baker->stop; |
| 75 | +$able->psql('postgres', "select raftable('world', '$tests{world}');"); |
| 76 | + |
| 77 | +#$baker->start; |
| 78 | +while (my ($key, $value) = each(%tests)) |
| 79 | +{ |
| 80 | + my $o = $baker->psql('postgres', "select raftable('$key');"); |
| 81 | + is($o, $value, "Check that baker has all the state replicated"); |
| 82 | +} |
| 83 | + |
| 84 | +exit(0); |
0 commit comments