-
Notifications
You must be signed in to change notification settings - Fork 39
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
Implement :remove_link action #487
Changes from 5 commits
7388864
6114105
79b1483
ed674ff
2d1dfc9
7cb2274
bb7f8d6
19868b2
52eb2cb
d0b28ec
85364e4
6771dfb
02bf407
ad972a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -32,6 +32,28 @@ defmodule Membrane.Core.Parent.ChildLifeController.LinkUtils do | |||||
end) | ||||||
end | ||||||
|
||||||
@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 | ||||||
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 | ||||||
|
||||||
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We should probably first check if such a child exists to give a better error when it doesn't |
||||||
""" | ||||||
end | ||||||
end | ||||||
|
||||||
@spec unlink_element(Membrane.Child.name_t(), Parent.state_t()) :: Parent.state_t() | ||||||
def unlink_element(child_name, state) do | ||||||
Map.update!( | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,7 +8,7 @@ defmodule Membrane.Pipeline.Action do | |||||
callback unless explicitly stated otherwise. | ||||||
""" | ||||||
|
||||||
alias Membrane.{Child, ChildrenSpec} | ||||||
alias Membrane.{Child, ChildrenSpec, Pad} | ||||||
|
||||||
@typedoc """ | ||||||
Action that sends a message to a child identified by name. | ||||||
|
@@ -29,6 +29,11 @@ defmodule Membrane.Pipeline.Action do | |||||
@type remove_child_t :: | ||||||
{:remove_child, Child.name_t() | [Child.name_t()]} | ||||||
|
||||||
@typedoc """ | ||||||
Actions that removes link, that relates to specified child and pad. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
""" | ||||||
@type remove_link_t :: {:remove_link, {Child.name_t(), Pad.ref_t()}} | ||||||
|
||||||
@typedoc """ | ||||||
Starts a timer that will invoke `c:Membrane.Pipeline.handle_tick/3` callback | ||||||
every `interval` according to the given `clock`. | ||||||
|
@@ -121,6 +126,7 @@ defmodule Membrane.Pipeline.Action do | |||||
notify_child_t | ||||||
| spec_t | ||||||
| remove_child_t | ||||||
| remove_link_t | ||||||
| start_timer_t | ||||||
| timer_interval_t | ||||||
| stop_timer_t | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,28 @@ defmodule Membrane.Integration.LinkingTest do | |
|
||
require Membrane.Pad, as: Pad | ||
|
||
defmodule Element do | ||
use Membrane.Endpoint | ||
|
||
def_input_pad :input, | ||
availability: :on_request, | ||
accepted_format: _any | ||
|
||
def_output_pad :output, | ||
availability: :on_request, | ||
accepted_format: _any | ||
|
||
@impl true | ||
def handle_demand(_pad, _size, _unit, _ctx, state) do | ||
{[], state} | ||
end | ||
|
||
@impl true | ||
def handle_pad_removed(pad_ref, _ctx, state) do | ||
{[notify_parent: {:handle_pad_removed, pad_ref}], state} | ||
end | ||
end | ||
|
||
defmodule Bin do | ||
use Membrane.Bin | ||
|
||
|
@@ -413,6 +435,49 @@ defmodule Membrane.Integration.LinkingTest do | |
assert_start_of_stream(pipeline, :sink) | ||
end | ||
|
||
test "Parent successfully unlinks children with dynamic pads using :remove_link action" do | ||
structure = | ||
[ | ||
child(:source, __MODULE__.Element), | ||
child(:sink, __MODULE__.Element) | ||
] ++ | ||
Enum.map(1..10, fn i -> | ||
get_child(:source) | ||
|> via_out(Pad.ref(:output, i)) | ||
|> via_in(Pad.ref(:input, i)) | ||
|> get_child(:sink) | ||
end) | ||
|
||
pipeline = Testing.Pipeline.start_link_supervised!(structure: structure) | ||
|
||
for pad_id <- 1..10 do | ||
actions = | ||
if rem(pad_id, 2) == 0, | ||
do: [remove_link: {:source, Pad.ref(:output, pad_id)}], | ||
else: [remove_link: {:sink, Pad.ref(:input, pad_id)}] | ||
|
||
Testing.Pipeline.execute_actions(pipeline, actions) | ||
|
||
assert_pad_removed(pipeline, pad_id) | ||
|
||
if pad_id < 10 do | ||
for i <- (pad_id + 1)..10 do | ||
refute_pad_removed(pipeline, i) | ||
end | ||
end | ||
end | ||
end | ||
|
||
defp assert_pad_removed(pipeline, id) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this function should be called |
||
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)}, 10) | ||
varsill marked this conversation as resolved.
Show resolved
Hide resolved
|
||
refute_pipeline_notified(pipeline, :sink, {:handle_pad_removed, Pad.ref(:input, ^id)}, 10) | ||
end | ||
|
||
defp get_child_pid(ref, parent_pid) do | ||
state = :sys.get_state(parent_pid) | ||
state.children[ref].pid | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.