-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathtelemetry_test.exs
More file actions
305 lines (236 loc) · 8.63 KB
/
Copy pathtelemetry_test.exs
File metadata and controls
305 lines (236 loc) · 8.63 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
defmodule Membrane.TelemetryTest do
@moduledoc """
Test suite for Membrane telemetry public API. It tests if telemetry datapoints are reported
properly for all event types and span types upon attaching to the :telemetry system.
Remove below with 2.0.0 release:
In case of a need to test legacy telemetry paste the following snippet into the config file:
# config to test legacy version of telemetry
# config :membrane_core, :telemetry_flags, [
# :report_init,
# :report_terminate,
# :report_buffer,
# :report_queue,
# :report_link,
# metrics: [
# :buffer,
# :bitrate,
# :queue_len,
# :stream_format,
# :event,
# :store,
# :take_and_demand
# ]
# ]
```
"""
use ExUnit.Case, async: false
import ExUnit.CaptureLog
import Membrane.ChildrenSpec
import Membrane.Testing.Assertions
alias Membrane.Core.Telemetry
alias Membrane.Testing
require Logger
@moduletag if Telemetry.legacy?(), do: :skip, else: nil
defmodule TestFilter do
use Membrane.Filter
def_input_pad :input, accepted_format: _any
def_output_pad :output, accepted_format: _any
@impl true
def handle_buffer(_pad, buffer, _context, state), do: {[buffer: {:output, buffer}], state}
@impl true
def handle_parent_notification(:crash, _context, state) do
raise "Intended Crash Test"
{[], state}
end
end
defmodule TelemetryListener do
@spec handle_measurements(atom(), any(), map(), map()) :: :ok
def handle_measurements(name, value, metadata, %{dest: pid, ref: ref}) do
pid |> send({ref, :telemetry_ack, {name, value, metadata}})
end
end
setup do
if Telemetry.legacy?() do
[skip: true]
else
child_spec =
child(:source, %Testing.Source{output: ["a", "b", "c"]})
|> child(:filter, TestFilter)
|> child(:sink, Testing.Sink)
[child_spec: child_spec]
end
end
@paths ~w[:filter :sink :source]
@spans [:handle_init, :handle_setup, :handle_playing, :handle_terminate_request]
@steps [:start, :stop]
describe "Telemetry reports elements'" do
setup %{child_spec: child_spec} do
ref = make_ref()
spans =
for handler <- @spans,
step <- @steps do
[:membrane, :element, handler, step]
end
setup_pipeline_for_callbacks(spans, child_spec, ref)
[ref: ref]
end
# Test each lifecycle step for each element type
for element_type <- @paths,
span <- @spans do
test "#{element_type}/#{span}", %{ref: ref} do
element_type = unquote(element_type)
span = unquote(span)
assert_receive {^ref, :telemetry_ack,
{[:membrane, :element, ^span, :start], results,
%{component_path: [_pipeline, ^element_type]}}},
1000
assert results.monotonic_time
assert_receive {^ref, :telemetry_ack,
{[:membrane, :element, ^span, :stop], results,
%{component_path: [_pipeline, ^element_type]} = metadata}},
1000
assert results.duration >= 0
assert metadata.component_type == :element
end
end
end
describe "Telemetry reports pipelines'" do
setup %{child_spec: child_spec} do
ref = make_ref()
spans =
for span <- @spans,
step <- @steps do
[:membrane, :pipeline, span, step]
end
setup_pipeline_for_callbacks(spans, child_spec, ref)
[ref: ref]
end
test "lifecycle", %{ref: ref} do
assert_receive {^ref, :telemetry_ack,
{[:membrane, :pipeline, :handle_init, :start], results, %{}}}
assert results.monotonic_time
assert_receive {^ref, :telemetry_ack,
{[:membrane, :pipeline, :handle_init, :stop], results, %{}}}
assert results.duration >= 0
assert_receive {^ref, :telemetry_ack,
{[:membrane, :pipeline, :handle_setup, :start], results, %{}}}
assert results.monotonic_time
assert_receive {^ref, :telemetry_ack,
{[:membrane, :pipeline, :handle_setup, :stop], results, %{}}}
assert results.duration >= 0
end
end
describe "Telemetry properly reports end of span when exception was encountered" do
setup %{child_spec: child_spec} do
ref = make_ref()
spans =
[
[:membrane, :element, :handle_parent_notification, :start],
[:membrane, :element, :handle_parent_notification, :stop],
[:membrane, :element, :handle_parent_notification, :exception]
]
:telemetry.attach_many(ref, spans, &TelemetryListener.handle_measurements/4, %{
dest: self(),
ref: ref
})
pid = Testing.Pipeline.start_supervised!(spec: child_spec)
capture_log(fn ->
:ok = Testing.Pipeline.notify_child(pid, :filter, :crash)
end)
[ref: ref]
end
test "in element", %{ref: ref} do
assert_receive {^ref, :telemetry_ack,
{[:membrane, :element, :handle_parent_notification, :start], _results,
%{component_path: [_, ":filter"]}}},
1000
assert_receive {^ref, :telemetry_ack,
{[:membrane, :element, :handle_parent_notification, :exception], results,
%{component_path: [_, ":filter"]}}},
1000
assert results.duration >= 0
refute_received {^ref, :telemetry_ack,
{[:membrane, :element, :handle_parent_notification, :stop], _results, _}}
end
end
describe "Telemetry properly reports following datapoints: " do
test "Link", %{child_spec: child_spec} do
ref = setup_pipeline_for(:link, child_spec)
assert_receive {^ref, :telemetry_ack, {[:membrane, :datapoint, :link], link1, metadata}}
assert_event_metadata(metadata)
assert_receive {^ref, :telemetry_ack, {[:membrane, :datapoint, :link], link2, metadata}}
assert_event_metadata(metadata)
assert [
%{
from: ":filter",
pad_from: ":output",
pad_to: ":input",
parent_component_path: path,
to: ":sink"
},
%{
from: ":source",
pad_from: ":output",
pad_to: ":input",
parent_component_path: path,
to: ":filter"
}
] = Enum.sort([link1.value, link2.value])
end
test "Stream Format", %{child_spec: child_spec} do
ref = setup_pipeline_for(:stream_format, child_spec)
assert_receive {^ref, :telemetry_ack,
{[:membrane, :datapoint, :stream_format], measurement, metadata}}
assert measurement.value.format.type == :bytestream
assert_event_metadata(metadata)
end
test "Buffer", %{child_spec: child_spec} do
ref = setup_pipeline_for(:buffer, child_spec)
for _i <- 1..3 do
assert_receive {^ref, :telemetry_ack,
{[:membrane, :datapoint, :buffer], measurement, metadata}}
assert measurement.value != 0
assert_event_metadata(metadata)
end
end
test "Event", %{child_spec: child_spec} do
ref = setup_pipeline_for(:event, child_spec)
assert_receive {^ref, :telemetry_ack,
{[:membrane, :datapoint, :event], measurement, metadata}}
assert measurement.value.pad_ref == ":input"
assert_event_metadata(metadata)
end
test "Queue Length", %{child_spec: child_spec} do
ref = setup_pipeline_for(:queue_len, child_spec)
assert_receive {^ref, :telemetry_ack,
{[:membrane, :datapoint, :queue_len], measurement, metadata}}
assert measurement.value
assert_event_metadata(metadata)
end
end
defp assert_event_metadata(metadata) do
assert is_atom(metadata.datapoint)
assert is_list(metadata.component_path)
assert metadata.component_metadata
end
defp setup_pipeline_for(event, child_spec) do
ref = make_ref()
:telemetry.attach(
ref,
[:membrane, :datapoint, event],
&TelemetryListener.handle_measurements/4,
%{dest: self(), ref: ref}
)
Testing.Pipeline.start_link_supervised!(spec: child_spec)
ref
end
defp setup_pipeline_for_callbacks(spans, child_spec, ref) do
:telemetry.attach_many(ref, spans, &TelemetryListener.handle_measurements/4, %{
dest: self(),
ref: ref
})
pid = Testing.Pipeline.start_link_supervised!(spec: child_spec)
assert_end_of_stream(pid, :sink)
:ok = Testing.Pipeline.terminate(pid)
end
end