diff --git a/lib/membrane/core/parent/child_life_controller/link_utils.ex b/lib/membrane/core/parent/child_life_controller/link_utils.ex index 8844548ab..4f6cb5fbb 100644 --- a/lib/membrane/core/parent/child_life_controller/link_utils.ex +++ b/lib/membrane/core/parent/child_life_controller/link_utils.ex @@ -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() diff --git a/test/membrane/integration/linking_test.exs b/test/membrane/integration/linking_test.exs index ef93fcff8..74cb9e740 100644 --- a/test/membrane/integration/linking_test.exs +++ b/test/membrane/integration/linking_test.exs @@ -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) @@ -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