|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | + |
| 4 | +use Cluster; |
| 5 | +use TestLib; |
| 6 | +use Test::More tests => 2; |
| 7 | + |
| 8 | +use DBI; |
| 9 | +use DBD::Pg ':async'; |
| 10 | + |
| 11 | +sub query_row |
| 12 | +{ |
| 13 | + my ($dbi, $sql, @keys) = @_; |
| 14 | + my $sth = $dbi->prepare($sql) || die; |
| 15 | + $sth->execute(@keys) || die; |
| 16 | + my $ret = $sth->fetchrow_array || undef; |
| 17 | + diag("query_row('$sql') -> $ret\n"); |
| 18 | + return $ret; |
| 19 | +} |
| 20 | + |
| 21 | +sub query_exec |
| 22 | +{ |
| 23 | + my ($dbi, $sql) = @_; |
| 24 | + my $rv = $dbi->do($sql) || die; |
| 25 | + diag("query_exec('$sql') = $rv\n"); |
| 26 | + return $rv; |
| 27 | +} |
| 28 | + |
| 29 | +sub query_exec_async |
| 30 | +{ |
| 31 | + my ($dbi, $sql) = @_; |
| 32 | + my $rv = $dbi->do($sql, {pg_async => PG_ASYNC}) || die; |
| 33 | + diag("query_exec_async('$sql')\n"); |
| 34 | + return $rv; |
| 35 | +} |
| 36 | + |
| 37 | +my $cluster = new Cluster(2); |
| 38 | + |
| 39 | +$cluster->init(); |
| 40 | +$cluster->configure(); |
| 41 | +$cluster->start(); |
| 42 | + |
| 43 | +my ($rc, $out, $err); |
| 44 | +sleep(10); |
| 45 | + |
| 46 | +$cluster->psql(0, 'postgres', "create table t(k int primary key, v text)"); |
| 47 | +$cluster->psql(0, 'postgres', "insert into t values (1, 'hello'), (2, 'world')"); |
| 48 | + |
| 49 | +my @conns = map { DBI->connect('DBI:Pg:' . $_->connstr()) } @{$cluster->{nodes}}; |
| 50 | + |
| 51 | +query_exec($conns[0], "begin"); |
| 52 | +query_exec($conns[1], "begin"); |
| 53 | + |
| 54 | +query_exec($conns[0], "update t set v = 'asd' where k = 1"); |
| 55 | +query_exec($conns[1], "update t set v = 'bsd'"); |
| 56 | + |
| 57 | +query_exec($conns[0], "update t set v = 'bar' where k = 2"); |
| 58 | +query_exec($conns[1], "update t set v = 'foo'"); |
| 59 | + |
| 60 | +query_exec_async($conns[0], "commit"); |
| 61 | +query_exec_async($conns[1], "commit"); |
| 62 | + |
| 63 | +my $timeout = 5; |
| 64 | +while ($timeout > 0) |
| 65 | +{ |
| 66 | + my $r0 = $conns[0]->pg_ready(); |
| 67 | + my $r1 = $conns[1]->pg_ready(); |
| 68 | + if ($r0 && $r1) { |
| 69 | + last; |
| 70 | + } |
| 71 | + diag("queries still running: [0]=$r0 [1]=$r1\n"); |
| 72 | + sleep(1); |
| 73 | +} |
| 74 | + |
| 75 | +if ($timeout > 0) |
| 76 | +{ |
| 77 | + diag("queries finished\n"); |
| 78 | + |
| 79 | + my $succeeded = 0; |
| 80 | + $succeeded++ if $conns[0]->pg_result(); |
| 81 | + $succeeded++ if $conns[1]->pg_result(); |
| 82 | + |
| 83 | + pass("queries finished"); |
| 84 | +} |
| 85 | +else |
| 86 | +{ |
| 87 | + diag("queries timed out\n"); |
| 88 | + $conns[0]->pg_cancel() unless $conns[0]->pg_ready(); |
| 89 | + $conns[1]->pg_cancel() unless $conns[1]->pg_ready(); |
| 90 | + |
| 91 | + fail("queries timed out"); |
| 92 | +} |
| 93 | + |
| 94 | +query_row($conns[0], "select * from t where k = 1"); |
| 95 | + |
| 96 | +ok($cluster->stop('fast'), "cluster stops"); |
| 97 | +1; |
0 commit comments