Skip to content

Commit

Permalink
Reduce test flake (#2168)
Browse files Browse the repository at this point in the history
  • Loading branch information
alco authored Dec 16, 2024
1 parent b479bfa commit e325ac3
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions packages/sync-service/test/electric/plug/serve_shape_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ defmodule Electric.Plug.ServeShapePlugTest do
@start_offset_50 LogOffset.new(Lsn.from_integer(50), 0)
@test_pg_id "12345"

# Higher timeout is needed for some tests that tend to run slower on CI.
@receive_timeout 1000

def load_column_info({"public", "users"}, _),
do: {:ok, @test_shape.table_info[{"public", "users"}][:columns]}

Expand Down Expand Up @@ -453,9 +456,7 @@ defmodule Electric.Plug.ServeShapePlugTest do
|> call_serve_shape_plug(ctx)
end)

# Raised timeout here because sometimes, rarely, the task takes a little while to reach this point
assert_receive :got_log_stream, 300
Process.sleep(50)
assert_receive :got_log_stream, @receive_timeout

# Simulate new changes arriving
Registry.dispatch(@registry, @test_shape_handle, fn [{pid, ref}] ->
Expand Down Expand Up @@ -512,9 +513,7 @@ defmodule Electric.Plug.ServeShapePlugTest do
|> call_serve_shape_plug(ctx)
end)

# Raised timeout here because sometimes, rarely, the task takes a little while to reach this point
assert_receive :got_log_stream, 300
Process.sleep(50)
assert_receive :got_log_stream, @receive_timeout

# Simulate shape rotation
Registry.dispatch(@registry, @test_shape_handle, fn [{pid, ref}] ->
Expand Down Expand Up @@ -757,7 +756,8 @@ defmodule Electric.Plug.ServeShapePlugTest do
|> call_serve_shape_plug(ctx)
end)

Process.sleep(50)
# Wait for the task process to subscribe to stack events
wait_until_subscribed(ctx.stack_id, 50, 4)

Electric.StackSupervisor.dispatch_stack_event(Registry.StackEvents, ctx.stack_id, :ready)

Expand All @@ -783,4 +783,17 @@ defmodule Electric.Plug.ServeShapePlugTest do
defp max_age(ctx), do: Access.get(ctx, :max_age, 60)
defp stale_age(ctx), do: Access.get(ctx, :stale_age, 300)
defp long_poll_timeout(ctx), do: Access.get(ctx, :long_poll_timeout, 20_000)

defp wait_until_subscribed(stack_id, _sleep, 0) do
raise "Timed out waiting for a process to subscribe to stack events in stack \"#{stack_id}\""
end

defp wait_until_subscribed(stack_id, sleep, num_attempts) do
if Registry.lookup(Registry.StackEvents, {:stack_status, stack_id}) != [] do
:ok
else
Process.sleep(sleep)
wait_until_subscribed(stack_id, sleep, num_attempts - 1)
end
end
end

0 comments on commit e325ac3

Please sign in to comment.