-
Notifications
You must be signed in to change notification settings - Fork 33
/
pipeline.ex
551 lines (439 loc) · 16.5 KB
/
pipeline.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
defmodule Membrane.Testing.Pipeline do
@moduledoc """
This Pipeline was created to reduce testing boilerplate and ease communication
with its children. It also provides a utility for informing testing process about
playback changes and received notifications.
When you want a build Pipeline to test your children you need three things:
- Pipeline Module
- List of children
- Links between those children
To start a testing pipeline you need to build
a keyword list representing the options used to determine the pipeline's behaviour and then
pass that options list to the `Membrane.Testing.Pipeline.start_link_supervised!/1`.
The testing pipeline can be started in one of two modes - either with its `:default` behaviour, or by
injecting a custom module behaviour. The usage of a `:default` pipeline implementation is presented below:
```
links = [
child(:el1, MembraneElement1) |>
child(:el2, MembraneElement2)
...
]
options = [
module: :default # :default is the default value for this parameter, so you do not need to pass it here
spec: links
]
pipeline = Membrane.Testing.Pipeline.start_link_supervised!(options)
```
You can also pass your custom pipeline's module as a `:module` option of
the options list. Every callback of the module
will be executed before the callbacks of Testing.Pipeline.
Passed module has to return a proper spec. There should be no children
nor links specified in options passed to test pipeline as that would
result in a failure.
```
options = [
module: Your.Module
]
pipeline = Membrane.Testing.Pipeline.start_link_supervised!(options)
```
See `t:Membrane.Testing.Pipeline.options/0` for available options.
## Assertions
This pipeline is designed to work with `Membrane.Testing.Assertions`. Check
them out or see example below for more details.
## Messaging children
You can send messages to children using their names specified in the children
list. Please check `message_child/3` for more details.
## Example usage
Firstly, we can start the pipeline providing its options as a keyword list:
import Membrane.ChildrenSpec
children = [
child(source, %Membrane.Testing.Source{} ) |>
child(:tested_element, TestedElement) |>
child(sink, %Membrane.Testing.Sink{})
]
{:ok, pipeline} = Membrane.Testing.Pipeline.start_link(spec: links)
We can now wait till the end of the stream reaches the sink element (don't forget
to import `Membrane.Testing.Assertions`):
assert_end_of_stream(pipeline, :sink)
We can also assert that the `Membrane.Testing.Sink` processed a specific
buffer:
assert_sink_buffer(pipeline, :sink, %Membrane.Buffer{payload: 1})
"""
use Membrane.Pipeline
alias Membrane.Child
alias Membrane.ChildrenSpec
alias Membrane.Core.Message
alias Membrane.{Element, Pipeline}
alias Membrane.Testing.Notification
require Membrane.Core.Message
defmodule State do
@moduledoc false
@enforce_keys [:test_process, :module]
defstruct @enforce_keys ++ [:custom_pipeline_state, :raise_on_child_pad_removed?]
@typedoc """
Structure for holding state
## Test Process
`pid` of process that shall receive messages from the pipeline
## Module
Pipeline Module with custom callbacks.
## Custom Pipeline State
State of the pipeline defined by Module.
"""
@type t :: %__MODULE__{
test_process: pid() | nil,
module: module() | nil,
custom_pipeline_state: Pipeline.state(),
raise_on_child_pad_removed?: boolean() | nil
}
end
@type options ::
[
module: :default,
spec: [ChildrenSpec.builder()],
test_process: pid(),
name: Pipeline.name(),
raise_on_child_pad_removed?: boolean()
]
| [
module: module(),
custom_args: Pipeline.pipeline_options(),
test_process: pid(),
name: Pipeline.name()
]
@spec child_spec(options) :: Supervisor.child_spec()
def child_spec(options) do
id = Keyword.get(options, :name, make_ref())
super(options) |> Map.merge(%{restart: :transient, id: id})
end
@spec start_link(options) :: Pipeline.on_start()
def start_link(options) do
do_start(:start_link, options)
end
@spec start(options) :: Pipeline.on_start()
def start(options) do
do_start(:start, options)
end
defp do_start(type, options) do
:ok = validate_options!(options)
{process_options, options} = Keyword.split(options, [:name])
options = Keyword.put_new(options, :test_process, self())
apply(Pipeline, type, [__MODULE__, options, process_options])
end
@doc """
Starts the pipeline under the ExUnit test supervisor and links it to the current process.
Can be used only in tests.
"""
@spec start_link_supervised(options) :: Pipeline.on_start()
def start_link_supervised(pipeline_options \\ []) do
pipeline_options = Keyword.put_new(pipeline_options, :test_process, self())
# TODO use start_link_supervised when added
with {:ok, supervisor, pipeline} <- ex_unit_start_supervised({__MODULE__, pipeline_options}) do
Process.link(pipeline)
{:ok, supervisor, pipeline}
else
{:error, {error, _child_info}} -> {:error, error}
end
end
@spec start_link_supervised!(options) :: pipeline_pid :: pid
def start_link_supervised!(pipeline_options \\ []) do
{:ok, _supervisor, pipeline} = start_link_supervised(pipeline_options)
pipeline
end
@doc """
Starts the pipeline under the ExUnit test supervisor.
Can be used only in tests.
"""
@spec start_supervised(options) :: Pipeline.on_start()
def start_supervised(pipeline_options \\ []) do
pipeline_options = Keyword.put_new(pipeline_options, :test_process, self())
ex_unit_start_supervised({__MODULE__, pipeline_options})
end
@spec start_supervised!(options) :: pipeline_pid :: pid
def start_supervised!(pipeline_options \\ []) do
{:ok, _supervisor, pipeline} = start_supervised(pipeline_options)
pipeline
end
defp ex_unit_start_supervised(child_spec) do
# It's not a 'normal' call to keep dialyzer quiet
apply(ExUnit.Callbacks, :start_supervised, [child_spec])
end
@spec terminate(pid, Keyword.t()) :: :ok | {:ok, pid()} | {:error, :timeout}
defdelegate terminate(pipeline, opts \\ []), to: Pipeline
@doc """
Sends message to a child by Element name.
## Example
Knowing that `pipeline` has child named `sink`, message can be sent as follows:
message_child(pipeline, :sink, {:message, "to handle"})
"""
@spec message_child(pid(), Element.name(), any()) :: :ok
def message_child(pipeline, child, message) do
send(pipeline, {:for_element, child, message})
:ok
end
@doc """
Executes specified actions in the pipeline.
The actions are returned from the `handle_info` callback.
"""
@spec execute_actions(pid(), Keyword.t()) :: :ok
def execute_actions(pipeline, actions) do
send(pipeline, {__MODULE__, :__execute_actions__, actions})
:ok
end
@doc """
Returns the pid of the children process.
Accepts pipeline pid as a first argument and a child reference or a list
of child references representing a path as a second argument.
If second argument is a child reference, function gets pid of this child
from pipeline.
If second argument is a path of child references, function gets pid of
last a component pointed by this path.
Returns
* `{:ok, child_pid}`, if a child was succesfully found
* `{:error, reason}`, if, for example, pipeline is not alive or children path is invalid
"""
@spec get_child_pid(pid(), child_name_path :: Child.name() | [Child.name()]) ::
{:ok, pid()} | {:error, reason :: term()}
def get_child_pid(pipeline, [_head | _tail] = child_name_path) do
do_get_child_pid(pipeline, child_name_path)
end
def get_child_pid(pipeline, child_name) when not is_list(child_name) do
do_get_child_pid(pipeline, [child_name])
end
@doc """
Returns the pid of the children process.
Works as get_child_pid/2, but raises an error instead of returning
`{:error, reason}` tuple.
"""
@spec get_child_pid!(pid(), child_name_path :: Child.name() | [Child.name()]) :: pid()
def get_child_pid!(parent_pid, child_name_path) do
{:ok, child_pid} = get_child_pid(parent_pid, child_name_path)
child_pid
end
defp do_get_child_pid(component_pid, child_name_path, is_pipeline? \\ true)
defp do_get_child_pid(component_pid, [], _is_pipeline?) do
{:ok, component_pid}
end
defp do_get_child_pid(component_pid, [child_name | child_name_path_tail], is_pipeline?) do
case Message.call(component_pid, :get_child_pid, child_name) do
{:ok, child_pid} ->
do_get_child_pid(child_pid, child_name_path_tail, false)
{:error, {:call_failure, {:noproc, _call_info}}} ->
if is_pipeline?,
do: {:error, :pipeline_not_alive},
else: {:error, :component_not_alive}
{:error, _reason} = error ->
error
end
end
@impl true
def handle_init(ctx, options) do
case Keyword.get(options, :module, :default) do
:default ->
spec = Bunch.listify(Keyword.get(options, :spec, []))
test_process = Keyword.fetch!(options, :test_process)
raise? = Keyword.get(options, :raise_on_child_pad_removed?, true)
new_state = %State{
test_process: test_process,
raise_on_child_pad_removed?: raise?,
module: nil
}
{[spec: spec], new_state}
module when is_atom(module) ->
case Code.ensure_compiled(module) do
{:module, _} -> :ok
{:error, _} -> raise "Unknown module: #{inspect(module)}"
end
new_state = %State{
module: module,
custom_pipeline_state: Keyword.get(options, :custom_args),
test_process: Keyword.fetch!(options, :test_process)
}
injected_module_result = eval_injected_module_callback(:handle_init, [ctx], new_state)
testing_pipeline_result = {[], new_state}
combine_results(injected_module_result, testing_pipeline_result)
not_a_module ->
raise "Not a module: #{inspect(not_a_module)}"
end
end
@impl true
def handle_setup(ctx, %State{} = state) do
{custom_actions, custom_state} =
eval_injected_module_callback(
:handle_setup,
[ctx],
state
)
:ok = notify_test_process(state.test_process, :setup)
{custom_actions, Map.put(state, :custom_pipeline_state, custom_state)}
end
@impl true
def handle_playing(ctx, %State{} = state) do
{custom_actions, custom_state} =
eval_injected_module_callback(
:handle_playing,
[ctx],
state
)
:ok = notify_test_process(state.test_process, :play)
{custom_actions, Map.put(state, :custom_pipeline_state, custom_state)}
end
@impl true
def handle_child_notification(
%Notification{payload: notification},
from,
_ctx,
%State{} = state
) do
:ok =
notify_test_process(state.test_process, {:handle_child_notification, {notification, from}})
{[], state}
end
@impl true
def handle_child_notification(notification, from, ctx, %State{} = state) do
{custom_actions, custom_state} =
eval_injected_module_callback(
:handle_child_notification,
[notification, from, ctx],
state
)
:ok =
notify_test_process(state.test_process, {:handle_child_notification, {notification, from}})
{custom_actions, Map.put(state, :custom_pipeline_state, custom_state)}
end
@impl true
def handle_spec_started(children, ctx, %State{} = state) do
{custom_actions, custom_state} =
eval_injected_module_callback(
:handle_spec_started,
[children, ctx],
state
)
{custom_actions, Map.put(state, :custom_pipeline_state, custom_state)}
end
@impl true
def handle_info({__MODULE__, :__execute_actions__, actions}, _ctx, %State{} = state) do
{actions, state}
end
@impl true
def handle_info({:for_element, element, message}, ctx, %State{} = state) do
injected_module_result =
eval_injected_module_callback(
:handle_info,
[{:for_element, element, message}, ctx],
state
)
testing_pipeline_result = {[notify_child: {element, message}], state}
combine_results(injected_module_result, testing_pipeline_result)
end
@impl true
def handle_info(message, ctx, %State{} = state) do
{custom_actions, custom_state} =
eval_injected_module_callback(
:handle_info,
[message, ctx],
state
)
:ok = notify_test_process(state.test_process, {:handle_info, message})
{custom_actions, Map.put(state, :custom_pipeline_state, custom_state)}
end
@impl true
def handle_call(message, ctx, state) do
{custom_actions, custom_state} =
eval_injected_module_callback(
:handle_call,
[message, ctx],
state
)
:ok = notify_test_process(state.test_process, {:handle_call, message})
{custom_actions, Map.put(state, :custom_pipeline_state, custom_state)}
end
@impl true
def handle_element_start_of_stream(element, pad, ctx, state) do
{custom_actions, custom_state} =
eval_injected_module_callback(
:handle_element_start_of_stream,
[element, pad, ctx],
state
)
:ok =
notify_test_process(state.test_process, {:handle_element_start_of_stream, {element, pad}})
{custom_actions, Map.put(state, :custom_pipeline_state, custom_state)}
end
@impl true
def handle_element_end_of_stream(element, pad, ctx, state) do
{custom_actions, custom_state} =
eval_injected_module_callback(
:handle_element_end_of_stream,
[element, pad, ctx],
state
)
:ok = notify_test_process(state.test_process, {:handle_element_end_of_stream, {element, pad}})
{custom_actions, Map.put(state, :custom_pipeline_state, custom_state)}
end
@impl true
def handle_tick(timer, ctx, state) do
{custom_actions, custom_state} =
eval_injected_module_callback(
:handle_tick,
[timer, ctx],
state
)
{custom_actions, Map.put(state, :custom_pipeline_state, custom_state)}
end
@impl true
def handle_child_pad_removed(child, pad, ctx, state) do
if state.raise_on_child_pad_removed? do
raise """
Child #{inspect(child)} removed it's pad #{inspect(pad)}. If you want to
handle such a scenario, pass `raise_on_child_pad_removed?: false` option to
`Membrane.Testing.Pipeline.start_*/2` or pass there a pipeline module
implementing this callback via `:module` option.
"""
end
{custom_actions, custom_state} =
eval_injected_module_callback(
:handle_child_pad_removed,
[child, pad, ctx],
state
)
:ok = notify_test_process(state.test_process, {:handle_child_pad_removed, {child, pad}})
{custom_actions, Map.put(state, :custom_pipeline_state, custom_state)}
end
@impl true
def handle_crash_group_down(group_name, ctx, state) do
{custom_actions, custom_state} =
eval_injected_module_callback(
:handle_crash_group_down,
[group_name, ctx],
state
)
:ok = notify_test_process(state.test_process, {:handle_crash_group_down, group_name})
{custom_actions, Map.put(state, :custom_pipeline_state, custom_state)}
end
defp eval_injected_module_callback(callback, args, state)
defp eval_injected_module_callback(_callback, _args, %State{module: nil}),
do: {[], nil}
defp eval_injected_module_callback(callback, args, state) do
apply(state.module, callback, args ++ [state.custom_pipeline_state])
end
defp notify_test_process(test_process, message) do
send(test_process, {__MODULE__, self(), message})
:ok
end
defp combine_results({custom_actions, custom_state}, {actions, state}) do
{Enum.concat(custom_actions, actions), Map.put(state, :custom_pipeline_state, custom_state)}
end
defp validate_options!(options) do
allowed_keys_1 = [:module, :mode, :spec, :test_process, :name, :raise_on_child_pad_removed?]
allowed_keys_2 = [:module, :custom_args, :test_process, :name]
with {:error, _keys} <- Keyword.validate(options, allowed_keys_1),
{:error, _keys} <- Keyword.validate(options, allowed_keys_2) do
raise """
Options passed to #{inspect(__MODULE__)} start function has to fulfill type
#{inspect(__MODULE__)}.options, while they are #{inspect(options)}
"""
else
{:ok, _keyword} -> :ok
end
end
end