Skip to content

Commit 32a164c

Browse files
authored
chore: Ensure test processes are supervised by the test supervisor (#2841)
Ensures that all processes started are started under the ExUnit test supervisor, in the hope that this gives us better guarantees about cleanups occurring at the right time and we reduce flakes that seem to potentially be related to timing issues.
1 parent 7267fe3 commit 32a164c

15 files changed

Lines changed: 188 additions & 161 deletions

packages/sync-service/lib/electric/postgres/replication_client.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ defmodule Electric.Postgres.ReplicationClient do
215215
{:noreply, state}
216216
end
217217

218+
# This callback is invoked when the connection process receives a shutdown signal.
219+
def handle_info({:EXIT, _pid, :shutdown}, _state) do
220+
Logger.debug("Replication client #{inspect(self())} received shutdown signal, stopping")
221+
{:disconnect, :shutdown}
222+
end
223+
218224
# The implementation of Postgrex.ReplicationConnection doesn't give us a convenient way to
219225
# check whether the START_REPLICATION_SLOT statement succeeded before switching the
220226
# connection into streaming mode. Returning {:query, "START_REPLICATION_SLOT ...", state}

packages/sync-service/test/electric/concurrent_stream_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule Electric.ConcurrentStreamTest do
77
describe "stream_to_end/2" do
88
setup %{tmp_dir: tmp_dir, test: test} do
99
db = :"cubdb_#{test}"
10-
CubDB.start_link(data_dir: tmp_dir, name: db)
10+
start_link_supervised!({CubDB, data_dir: tmp_dir, name: db})
1111
{:ok, %{db: db}}
1212
end
1313

packages/sync-service/test/electric/connection/manager_test.exs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,20 @@ defmodule Electric.Connection.ConnectionManagerTest do
102102
wait_until_active(stack_id)
103103

104104
# Start another lock process so that when ConnectionManager exits it is not able to restore its readiness immediately.
105+
new_stack_id = stack_id <> "_new"
106+
_registry = start_link_supervised!({Electric.ProcessRegistry, stack_id: new_stack_id})
107+
105108
lock_opts = [
106109
connection_opts: ctx.connection_opts,
107110
connection_manager: self(),
108111
lock_name: Keyword.fetch!(ctx.replication_opts, :slot_name),
109-
stack_id: stack_id
112+
stack_id: new_stack_id
110113
]
111114

112-
Electric.Postgres.LockConnection.start_link(lock_opts)
115+
start_supervised!(%{
116+
id: :alt_lock,
117+
start: {Electric.Postgres.LockConnection, :start_link, [lock_opts]}
118+
})
113119

114120
monitor = monitor_replication_client(stack_id)
115121

packages/sync-service/test/electric/postgres/lock_connection_test.exs

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ defmodule Electric.Postgres.LockConnectionTest do
1818
} do
1919
log =
2020
capture_log(fn ->
21-
assert {:ok, _pid} =
22-
LockConnection.start_link(
23-
connection_opts: config,
24-
connection_manager: self(),
25-
lock_name: @lock_name,
26-
stack_id: stack_id
27-
)
21+
start_supervised!(%{
22+
id: :lock1,
23+
start:
24+
{LockConnection, :start_link,
25+
[
26+
[
27+
connection_opts: config,
28+
connection_manager: self(),
29+
lock_name: @lock_name,
30+
stack_id: stack_id
31+
]
32+
]}
33+
})
2834

2935
assert_lock_acquired()
3036
end)
@@ -44,33 +50,45 @@ defmodule Electric.Postgres.LockConnectionTest do
4450

4551
test "should wait if lock is already acquired", %{db_config: config, stack_id: stack_id} do
4652
# grab lock with one connection
47-
assert {:ok, pid1} =
48-
LockConnection.start_link(
49-
connection_opts: config,
50-
connection_manager: self(),
51-
lock_name: @lock_name,
52-
stack_id: stack_id
53-
)
53+
start_supervised!(%{
54+
id: :lock1,
55+
start:
56+
{LockConnection, :start_link,
57+
[
58+
[
59+
connection_opts: config,
60+
connection_manager: self(),
61+
lock_name: @lock_name,
62+
stack_id: stack_id
63+
]
64+
]}
65+
})
5466

5567
assert_lock_acquired()
5668

5769
# try to grab the same lock using a different connection
5870
new_stack_id = stack_id <> "_new"
59-
_registry = start_link_supervised!({Electric.ProcessRegistry, stack_id: new_stack_id})
71+
start_link_supervised!({Electric.ProcessRegistry, stack_id: new_stack_id})
6072

61-
assert {:ok, _pid} =
62-
LockConnection.start_link(
63-
connection_opts: config,
64-
connection_manager: self(),
65-
lock_name: @lock_name,
66-
stack_id: new_stack_id
67-
)
73+
start_supervised!(%{
74+
id: :lock2,
75+
start:
76+
{LockConnection, :start_link,
77+
[
78+
[
79+
connection_opts: config,
80+
connection_manager: self(),
81+
lock_name: @lock_name,
82+
stack_id: new_stack_id
83+
]
84+
]}
85+
})
6886

6987
# should fail to grab it
7088
refute_lock_acquired()
7189

7290
# should immediately grab it once previous lock is released
73-
GenServer.stop(pid1)
91+
stop_supervised!(:lock1)
7492
assert_lock_acquired()
7593
end
7694
end

packages/sync-service/test/electric/postgres/replication_client_test.exs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,14 @@ defmodule Electric.Postgres.ReplicationClientTest do
470470
defp start_client(ctx, overrides \\ []) do
471471
ctx = Enum.into(overrides, ctx)
472472

473-
{:ok, client_pid} =
474-
ReplicationClient.start_link(
475-
stack_id: ctx.stack_id,
476-
replication_opts: ctx.replication_opts
477-
)
473+
client_pid =
474+
start_link_supervised!(%{
475+
id: ReplicationClient,
476+
start:
477+
{ReplicationClient, :start_link,
478+
[[stack_id: ctx.stack_id, replication_opts: ctx.replication_opts]]},
479+
restart: :temporary
480+
})
478481

479482
conn_mgr = ctx.connection_manager
480483
assert_receive {^conn_mgr, :streaming_started}, @assert_receive_db_timeout

packages/sync-service/test/electric/process_registry_test.exs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ defmodule Electric.ProcessRegistryTest do
66

77
describe "alive?/2" do
88
test "should return false for inexistent process" do
9-
{:ok, _} =
10-
ProcessRegistry.start_link(
11-
name: ProcessRegistry.registry_name(@stack_id),
12-
keys: :duplicate,
13-
stack_id: @stack_id
14-
)
9+
start_link_supervised!(
10+
{ProcessRegistry,
11+
name: ProcessRegistry.registry_name(@stack_id), keys: :duplicate, stack_id: @stack_id}
12+
)
1513

1614
assert false == ProcessRegistry.alive?(@stack_id, "bar")
1715
end

packages/sync-service/test/electric/replication/publication_manager_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ defmodule Electric.Replication.PublicationManagerTest do
240240
ctx: ctx,
241241
opts: opts
242242
} do
243-
GenServer.stop(opts[:server])
243+
stop_supervised!(opts[:server])
244244

245245
test_id = self()
246246

packages/sync-service/test/electric/replication/shape_log_collector_test.exs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ defmodule Electric.Replication.ShapeLogCollectorTest do
8181
registry: registry_name
8282
]
8383

84-
{:ok, shape_cache_pid} = Electric.ShapeCache.start_link(shape_cache_opts)
84+
shape_cache_pid = start_link_supervised!({Electric.ShapeCache, shape_cache_opts})
8585

8686
%{server: pid, registry: registry_name, shape_cache: shape_cache_pid}
8787
end
@@ -103,13 +103,14 @@ defmodule Electric.Replication.ShapeLogCollectorTest do
103103

104104
consumers =
105105
Enum.map(1..3, fn id ->
106-
{:ok, consumer} =
107-
Support.TransactionConsumer.start_link(
108-
id: id,
109-
parent: parent,
110-
producer: ctx.server,
111-
shape: @shape
112-
)
106+
consumer =
107+
start_link_supervised!(%{
108+
id: {:consumer, id},
109+
start:
110+
{Support.TransactionConsumer, :start_link,
111+
[[id: id, parent: parent, producer: ctx.server, shape: @shape]]},
112+
restart: :temporary
113+
})
113114

114115
{id, consumer}
115116
end)
@@ -240,13 +241,14 @@ defmodule Electric.Replication.ShapeLogCollectorTest do
240241

241242
consumers =
242243
Enum.map(1..3, fn id ->
243-
{:ok, consumer} =
244-
Support.TransactionConsumer.start_link(
245-
id: id,
246-
parent: parent,
247-
producer: ctx.server,
248-
shape: @shape
249-
)
244+
consumer =
245+
start_link_supervised!(%{
246+
id: {:consumer, id},
247+
start:
248+
{Support.TransactionConsumer, :start_link,
249+
[[id: id, parent: parent, producer: ctx.server, shape: @shape]]},
250+
restart: :temporary
251+
})
250252

251253
{id, consumer}
252254
end)
@@ -338,12 +340,10 @@ defmodule Electric.Replication.ShapeLogCollectorTest do
338340

339341
consumer_id = "test_consumer"
340342

341-
{:ok, consumer} =
342-
Support.TransactionConsumer.start_link(
343-
id: consumer_id,
344-
parent: self(),
345-
producer: pid,
346-
shape: @shape
343+
consumer =
344+
start_link_supervised!(
345+
{Support.TransactionConsumer,
346+
id: consumer_id, parent: self(), producer: pid, shape: @shape}
347347
)
348348

349349
consumers = [{consumer_id, consumer}]

packages/sync-service/test/electric/shape_cache/file_storage_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule Electric.ShapeCache.FileStorageTest do
2727
)
2828

2929
shape_opts = FileStorage.for_shape(@shape_handle, opts)
30-
{:ok, pid} = FileStorage.start_link(shape_opts)
30+
pid = start_link_supervised!({FileStorage, shape_opts})
3131
{:ok, %{opts: shape_opts, shared_opts: opts, pid: pid, storage: {FileStorage, shape_opts}}}
3232
end
3333

packages/sync-service/test/electric/shape_cache/storage_implementations_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ defmodule Electric.ShapeCache.StorageImplimentationsTest do
753753
defp start_storage(%{module: module} = context) do
754754
opts = module |> opts(context) |> module.shared_opts()
755755
shape_opts = module.for_shape(@shape_handle, opts)
756-
{:ok, pid} = module.start_link(shape_opts)
756+
pid = start_link_supervised!({module, shape_opts})
757757
{:ok, %{opts: shape_opts, shared_opts: opts, pid: pid, storage: {module, shape_opts}}}
758758
end
759759

0 commit comments

Comments
 (0)