Skip to content

Commit

Permalink
Handle attempt to call :remove_link with not existing pad
Browse files Browse the repository at this point in the history
  • Loading branch information
FelonEkonom committed Dec 5, 2022
1 parent 7388864 commit 6114105
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
26 changes: 16 additions & 10 deletions lib/membrane/core/parent/child_life_controller/link_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,24 @@ defmodule Membrane.Core.Parent.ChildLifeController.LinkUtils do

@spec remove_link(Membrane.Child.name_t(), Pad.ref_t(), Parent.state_t()) :: Parent.state_t()
def remove_link(child_name, pad_ref, state) do
{_link_id, link} =
Enum.find(state.links, fn {_link_id, link} ->
[link.from, link.to]
|> Enum.any?(&(&1.child == child_name and &1.pad_ref == pad_ref))
end)
Enum.find(state.links, fn {_id, link} ->
[link.from, link.to]
|> Enum.any?(&(&1.child == child_name and &1.pad_ref == pad_ref))
end)
|> case do
{_id, %Link{} = link} ->
for endpoint <- [link.from, link.to] do
Message.send(endpoint.pid, :handle_unlink, endpoint.pad_ref)
end

for endpoint <- [link.from, link.to] do
Message.send(endpoint.pid, :handle_unlink, endpoint.pad_ref)
end
links = Map.delete(state.links, link.id)
Map.put(state, :links, links)

links = Map.delete(state.links, link.id)
Map.put(state, :links, links)
nil ->
raise LinkError, """
Attempted to unlink pad #{inspect(pad_ref)} of child #{inspect(child_name)}, but this child does not have this pad
"""
end
end

@spec unlink_element(Membrane.Child.name_t(), Parent.state_t()) :: Parent.state_t()
Expand Down
12 changes: 6 additions & 6 deletions test/membrane/integration/linking_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@ defmodule Membrane.Integration.LinkingTest do
test "Parent successfully unlinks children with dynamic pads using :remove_link action" do
structure =
[
child(:source, __MODULE__.Element)
|> child(:sink, __MODULE__.Element)
child(:source, __MODULE__.Element),
child(:sink, __MODULE__.Element)
] ++
Enum.map(1..10, fn i ->
get_child(:source)
Expand Down Expand Up @@ -418,13 +418,13 @@ defmodule Membrane.Integration.LinkingTest do
end

defp assert_pad_removed(pipeline, id) do
assert_pipeline_notified(pipeline, :source, {:handle_pad_removed, Pad.ref(:output, ^id)}, 1)
assert_pipeline_notified(pipeline, :sink, {:handle_pad_removed, Pad.ref(:input, ^id)}, 1)
assert_pipeline_notified(pipeline, :source, {:handle_pad_removed, Pad.ref(:output, ^id)})
assert_pipeline_notified(pipeline, :sink, {:handle_pad_removed, Pad.ref(:input, ^id)})
end

defp refute_pad_removed(pipeline, id) do
refute_pipeline_notified(pipeline, :source, {:handle_pad_removed, Pad.ref(:output, ^id)}, 1)
refute_pipeline_notified(pipeline, :sink, {:handle_pad_removed, Pad.ref(:input, ^id)}, 1)
refute_pipeline_notified(pipeline, :source, {:handle_pad_removed, Pad.ref(:output, ^id)}, 10)
refute_pipeline_notified(pipeline, :sink, {:handle_pad_removed, Pad.ref(:input, ^id)}, 10)
end

defp get_child_pid(ref, parent_pid) do
Expand Down

0 comments on commit 6114105

Please sign in to comment.