-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathdemands_test.exs
More file actions
310 lines (234 loc) · 9.02 KB
/
demands_test.exs
File metadata and controls
310 lines (234 loc) · 9.02 KB
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
defmodule Membrane.Integration.DemandsTest do
use Bunch
use ExUnit.Case, async: true
import ExUnit.Assertions
import Membrane.ChildrenSpec
import Membrane.Testing.Assertions
alias Membrane.Buffer
alias Membrane.Support.DemandsTest.Filter
alias Membrane.Testing
alias Membrane.Testing.{Pipeline, Sink, Source}
require Membrane.Pad, as: Pad
defp assert_buffers_received(range, pid) do
Enum.each(range, fn i ->
assert_sink_buffer(pid, :sink, buffer)
assert %Buffer{payload: <<^i::16>> <> <<255>>} = buffer
end)
end
defp test_pipeline(pid) do
assert_sink_playing(pid, :sink)
demand = 500
Pipeline.notify_child(pid, :sink, {:make_demand, demand})
0..(demand - 1)
|> assert_buffers_received(pid)
pattern = %Buffer{payload: <<demand::16>> <> <<255>>}
refute_sink_buffer(pid, :sink, ^pattern, 0)
Pipeline.notify_child(pid, :sink, {:make_demand, demand})
demand..(2 * demand - 1)
|> assert_buffers_received(pid)
end
test "Regular pipeline with proper demands" do
links =
child(:source, Source)
|> child(:filter, Filter)
|> child(:sink, %Sink{autodemand: false})
pid = Pipeline.start_link_supervised!(spec: links)
test_pipeline(pid)
end
test "Pipeline with filter underestimating demand" do
filter_demand_gen = fn _incoming_demand -> 2 end
links =
child(:source, Source)
|> child(:filter, %Filter{demand_generator: filter_demand_gen})
|> child(:sink, %Sink{autodemand: false})
pid = Pipeline.start_link_supervised!(spec: links)
test_pipeline(pid)
end
test "Pipeline with source not generating enough buffers" do
alias Membrane.Buffer
actions_gen = fn cnt, _size ->
cnt..(4 + cnt - 1)
|> Enum.map(fn cnt ->
buf = %Buffer{payload: <<cnt::16>>}
{:buffer, {:output, buf}}
end)
|> Enum.concat(redemand: :output)
~> {&1, cnt + 4}
end
spec =
child(:source, %Source{output: {0, actions_gen}})
|> child(:filter, Filter)
|> child(:sink, %Sink{autodemand: false})
pid = Pipeline.start_link_supervised!(spec: spec)
test_pipeline(pid)
end
test "handle_demand is not called for pad with end of stream" do
defmodule Source do
use Membrane.Source
defmodule StreamFormat do
defstruct []
end
def_output_pad :output, flow_control: :manual, accepted_format: _any
@impl true
def handle_init(_ctx, _opts), do: {[], %{eos_sent?: false}}
@impl true
def handle_demand(_pad, _size, _unit, _ctx, %{eos_sent?: true}) do
raise "handle_demand cannot be called after sending end of stream"
end
@impl true
def handle_demand(:output, n, :buffers, _ctx, %{eos_sent?: false} = state) do
buffers =
1..(n - 1)//1
|> Enum.map(fn _i -> %Membrane.Buffer{payload: <<>>} end)
Process.send_after(self(), :second_left, 1000)
{[stream_format: {:output, %StreamFormat{}}, buffer: {:output, buffers}], state}
end
@impl true
def handle_info(:second_left, _ctx, %{eos_sent?: false} = state) do
buffer = %Membrane.Buffer{payload: <<>>}
state = %{state | eos_sent?: true}
{[buffer: {:output, buffer}, end_of_stream: :output], state}
end
end
defmodule Sink do
use Membrane.Sink
def_input_pad :input, flow_control: :manual, demand_unit: :buffers, accepted_format: _any
@impl true
def handle_playing(_ctx, state), do: {[demand: {:input, 1}], state}
@impl true
def handle_buffer(:input, _buffer, _ctx, state), do: {[demand: {:input, 1}], state}
end
pipeline = Testing.Pipeline.start_link_supervised!(spec: child(Source) |> child(:sink, Sink))
assert_end_of_stream(pipeline, :sink)
Testing.Pipeline.terminate(pipeline)
end
defmodule RedemandingSource do
use Membrane.Source
@sleep_time 10
def_output_pad :output, accepted_format: _any, flow_control: :manual
defmodule StreamFormat do
defstruct []
end
@spec sleep_time() :: pos_integer()
def sleep_time(), do: @sleep_time
@impl true
def handle_playing(_ctx, _default_state) do
{[stream_format: {:output, %StreamFormat{}}], %{counter: 0}}
end
@impl true
def handle_demand(:output, _size, _unit, _ctx, state) do
Process.sleep(@sleep_time)
actions = [buffer: {:output, %Membrane.Buffer{payload: state.counter}}, redemand: :output]
{actions, Map.update!(state, :counter, &(&1 + 1))}
end
end
defmodule PausingSink do
use Membrane.Sink
def_input_pad :input, accepted_format: _any, flow_control: :auto
@impl true
def handle_init(_ctx, _opts), do: {[], %{counter: 0}}
@impl true
def handle_playing(_ctx, state) do
{[notify_parent: :playing], state}
end
@impl true
def handle_buffer(:input, _buffer, _ctx, state) do
{[], Map.update!(state, :counter, &(&1 + 1))}
end
@impl true
def handle_parent_notification(action, _ctx, state)
when action in [:pause_auto_demand, :resume_auto_demand] do
actions = [
{action, :input},
notify_parent: {:buff_no, state.counter}
]
{actions, %{state | counter: 0}}
end
end
defmodule Funnel do
use Membrane.Filter
def_input_pad :input, accepted_format: _any, flow_control: :auto, availability: :on_request
def_output_pad :output, accepted_format: _any, flow_control: :auto
def_options pads_upperbounds: [spec: map()]
@impl true
def handle_init(_ctx, %{pads_upperbounds: pads_upperbounds}) do
{[], %{pads_upperbounds: pads_upperbounds, pads_counters: %{}}}
end
@impl true
def handle_pad_added(pad, _ctx, state) do
{[], put_in(state, [:pads_counters, pad], 0)}
end
@impl true
def handle_buffer(pad, buffer, ctx, state) do
buffer = %{buffer | metadata: pad}
{pad_counter, state} = get_and_update_in(state, [:pads_counters, pad], &{&1, &1 + 1})
actions =
with {:ok, upperbound} when pad_counter > upperbound <-
Map.fetch(state.pads_upperbounds, pad),
%{auto_demand_paused?: false} <- ctx.pads[pad] do
[pause_auto_demand: pad, buffer: {:output, buffer}]
else
_other -> [buffer: {:output, buffer}]
end
{actions, state}
end
@impl true
def handle_end_of_stream(_pad, ctx, state) do
if ctx.pads.output.end_of_stream? do
{[], state}
else
{[end_of_stream: :output], state}
end
end
end
test "funnel pausing auto demands on one of its pads" do
spec = [
child({:source, :a}, RedemandingSource)
|> via_in(Pad.ref(:input, :a), auto_demand_size: 10)
|> child(:funnel, %Funnel{pads_upperbounds: %{Pad.ref(:input, :a) => 100}})
|> child(:sink, %Testing.Sink{autodemand: true}),
child({:source, :b}, RedemandingSource)
|> via_in(Pad.ref(:input, :b), auto_demand_size: 10)
|> get_child(:funnel)
]
pipeline = Testing.Pipeline.start_link_supervised!(spec: spec)
assert_sink_buffer(pipeline, :sink, %{payload: 100, metadata: Pad.ref(:input, :a)})
assert_sink_buffer(pipeline, :sink, %{payload: 100, metadata: Pad.ref(:input, :b)})
assert_sink_buffer(pipeline, :sink, %{payload: 500, metadata: Pad.ref(:input, :b)}, 8000)
refute_sink_buffer(pipeline, :sink, %{payload: 200, metadata: Pad.ref(:input, :a)}, 5000)
Testing.Pipeline.terminate(pipeline)
end
defmodule Sync do
use Bunch
use ExUnit.Case, async: false
alias Membrane.Integration.DemandsTest.{PausingSink, RedemandingSource}
test "actions :pause_auto_demand and :resume_auto_demand" do
pipeline =
Testing.Pipeline.start_link_supervised!(
spec:
child(RedemandingSource)
|> via_in(:input, auto_demand_size: 10)
|> child(:sink, PausingSink)
)
assert_sink_playing(pipeline, :sink)
for i <- 1..10 do
# during sleep below source should send around 100 buffers
Process.sleep(100 * RedemandingSource.sleep_time())
Testing.Pipeline.execute_actions(pipeline, notify_child: {:sink, :pause_auto_demand})
assert_pipeline_notified(pipeline, :sink, {:buff_no, buff_no})
# sink should receive around 100 buffers, but the boundary is set to 65, in case of eg.
# slowdown of the source when running all tests in the project asynchronously
if i != 1, do: assert(buff_no > 65)
# during sleep below source should send up to about auto_demand_size = 10 buffers
Process.sleep(100 * RedemandingSource.sleep_time())
Testing.Pipeline.execute_actions(pipeline, notify_child: {:sink, :resume_auto_demand})
assert_pipeline_notified(pipeline, :sink, {:buff_no, buff_no})
# sink should probably receive between 5 and 15 buffers, but the boundary is set to 25,
# to handle the case when eg. there is a delay in receiving the notification from the
# pipeline by the :sink
assert buff_no < 25
end
Testing.Pipeline.terminate(pipeline)
end
end
end