-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renamespaced testing generic testing utilities.
- Loading branch information
Showing
16 changed files
with
191 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 0 additions & 102 deletions
102
lib/membrane/integration/testing_configurable_pipeline.ex
This file was deleted.
Oops, something went wrong.
8 changes: 5 additions & 3 deletions
8
...mbrane/integration/testing_data_source.ex → lib/membrane/testing/data_source.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/membrane/integration/testing_event.ex → lib/membrane/testing/event.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
defmodule Membrane.Support.TestingEvent do | ||
defmodule Membrane.Testing.Event do | ||
@derive Membrane.EventProtocol | ||
defstruct [] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
defmodule Membrane.Testing.Pipeline do | ||
@moduledoc """ | ||
Provides a testing utility pipeline which can be easily configured from test. | ||
""" | ||
|
||
use Membrane.Pipeline | ||
|
||
alias Membrane.Element | ||
alias Membrane.Pipeline.Spec | ||
|
||
defmodule Options do | ||
@moduledoc """ | ||
Structure representing `options` passed to testing pipeline. | ||
## Monitored Callbacks | ||
List of callback names that shall be sent to process in `test_process` field | ||
## Test Process | ||
`pid` of process that shall receive messages about Pipeline state. | ||
## Elements | ||
List of element specs. | ||
## Links | ||
If links is not present or set to nil it will be populated automatically based on elements order using default pad names. | ||
""" | ||
|
||
@enforce_keys [:elements, :test_process] | ||
defstruct @enforce_keys ++ [:monitored_callbacks, :links] | ||
|
||
@type pipeline_callback :: | ||
:handle_notification | ||
| :handle_playing_to_prepared | ||
| :handle_prepared_to_playing | ||
| :handle_prepared_to_stopped | ||
| :handle_stopped_to_prepared | ||
|
||
@type t :: %__MODULE__{ | ||
monitored_callbacks: pipeline_callback(), | ||
test_process: pid(), | ||
elements: Spec.children_spec_t(), | ||
links: Spec.links_spec_t() | ||
} | ||
end | ||
|
||
@doc """ | ||
Links subsequent elements using default pads (linking `:input` to `:output` of previous element). | ||
## Examples | ||
iex> Pipeline.populate_links([el1: MembraneElement1, el2: MembraneElement2], :my_pid) | ||
%{{:output, :el1} => {:input, :el2}} | ||
""" | ||
@spec populate_links(elements :: Spec.children_spec_t()) :: Spec.links_spec_t() | ||
def populate_links(elements) do | ||
[first_name | element_names] = Enum.map(elements, fn {name, _} -> name end) | ||
|
||
{links, _} = | ||
Enum.reduce(element_names, {%{}, first_name}, fn x, {links, previous_element} -> | ||
links = Map.put(links, {:output, previous_element}, {:input, x}) | ||
{links, x} | ||
end) | ||
|
||
links | ||
end | ||
|
||
@doc """ | ||
Sends message to a child by Element name. | ||
## Examples | ||
message_child(pipeline, :sink, {:message, "to handle"}) | ||
""" | ||
@spec message_child(pid(), Element.name_t(), any()) :: :ok | ||
def message_child(pipeline, child, message) do | ||
send(pipeline, {:for_element, child, message}) | ||
:ok | ||
end | ||
|
||
@impl true | ||
def handle_init(options) | ||
|
||
def handle_init(%Options{monitored_callbacks: nil} = options), | ||
do: handle_init(%Options{options | monitored_callbacks: []}) | ||
|
||
def handle_init(%Options{links: nil, elements: elements} = options) do | ||
new_links = populate_links(elements) | ||
%Options{options | links: new_links} | ||
end | ||
|
||
def handle_init(args) do | ||
%Options{elements: elements, links: links} = args | ||
|
||
spec = %Membrane.Pipeline.Spec{ | ||
children: elements, | ||
links: links | ||
} | ||
|
||
new_state = Map.take(args, [:monitored_callbacks, :test_process]) | ||
{{:ok, spec}, new_state} | ||
end | ||
|
||
@impl true | ||
def handle_stopped_to_prepared(state), | ||
do: notify_parent(:handle_stopped_to_prepared, state) | ||
|
||
@impl true | ||
def handle_playing_to_prepared(state), | ||
do: notify_parent(:handle_playing_to_prepared, state) | ||
|
||
@impl true | ||
def handle_prepared_to_playing(state), | ||
do: notify_parent(:handle_prepared_to_playing, state) | ||
|
||
@impl true | ||
def handle_prepared_to_stopped(state), | ||
do: notify_parent(:handle_prepared_to_stopped, state) | ||
|
||
@impl true | ||
def handle_notification(notification, from, state), | ||
do: notify_parent({:handle_notification, {notification, from}}, state) | ||
|
||
@impl true | ||
def handle_other({:for_element, element, message}, state) do | ||
{{:ok, forward: {element, message}}, state} | ||
end | ||
|
||
def handle_other(message, state), | ||
do: notify_parent({:handle_other, message}, state) | ||
|
||
defp get_callback_name(name) when is_atom(name), do: name | ||
defp get_callback_name({name, _}), do: name | ||
|
||
defp notify_parent(message, state) do | ||
%{test_process: parent, monitored_callbacks: monitored_callbacks} = state | ||
|
||
if get_callback_name(message) in monitored_callbacks do | ||
send(parent, {__MODULE__, message}) | ||
end | ||
|
||
{:ok, state} | ||
end | ||
end |
8 changes: 3 additions & 5 deletions
8
...sting_configurable_pipeline/assertions.ex → lib/membrane/testing/pipeline/assertions.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.