Skip to content

Commit

Permalink
improve resource guard test (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-hek authored Mar 18, 2024
1 parent 2ed8f4d commit aef442d
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions test/membrane/resource_guard_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ defmodule Membrane.ResourceGuardTest do

@impl true
def handle_setup(ctx, state) do
{:ok, pid} = Task.start(fn -> Process.sleep(:infinity) end)
Process.register(pid, :membrane_resource_guard_test_element_resource)

ResourceGuard.register(ctx.resource_guard, fn ->
Process.exit(pid, :shutdown)
send(:membrane_resource_guard_test_process, :element_guard_triggered)
end)

{[notify_parent: :ready], state}
Expand All @@ -32,11 +29,8 @@ defmodule Membrane.ResourceGuardTest do

@impl true
def handle_setup(ctx, state) do
{:ok, pid} = Task.start(fn -> Process.sleep(:infinity) end)
Process.register(pid, :membrane_resource_guard_test_bin_resource)

ResourceGuard.register(ctx.resource_guard, fn ->
Process.exit(pid, :shutdown)
send(:membrane_resource_guard_test_process, :bin_guard_triggered)
end)

{[notify_parent: :ready], state}
Expand All @@ -50,37 +44,39 @@ defmodule Membrane.ResourceGuardTest do

@impl true
def handle_call(:setup_guard, ctx, state) do
{:ok, pid} = Task.start(fn -> Process.sleep(:infinity) end)
Process.register(pid, :membrane_resource_guard_test_pipeline_resource)

ResourceGuard.register(ctx.resource_guard, fn ->
Process.exit(pid, :shutdown)
send(:membrane_resource_guard_test_process, :pipeline_guard_triggered)
end)

{[reply: :ready], state}
end
end

Process.register(self(), :membrane_resource_guard_test_process)

pipeline = Testing.Pipeline.start_link_supervised!(module: Pipeline)

Testing.Pipeline.execute_actions(pipeline,
spec: [child(:element, Element), child(:bin, Bin)]
)

assert_pipeline_notified(pipeline, :element, :ready)
monitor_ref = Process.monitor(:membrane_resource_guard_test_element_resource)
Testing.Pipeline.execute_actions(pipeline, remove_children: :element)
assert_receive {:DOWN, ^monitor_ref, :process, _pid, :shutdown}
assert_receive :element_guard_triggered

assert_pipeline_notified(pipeline, :bin, :ready)
monitor_ref = Process.monitor(:membrane_resource_guard_test_bin_resource)
Testing.Pipeline.execute_actions(pipeline, remove_children: :bin)
assert_receive {:DOWN, ^monitor_ref, :process, _pid, :shutdown}
assert_receive :bin_guard_triggered

Testing.Pipeline.execute_actions(pipeline,
spec: [child(:element, Element), child(:bin, Bin)]
)

assert :ready = Membrane.Pipeline.call(pipeline, :setup_guard)
monitor_ref = Process.monitor(:membrane_resource_guard_test_pipeline_resource)
Membrane.Pipeline.terminate(pipeline)
assert_receive {:DOWN, ^monitor_ref, :process, _pid, :shutdown}
assert_receive :element_guard_triggered
assert_receive :bin_guard_triggered
assert_receive :pipeline_guard_triggered
end

test "Resources can be cleaned up manually and automatically when the owner process dies" do
Expand Down

0 comments on commit aef442d

Please sign in to comment.