Skip to content

Commit 9ff6afe

Browse files
committed
Fix the destroy_cluster Perl routine.
When removing all the configuration, we must ensure that we drop all subscriptions before calling the node_drop() routine. Having issues with subscription naming revealed by three or more configuration tests led this routine to be more efficient: instead of making up names, simply pass through the list of subscriptions and remove each one of them. Also, multiple trailing spaces have been automatically removed with this commit.
1 parent cb48e17 commit 9ff6afe

File tree

1 file changed

+38
-43
lines changed

1 file changed

+38
-43
lines changed

tests/tap/t/SpockTest.pm

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ sub system_maybe {
139139
# Create PostgreSQL configuration file
140140
sub create_postgresql_conf {
141141
my ($datadir, $port) = @_;
142-
142+
143143
open(my $conf, '>>', "$datadir/postgresql.conf") or die "Cannot open config file: $!";
144144
print $conf "shared_buffers=1GB\n";
145145
print $conf "shared_preload_libraries='spock'\n";
@@ -154,7 +154,7 @@ sub create_postgresql_conf {
154154
print $conf "spock.enable_spill=on\n";
155155
print $conf "port=$port\n";
156156
print $conf "listen_addresses='*'\n";
157-
157+
158158
# Enable logging
159159
print $conf "logging_collector=on\n";
160160
my $cwd = Cwd::getcwd();
@@ -175,7 +175,7 @@ sub create_postgresql_conf {
175175
print $conf "log_replication_commands=on\n";
176176
print $conf "log_min_duration_statement=0\n";
177177
print $conf "log_statement_stats=on\n";
178-
178+
179179
close($conf);
180180
}
181181

@@ -184,93 +184,93 @@ sub create_cluster {
184184
my ($num_nodes, $test_name) = @_;
185185
$num_nodes //= 2;
186186
$test_name //= "Create $num_nodes-node Spock cluster";
187-
187+
188188
# Initialize arrays
189189
@node_ports = ();
190190
@node_datadirs = ();
191191
$node_count = $num_nodes;
192-
192+
193193
# Generate ports and datadirs for all nodes
194194
for (my $i = 0; $i < $num_nodes; $i++) {
195195
my $port = $BASE_PORT + $i;
196196
my $datadir = "${DATADIR_BASE}_${i}_datadir";
197197
push @node_ports, $port;
198198
push @node_datadirs, $datadir;
199199
}
200-
200+
201201
# Clean up any existing test directories
202202
for my $datadir (@node_datadirs) {
203203
system_or_bail 'rm', '-rf', $datadir;
204204
}
205-
205+
206206
# Initialize PostgreSQL data directories for all nodes
207207
for (my $i = 0; $i < $num_nodes; $i++) {
208208
system_or_bail "$PG_BIN/initdb", '-A', 'trust', '-D', $node_datadirs[$i];
209209
}
210-
210+
211211
# Create logs directory in current working directory
212212
my $current_dir = getcwd();
213213
my $logs_dir = "$current_dir/logs";
214214
system_or_bail 'mkdir', '-p', $logs_dir;
215215
system_or_bail 'chmod', '755', $logs_dir;
216-
216+
217217
# Copy configuration files if they exist
218218
if (-f 'regress-pg_hba.conf') {
219219
for my $datadir (@node_datadirs) {
220220
system_or_bail 'cp', 'regress-pg_hba.conf', "$datadir/pg_hba.conf";
221221
}
222222
}
223-
223+
224224
# Create PostgreSQL configuration for all nodes
225225
for (my $i = 0; $i < $num_nodes; $i++) {
226226
create_postgresql_conf($node_datadirs[$i], $node_ports[$i]);
227227
}
228-
228+
229229
# Start PostgreSQL instances for all nodes
230230
for (my $i = 0; $i < $num_nodes; $i++) {
231231
system("$PG_BIN/postgres -D $node_datadirs[$i] >> '$LOG_FILE' 2>&1 &");
232232
}
233-
233+
234234
# Allow PostgreSQL servers to startup
235235
system_or_bail 'sleep', '17';
236-
236+
237237
# Create superuser on all nodes (ignore if already exists)
238238
for (my $i = 0; $i < $num_nodes; $i++) {
239239
system_maybe "$PG_BIN/psql", '-p', $node_ports[$i], '-d', 'postgres', '-c', "CREATE USER super SUPERUSER";
240240
}
241-
241+
242242
# Create database and user for testing on all nodes (ignore if already exists)
243243
for (my $i = 0; $i < $num_nodes; $i++) {
244244
system_maybe "$PG_BIN/psql", '-p', $node_ports[$i], '-d', 'postgres', '-c', "CREATE DATABASE $DB_NAME";
245245
system_maybe "$PG_BIN/psql", '-p', $node_ports[$i], '-d', $DB_NAME, '-c', "CREATE USER $DB_USER SUPERUSER";
246246
}
247-
247+
248248
# Install Spock extension on all nodes
249249
for (my $i = 0; $i < $num_nodes; $i++) {
250250
system_or_bail "$PG_BIN/psql", '-p', $node_ports[$i], '-d', $DB_NAME, '-c', "CREATE EXTENSION IF NOT EXISTS spock";
251251
system_or_bail "$PG_BIN/psql", '-p', $node_ports[$i], '-d', $DB_NAME, '-c', "ALTER EXTENSION spock UPDATE";
252252
}
253-
253+
254254
# Test if PostgreSQL instances are running
255255
for (my $i = 0; $i < $num_nodes; $i++) {
256256
my $node_name = "n" . ($i + 1);
257257
command_ok([ "$PG_BIN/pg_isready", '-h', $HOST, '-p', $node_ports[$i], '-U', $DB_USER ], "$node_name is running");
258258
}
259-
259+
260260
# Test Spock extension on all nodes
261261
for (my $i = 0; $i < $num_nodes; $i++) {
262262
my $node_name = "n" . ($i + 1);
263263
command_ok([ "$PG_BIN/psql", '-p', $node_ports[$i], '-d', $DB_NAME, '-c', "SELECT 1 FROM pg_extension WHERE extname = 'spock'" ], "Check Spock extension on $node_name");
264264
}
265-
265+
266266
# Create nodes
267267
for (my $i = 0; $i < $num_nodes; $i++) {
268268
my $node_name = "n" . ($i + 1);
269269
system_or_bail "$PG_BIN/psql", '-p', $node_ports[$i], '-d', $DB_NAME, '-c', "SELECT spock.node_create('$node_name', 'host=$HOST dbname=$DB_NAME port=$node_ports[$i] user=$DB_USER password=$DB_PASSWORD')";
270270
}
271-
271+
272272
$nodes_created = 1;
273-
273+
274274
pass($test_name);
275275
return 1;
276276
}
@@ -308,7 +308,7 @@ sub cross_wire {
308308
for (my $i = 0; $i < $num_nodes; $i++) {
309309
my $node_name = $node_names->[$i];
310310
my $repset_name = "${node_name}r" . ($i + 1);
311-
311+
312312
system_or_bail(
313313
"$PG_BIN/psql",
314314
'-p', $node_ports[$i],
@@ -328,26 +328,26 @@ sub cross_wire {
328328
sub cross_wire_first_two {
329329
my ($test_name) = @_;
330330
$test_name //= 'Cross-wire first 2 nodes (n1 and n2)';
331-
331+
332332
die "No cluster created. Call create_cluster() first." unless $nodes_created;
333333
die "Need at least 2 nodes to cross-wire first two" unless $node_count >= 2;
334-
334+
335335
# Create subscriptions only between the first 2 nodes (n1 and n2)
336336
for (my $i = 0; $i < 2; $i++) {
337337
for (my $j = 0; $j < 2; $j++) {
338338
next if $i == $j; # Skip self-subscription
339-
339+
340340
my $source_node = "n" . ($i + 1);
341341
my $target_node = "n" . ($j + 1);
342342
my $sub_name = "sub_${source_node}_${target_node}";
343-
343+
344344
system_or_bail "$PG_BIN/psql", '-p', $node_ports[$i], '-d', $DB_NAME, '-c', "SELECT spock.sub_create('$sub_name', 'host=$HOST dbname=$DB_NAME port=$node_ports[$j] user=$DB_USER password=$DB_PASSWORD', ARRAY['default', 'default_insert_only', 'ddl_sql'], true, true)";
345345
}
346346
}
347-
347+
348348
# Wait for cross-wiring to complete
349349
system_or_bail 'sleep', '10';
350-
350+
351351
pass($test_name);
352352
return 1;
353353
}
@@ -356,45 +356,40 @@ sub cross_wire_first_two {
356356
sub destroy_cluster {
357357
my ($test_name) = @_;
358358
$test_name //= 'Destroy multi-node Spock cluster';
359-
359+
360360
if ($nodes_created) {
361361
# Cleanup subscriptions and nodes (ignore errors for subscriptions that don't exist)
362362
for (my $i = 0; $i < $node_count; $i++) {
363-
for (my $j = 0; $j < $node_count; $j++) {
364-
next if $i == $j; # Skip self-subscription
365-
366-
my $source_node = "n" . ($i + 1);
367-
my $target_node = "n" . ($j + 1);
368-
my $sub_name = "sub_${source_node}_${target_node}";
369-
370-
system_maybe "$PG_BIN/psql", '-p', $node_ports[$i], '-d', $DB_NAME, '-c', "SELECT spock.sub_drop('$sub_name')";
371-
}
363+
# Remove each subscription, created on the node
364+
system_maybe "$PG_BIN/psql", '-p', $node_ports[$i], '-d', $DB_NAME, '-c',
365+
"SELECT sub_name AS deleted_subscriptions,sub_origin,sub_target
366+
FROM spock.subscription s, LATERAL spock.sub_drop(s.sub_name)";
372367
}
373-
368+
374369
for (my $i = 0; $i < $node_count; $i++) {
375370
my $node_name = "n" . ($i + 1);
376371
system_or_bail "$PG_BIN/psql", '-p', $node_ports[$i], '-d', $DB_NAME, '-c', "SELECT spock.node_drop('$node_name')";
377372
}
378-
373+
379374
# Stop PostgreSQL instances
380375
for (my $i = 0; $i < $node_count; $i++) {
381376
system("$PG_BIN/pg_ctl stop -D $node_datadirs[$i] -m immediate >> '$LOG_FILE' 2>&1 &");
382377
}
383-
378+
384379
# Wait for processes to stop
385380
system_or_bail 'sleep', '5';
386-
381+
387382
# Clean up test directories
388383
for my $datadir (@node_datadirs) {
389384
system_or_bail 'rm', '-rf', $datadir;
390385
}
391-
386+
392387
$nodes_created = 0;
393388
$node_count = 0;
394389
@node_ports = ();
395390
@node_datadirs = ();
396391
}
397-
392+
398393
pass($test_name);
399394
return 1;
400395
}

0 commit comments

Comments
 (0)