Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve resource guard test #777

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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