Skip to content

Commit debdda9

Browse files
committed
clean up tests
1 parent 1c54dfd commit debdda9

File tree

2 files changed

+82
-59
lines changed

2 files changed

+82
-59
lines changed

contrib/postgres_fdw/t/001_bank_check.pl renamed to contrib/postgres_fdw/t/001_bank_coordinator.pl

Lines changed: 68 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,71 @@
3232
# Prepare nodes
3333
###############################################################################
3434

35-
$master->psql('postgres', "CREATE EXTENSION postgres_fdw");
36-
$master->psql('postgres', "CREATE TABLE accounts(id integer primary key, amount integer)");
37-
$master->psql('postgres', "CREATE TABLE global_transactions(tx_time timestamp)");
35+
$master->safe_psql('postgres', qq[
36+
CREATE EXTENSION postgres_fdw;
37+
CREATE TABLE accounts(id integer primary key, amount integer);
38+
CREATE TABLE global_transactions(tx_time timestamp);
39+
]);
3840

3941
foreach my $node ($shard1, $shard2)
4042
{
4143
my $port = $node->port;
4244
my $host = $node->host;
4345

44-
$node->psql('postgres', "CREATE TABLE accounts(id integer primary key, amount integer)");
46+
$node->safe_psql('postgres',
47+
"CREATE TABLE accounts(id integer primary key, amount integer)");
4548

46-
$master->psql('postgres', "CREATE SERVER shard_$port FOREIGN DATA WRAPPER postgres_fdw options(dbname 'postgres', host '$host', port '$port')");
47-
$master->psql('postgres', "CREATE FOREIGN TABLE accounts_fdw_$port() inherits (accounts) server shard_$port options(table_name 'accounts')");
48-
$master->psql('postgres', "CREATE USER MAPPING for stas SERVER shard_$port options (user 'stas')");
49+
$master->safe_psql('postgres', qq[
50+
CREATE SERVER shard_$port FOREIGN DATA WRAPPER postgres_fdw options(dbname 'postgres', host '$host', port '$port');
51+
CREATE FOREIGN TABLE accounts_fdw_$port() inherits (accounts) server shard_$port options(table_name 'accounts');
52+
CREATE USER MAPPING for stas SERVER shard_$port options (user 'stas');
53+
])
4954
}
5055

51-
$shard1->psql('postgres', "insert into accounts select 2*id-1, 0 from generate_series(1, 10010) as id;");
52-
$shard1->psql('postgres', "CREATE TABLE local_transactions(tx_time timestamp)");
56+
$shard1->safe_psql('postgres', qq[
57+
insert into accounts select 2*id-1, 0 from generate_series(1, 10010) as id;
58+
CREATE TABLE local_transactions(tx_time timestamp);
59+
]);
5360

54-
$shard2->psql('postgres', "insert into accounts select 2*id, 0 from generate_series(1, 10010) as id;");
55-
$shard2->psql('postgres', "CREATE TABLE local_transactions(tx_time timestamp)");
61+
$shard2->safe_psql('postgres', qq[
62+
insert into accounts select 2*id, 0 from generate_series(1, 10010) as id;
63+
CREATE TABLE local_transactions(tx_time timestamp);
64+
]);
5665

57-
$master->pgbench(-n, -c => 20, -t => 30, -f => "$TestLib::log_path/../../t/bank.sql", 'postgres' );
66+
###############################################################################
67+
# pgbench scripts
68+
###############################################################################
69+
70+
my $bank = File::Temp->new();
71+
append_to_file($bank, q{
72+
\set id random(1, 20000)
73+
BEGIN;
74+
WITH upd AS (UPDATE accounts SET amount = amount - 1 WHERE id = :id RETURNING *)
75+
INSERT into global_transactions SELECT now() FROM upd;
76+
UPDATE accounts SET amount = amount + 1 WHERE id = (:id + 1);
77+
COMMIT;
78+
});
79+
80+
my $bank1 = File::Temp->new();
81+
append_to_file($bank1, q{
82+
\set id random(1, 10000)
83+
BEGIN;
84+
WITH upd AS (UPDATE accounts SET amount = amount - 1 WHERE id = (2*:id + 1) RETURNING *)
85+
INSERT into local_transactions SELECT now() FROM upd;
86+
UPDATE accounts SET amount = amount + 1 WHERE id = (2*:id + 3);
87+
COMMIT;
88+
});
89+
90+
my $bank2 = File::Temp->new();
91+
append_to_file($bank2, q{
92+
\set id random(1, 10000)
93+
94+
BEGIN;
95+
WITH upd AS (UPDATE accounts SET amount = amount - 1 WHERE id = 2*:id RETURNING *)
96+
INSERT into local_transactions SELECT now() FROM upd;
97+
UPDATE accounts SET amount = amount + 1 WHERE id = (2*:id + 2);
98+
COMMIT;
99+
});
58100

59101
###############################################################################
60102
# Helpers
@@ -63,17 +105,11 @@
63105
sub count_and_delete_rows
64106
{
65107
my ($node, $table) = @_;
66-
my ($rc, $count, $err);
67-
68-
($rc, $count, $err) = $node->psql('postgres',"select count(*) from $table",
69-
on_error_die => 1);
70-
71-
die "count_rows: $err" if ($err ne '');
72-
73-
$node->psql('postgres',"delete from $table", on_error_die => 1);
108+
my $count;
74109

110+
$count = $node->safe_psql('postgres',"select count(*) from $table");
111+
$node->safe_psql('postgres',"delete from $table");
75112
diag($node->name, ": completed $count transactions");
76-
77113
return $count;
78114
}
79115

@@ -92,20 +128,20 @@ sub count_and_delete_rows
92128

93129
my $pgb_handle;
94130

95-
$pgb_handle = $master->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank.sql", 'postgres' );
131+
$pgb_handle = $master->pgbench_async(-n, -c => 5, -T => $seconds, -f => $bank, 'postgres' );
96132

97133
$started = time();
98134
$selects = 0;
99135
while (time() - $started < $seconds)
100136
{
101-
($rc, $total, $err) = $master->psql('postgres', "select sum(amount) from accounts");
137+
$total = $master->safe_psql('postgres', "select sum(amount) from accounts");
102138
if ( ($total ne $oldtotal) and ($total ne '') )
103139
{
104140
$isolation_errors++;
105141
$oldtotal = $total;
106142
diag("Isolation error. Total = $total");
107143
}
108-
if (($err eq '') and ($total ne '') ) { $selects++; }
144+
if ($total ne '') { $selects++; }
109145
}
110146

111147
$master->pgbench_await($pgb_handle);
@@ -124,25 +160,25 @@ sub count_and_delete_rows
124160
my ($pgb_handle1, $pgb_handle2, $pgb_handle3);
125161

126162
# global txses
127-
$pgb_handle1 = $master->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank.sql", 'postgres' );
163+
$pgb_handle1 = $master->pgbench_async(-n, -c => 5, -T => $seconds, -f => $bank, 'postgres' );
128164

129165
# concurrent local
130-
$pgb_handle2 = $shard1->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank1.sql", 'postgres' );
131-
$pgb_handle3 = $shard2->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank2.sql", 'postgres' );
166+
$pgb_handle2 = $shard1->pgbench_async(-n, -c => 5, -T => $seconds, -f => $bank1, 'postgres' );
167+
$pgb_handle3 = $shard2->pgbench_async(-n, -c => 5, -T => $seconds, -f => $bank2, 'postgres' );
132168

133169
$started = time();
134170
$selects = 0;
135171
$oldtotal = 0;
136172
while (time() - $started < $seconds)
137173
{
138-
($rc, $total, $err) = $master->psql('postgres', "select sum(amount) from accounts");
174+
$total = $master->safe_psql('postgres', "select sum(amount) from accounts");
139175
if ( ($total ne $oldtotal) and ($total ne '') )
140176
{
141177
$isolation_errors++;
142178
$oldtotal = $total;
143179
diag("Isolation error. Total = $total");
144180
}
145-
if (($err eq '') and ($total ne '') ) { $selects++; }
181+
if ($total ne '') { $selects++; }
146182
}
147183

148184
diag("selects = $selects");
@@ -168,10 +204,10 @@ sub count_and_delete_rows
168204
my $stable;
169205

170206
# global txses
171-
$pgb_handle1 = $master->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank.sql", 'postgres' );
207+
$pgb_handle1 = $master->pgbench_async(-n, -c => 5, -T => $seconds, -f => $bank, 'postgres' );
172208
# concurrent local
173-
$pgb_handle2 = $shard1->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank1.sql", 'postgres' );
174-
$pgb_handle3 = $shard2->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank2.sql", 'postgres' );
209+
$pgb_handle2 = $shard1->pgbench_async(-n, -c => 5, -T => $seconds, -f => $bank1, 'postgres' );
210+
$pgb_handle3 = $shard2->pgbench_async(-n, -c => 5, -T => $seconds, -f => $bank2, 'postgres' );
175211

176212
$selects = 0;
177213
$started = time();

contrib/postgres_fdw/t/0001_sharded_data.pl renamed to contrib/postgres_fdw/t/002_bank_participant.pl

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@
55
use TestLib;
66
use Test::More tests => 1;
77

8-
# my $master = get_new_node("master");
9-
# $master->init;
10-
# $master->append_conf('postgresql.conf', qq(
11-
# max_prepared_transactions = 30
12-
# log_checkpoints = true
13-
# postgres_fdw.use_tsdtm = on
14-
# ));
15-
# $master->start;
16-
178
my $shard1 = get_new_node("shard1");
189
$shard1->init;
1910
$shard1->append_conf('postgresql.conf', qq(
@@ -58,26 +49,22 @@
5849

5950
}
6051

61-
diag("\n");
62-
diag( $shard1->connstr('postgres'), "\n" );
63-
diag( $shard2->connstr('postgres'), "\n" );
64-
6552
$shard1->psql('postgres', "insert into accounts_local select 2*id-1, 0 from generate_series(1, 10010) as id;");
6653
$shard2->psql('postgres', "insert into accounts_local select 2*id, 0 from generate_series(1, 10010) as id;");
6754

68-
diag("\n");
69-
diag( $shard1->connstr('postgres'), "\n" );
70-
diag( $shard2->connstr('postgres'), "\n" );
71-
72-
#sleep(6000);
73-
74-
$shard1->pgbench(-n, -c => 20, -t => 30, -f => "$TestLib::log_path/../../t/bank.sql", 'postgres' );
75-
$shard2->pgbench(-n, -c => 20, -t => 30, -f => "$TestLib::log_path/../../t/bank.sql", 'postgres' );
55+
###############################################################################
56+
# pgbench scripts
57+
###############################################################################
7658

77-
diag("\n");
78-
diag( $shard1->connstr('postgres'), "\n" );
79-
diag( $shard2->connstr('postgres'), "\n" );
80-
# sleep(3600);
59+
my $bank = File::Temp->new();
60+
append_to_file($bank, q{
61+
\set id random(1, 20000)
62+
BEGIN;
63+
WITH upd AS (UPDATE accounts SET amount = amount - 1 WHERE id = :id RETURNING *)
64+
INSERT into global_transactions SELECT now() FROM upd;
65+
UPDATE accounts SET amount = amount + 1 WHERE id = (:id + 1);
66+
COMMIT;
67+
});
8168

8269
###############################################################################
8370
# Helpers
@@ -115,8 +102,8 @@ sub count_and_delete_rows
115102

116103
my ($pgb_handle1, $pgb_handle2);
117104

118-
$pgb_handle1 = $shard1->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank.sql", 'postgres' );
119-
$pgb_handle2 = $shard2->pgbench_async(-n, -c => 5, -T => $seconds, -f => "$TestLib::log_path/../../t/bank.sql", 'postgres' );
105+
$pgb_handle1 = $shard1->pgbench_async(-n, -c => 5, -T => $seconds, -f => $bank, 'postgres' );
106+
$pgb_handle2 = $shard2->pgbench_async(-n, -c => 5, -T => $seconds, -f => $bank, 'postgres' );
120107

121108
$started = time();
122109
$selects = 0;

0 commit comments

Comments
 (0)