Skip to content

Commit a738f4c

Browse files
authored
test: enable async on RepoCase tests using start_enactment (audit #18) (#80)
1 parent f47ae41 commit a738f4c

13 files changed

Lines changed: 30 additions & 18 deletions

lib/coloured_flow/runner/enactment/enactment.ex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ defmodule ColouredFlow.Runner.Enactment do
9696
@spec start_link(options()) :: GenServer.on_start()
9797
def start_link(options) do
9898
enactment_id = Keyword.fetch!(options, :enactment_id)
99+
# Forward `$callers` from the calling process so collaborating processes
100+
# (e.g., Ecto.Adapters.SQL.Sandbox-aware tests) can be tracked. See
101+
# `ExUnit.Callbacks.start_supervised/2` for the recommended pattern.
102+
options = Keyword.put_new(options, :"$callers", Process.get(:"$callers", []))
99103

100104
GenServer.start_link(
101105
__MODULE__,
@@ -107,10 +111,13 @@ defmodule ColouredFlow.Runner.Enactment do
107111

108112
@impl GenServer
109113
def init(options) do
110-
state =
111-
__MODULE__
112-
|> struct(options)
113-
|> Map.put(:hibernate_after, Lifespan.hibernate_after_from_options(options))
114+
{callers, options} = Keyword.pop(options, :"$callers", [])
115+
Process.put(:"$callers", callers)
116+
117+
options =
118+
Keyword.put(options, :hibernate_after, Lifespan.hibernate_after_from_options(options))
119+
120+
state = struct(__MODULE__, options)
114121

115122
{:ok, state, {:continue, :populate_state}}
116123
end

test/coloured_flow/runner/enactment/enactment_termination_and_exception_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ColouredFlow.Runner.Enactment.EnactmentTerminationAndExceptionTest do
2-
use ColouredFlow.RepoCase
2+
use ColouredFlow.RepoCase, async: true
33
use ColouredFlow.RunnerHelpers
44

55
import ColouredFlow.MultiSet, only: :sigils

test/coloured_flow/runner/enactment/enactment_test.exs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ColouredFlow.Runner.EnactmentTest do
2-
use ColouredFlow.RepoCase
2+
use ColouredFlow.RepoCase, async: true
33
use ColouredFlow.RunnerHelpers
44

55
alias ColouredFlow.Enactment.BindingElement
@@ -304,6 +304,7 @@ defmodule ColouredFlow.Runner.EnactmentTest do
304304

305305
test "coalesces a burst of :take_snapshot messages into fewer storage writes",
306306
%{enactment: enactment} do
307+
enactment_id = enactment.id
307308
[enactment_server: enactment_server] = start_enactment(%{enactment: enactment})
308309

309310
# Wait until the boot-time snapshot from `handle_continue` has been emitted
@@ -317,8 +318,12 @@ defmodule ColouredFlow.Runner.EnactmentTest do
317318
:telemetry.attach(
318319
handler_id,
319320
[:coloured_flow, :runner, :enactment, :take_snapshot],
320-
fn _event, _measurements, _metadata, _config ->
321-
send(self_pid, :snapshot_taken)
321+
fn _event, _measurements, metadata, _config ->
322+
# Filter by enactment_id so concurrent async tests don't pollute
323+
# this test's measurement.
324+
if metadata.enactment_id == enactment_id do
325+
send(self_pid, :snapshot_taken)
326+
end
322327
end,
323328
nil
324329
)

test/coloured_flow/runner/enactment/integration_tests/constants_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ColouredFlow.Runner.Enactment.IntegrationTests.ConstantsTest do
2-
use ColouredFlow.RepoCase
2+
use ColouredFlow.RepoCase, async: true
33
use ColouredFlow.RunnerHelpers
44

55
alias ColouredFlow.Enactment.BindingElement

test/coloured_flow/runner/enactment/integration_tests/pattern_match_on_complex_tokens_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ColouredFlow.Runner.Enactment.IntegrationTests.PatternMatchOnComplexTokensTest do
2-
use ColouredFlow.RepoCase
2+
use ColouredFlow.RepoCase, async: true
33
use ColouredFlow.RunnerHelpers
44

55
alias ColouredFlow.Enactment.BindingElement

test/coloured_flow/runner/enactment/integration_tests/reuse_workitems_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ColouredFlow.Runner.Enactment.IntegrationTests.ReuseWorkitemsTest do
2-
use ColouredFlow.RepoCase
2+
use ColouredFlow.RepoCase, async: true
33
use ColouredFlow.RunnerHelpers
44

55
alias ColouredFlow.Enactment.BindingElement

test/coloured_flow/runner/enactment/integration_tests/transition_without_input_places_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ColouredFlow.Runner.Enactment.IntegrationTests.TransitionWithoutInputPlacesTest do
2-
use ColouredFlow.RepoCase
2+
use ColouredFlow.RepoCase, async: true
33
use ColouredFlow.RunnerHelpers
44

55
alias ColouredFlow.Enactment.BindingElement

test/coloured_flow/runner/enactment/integration_tests/transition_without_output_places_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ColouredFlow.Runner.Enactment.IntegrationTests.TransitionWithoutOutputPlacesTest do
2-
use ColouredFlow.RepoCase
2+
use ColouredFlow.RepoCase, async: true
33
use ColouredFlow.RunnerHelpers
44

55
alias ColouredFlow.Enactment.BindingElement

test/coloured_flow/runner/enactment/integration_tests/transition_without_token_consumed_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ColouredFlow.Runner.Enactment.IntegrationTests.TransitionWithoutTokenConsumedTest do
2-
use ColouredFlow.RepoCase
2+
use ColouredFlow.RepoCase, async: true
33
use ColouredFlow.RunnerHelpers
44

55
alias ColouredFlow.Enactment.BindingElement

test/coloured_flow/runner/enactment/integration_tests/transition_without_token_produced_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ColouredFlow.Runner.Enactment.IntegrationTests.TransitionWithoutTokenProducedTest do
2-
use ColouredFlow.RepoCase
2+
use ColouredFlow.RepoCase, async: true
33
use ColouredFlow.RunnerHelpers
44

55
alias ColouredFlow.Enactment.BindingElement

0 commit comments

Comments
 (0)