33
44use PostgresNode;
55use TestLib;
6- use Test::Moretests => 2 ;
6+ use Test::Moretests => 1 ;
77
88use DBI;
99use DBD::Pg' :async' ;
@@ -14,23 +14,23 @@ sub query_row
1414my $sth =$dbi -> prepare($sql ) ||die ;
1515$sth -> execute(@keys ) ||die ;
1616my $ret =$sth -> fetchrow_array ||undef ;
17- print " query_row('$sql ') ->$ret \n " ;
17+ diag( " query_row('$sql ') ->$ret \n " ) ;
1818return $ret ;
1919}
2020
2121sub query_exec
2222{
2323my ($dbi ,$sql ) =@_ ;
2424my $rv =$dbi -> do($sql ) ||die ;
25- print " query_exec('$sql ') =$rv \n " ;
25+ diag( " query_exec('$sql ') =$rv \n " ) ;
2626return $rv ;
2727}
2828
2929sub query_exec_async
3030{
3131my ($dbi ,$sql ) =@_ ;
3232my $rv =$dbi -> do($sql , {pg_async => PG_ASYNC}) ||die ;
33- print " query_exec_async('$sql ') = $rv \n " ;
33+ diag( " query_exec_async('$sql ')\n " ) ;
3434return $rv ;
3535}
3636
@@ -44,7 +44,7 @@ sub allocate_ports
4444{
4545my $port =int (rand () * 16384) + 49152;
4646next if $allocated_ports {$port };
47- print " # Checking for port$port \n " ;
47+ diag( " Checking for port$port \n " ) ;
4848if (!TestLib::run_log([' pg_isready' ,' -h' ,$host ,' -p' ,$port ]))
4949{
5050$allocated_ports {$port } = 1;
@@ -73,8 +73,8 @@ sub allocate_ports
7373my $mm_connstr =join (' ,' ,map {" ${\$ _->connstr('postgres') }" }@nodes );
7474my $raft_peers =join (' ,' ,map {join (' :' ,$_ -> {id },$_ -> host,$_ -> {raftport }) }@nodes );
7575
76- print ( " # mm_connstr =$mm_connstr \n " );
77- print ( " # raft_peers =$raft_peers \n " );
76+ diag( " mm_connstr =$mm_connstr \n " );
77+ diag( " raft_peers =$raft_peers \n " );
7878
7979# Init and Configure
8080foreach my $node (@nodes )
@@ -125,33 +125,49 @@ sub allocate_ports
125125$nodes [0]-> psql(' postgres' ," create table t(k int primary key, v text)" );
126126$nodes [0]-> psql(' postgres' ," insert into t values (1, 'hello'), (2, 'world')" );
127127
128- # sub space2semicol
129- # {
130- # my $str = shift;
131- # $str =~ tr/ /;/;
132- # return $str;
133- # }
134- #
135128my @conns =map { DBI-> connect (' DBI:Pg:' .$_ -> connstr()) }@nodes ;
136129
137130query_exec($conns [0]," begin" );
138131query_exec($conns [1]," begin" );
139132
140133query_exec($conns [0]," update t set v = 'asd' where k = 1" );
141- query_exec($conns [1]," update t set v = 'bsd' where k = 2 " );
134+ query_exec($conns [1]," update t set v = 'bsd'" );
142135
143136query_exec($conns [0]," update t set v = 'bar' where k = 2" );
144- query_exec($conns [1]," update t set v = 'foo' where k = 1 " );
137+ query_exec($conns [1]," update t set v = 'foo'" );
145138
146139query_exec_async($conns [0]," commit" );
147140query_exec_async($conns [1]," commit" );
148141
149- for my $i (1..2)
142+ my $timeout = 5;
143+ while ($timeout > 0)
150144{
151- ($rc ,$out ,$err ) =$nodes [$i ]-> psql(' postgres' ," select * from t" );
152- print (" rc[$i ] =$rc \n " );
153- print (" out[$i ] =$out \n " );
154- print (" err[$i ] =$err \n " );
145+ my $r0 =$conns [0]-> pg_ready();
146+ my $r1 =$conns [1]-> pg_ready();
147+ if ($r0 &&$r1 ) {
148+ last ;
149+ }
150+ diag(" queries still running: [0]=$r0 [1]=$r1 \n " );
151+ sleep (1);
152+ }
153+
154+ if ($timeout > 0)
155+ {
156+ diag(" queries finished\n " );
157+
158+ my $succeeded = 0;
159+ $succeeded ++if $conns [0]-> pg_result();
160+ $succeeded ++if $conns [1]-> pg_result();
161+
162+ pass(" queries finished" );
163+ }
164+ else
165+ {
166+ diag(" queries timed out\n " );
167+ $conns [0]-> pg_cancel()unless $conns [0]-> pg_ready();
168+ $conns [1]-> pg_cancel()unless $conns [1]-> pg_ready();
169+
170+ fail(" queries timed out" );
155171}
156172
157- # sleep(2 );
173+ query_row( $conns [0], " select * from t where k = 1 " );