8 class _MyIter(bt2
._UserMessageIterator
):
9 def __init__(self
, self_output_port
):
13 def _build_meta(self
):
14 self
._tc
= self
._component
._create
_trace
_class
()
16 self
._sc
= self
._tc
.create_stream_class()
17 self
._ec
= self
._sc
.create_event_class(name
='salut')
18 self
._my
_int
_ft
= self
._tc
.create_signed_integer_field_class(32)
19 payload_ft
= self
._tc
.create_structure_field_class()
20 payload_ft
+= collections
.OrderedDict([
21 ('my_int', self
._my
_int
_ft
),
23 self
._ec
.payload_field_type
= payload_ft
24 self
._stream
= self
._t
.create_stream(self
._sc
)
25 self
._packet
= self
._stream
.create_packet()
27 def _create_event(self
, value
):
29 ev
.payload_field
['my_int'] = value
30 ev
.packet
= self
._packet
34 class GraphTestCase(unittest
.TestCase
):
36 self
._graph
= bt2
.Graph()
41 def test_create_empty(self
):
44 def test_add_component_user_cls(self
):
45 class MySink(bt2
._UserSinkComponent
):
49 comp
= self
._graph
.add_component(MySink
, 'salut')
50 self
.assertEqual(comp
.name
, 'salut')
52 def test_add_component_gen_cls(self
):
53 class MySink(bt2
._UserSinkComponent
):
57 comp
= self
._graph
.add_component(MySink
, 'salut')
59 comp2
= self
._graph
.add_component(comp
.cls
, 'salut2')
60 self
.assertEqual(comp2
.name
, 'salut2')
62 def test_add_component_params(self
):
65 class MySink(bt2
._UserSinkComponent
):
66 def __init__(self
, params
):
73 params
= {'hello': 23, 'path': '/path/to/stuff'}
74 comp
= self
._graph
.add_component(MySink
, 'salut', params
)
75 self
.assertEqual(params
, comp_params
)
78 def test_add_component_invalid_cls_type(self
):
79 with self
.assertRaises(TypeError):
80 self
._graph
.add_component(int, 'salut')
82 def test_connect_ports(self
):
83 class MyIter(bt2
._UserMessageIterator
):
87 class MySource(bt2
._UserSourceComponent
,
88 message_iterator_class
=MyIter
):
89 def __init__(self
, params
):
90 self
._add
_output
_port
('out')
92 class MySink(bt2
._UserSinkComponent
):
93 def __init__(self
, params
):
94 self
._add
_input
_port
('in')
99 src
= self
._graph
.add_component(MySource
, 'src')
100 sink
= self
._graph
.add_component(MySink
, 'sink')
102 conn
= self
._graph
.connect_ports(src
.output_ports
['out'],
103 sink
.input_ports
['in'])
104 self
.assertTrue(src
.output_ports
['out'].is_connected
)
105 self
.assertTrue(sink
.input_ports
['in'].is_connected
)
106 self
.assertEqual(src
.output_ports
['out'].connection
._ptr
, conn
._ptr
)
107 self
.assertEqual(sink
.input_ports
['in'].connection
._ptr
, conn
._ptr
)
109 def test_connect_ports_invalid_direction(self
):
110 class MyIter(bt2
._UserMessageIterator
):
114 class MySource(bt2
._UserSourceComponent
,
115 message_iterator_class
=MyIter
):
116 def __init__(self
, params
):
117 self
._add
_output
_port
('out')
119 class MySink(bt2
._UserSinkComponent
):
120 def __init__(self
, params
):
121 self
._add
_input
_port
('in')
126 src
= self
._graph
.add_component(MySource
, 'src')
127 sink
= self
._graph
.add_component(MySink
, 'sink')
129 with self
.assertRaises(TypeError):
130 conn
= self
._graph
.connect_ports(sink
.input_ports
['in'],
131 src
.output_ports
['out'])
133 def test_connect_ports_refused(self
):
134 class MyIter(bt2
._UserMessageIterator
):
138 class MySource(bt2
._UserSourceComponent
,
139 message_iterator_class
=MyIter
):
140 def __init__(self
, params
):
141 self
._add
_output
_port
('out')
143 class MySink(bt2
._UserSinkComponent
):
144 def __init__(self
, params
):
145 self
._add
_input
_port
('in')
150 def _accept_port_connection(self
, port
, other_port
):
153 src
= self
._graph
.add_component(MySource
, 'src')
154 sink
= self
._graph
.add_component(MySink
, 'sink')
156 with self
.assertRaises(bt2
.PortConnectionRefused
):
157 conn
= self
._graph
.connect_ports(src
.output_ports
['out'],
158 sink
.input_ports
['in'])
160 def test_cancel(self
):
161 self
.assertFalse(self
._graph
.is_canceled
)
163 self
.assertTrue(self
._graph
.is_canceled
)
165 # Test that Graph.run() raises bt2.GraphCanceled if the graph gets canceled
167 def test_cancel_while_running(self
):
168 class MyIter(_MyIter
):
170 return self
._create
_stream
_beginning
_message
(self
._stream
)
172 class MySource(bt2
._UserSourceComponent
,
173 message_iterator_class
=MyIter
):
174 def __init__(self
, params
):
175 self
._add
_output
_port
('out')
177 class MySink(bt2
._UserSinkComponent
):
178 def __init__(self
, params
):
179 self
._add
_input
_port
('in')
182 # Pretend that somebody asynchronously cancelled the graph.
186 return next(self
._msg
_iter
)
188 def _graph_is_configured(self
):
189 self
._msg
_iter
= self
._input
_ports
['in'].create_message_iterator()
192 up
= graph
.add_component(MySource
, 'down')
193 down
= graph
.add_component(MySink
, 'up')
194 graph
.connect_ports(up
.output_ports
['out'], down
.input_ports
['in'])
195 with self
.assertRaises(bt2
.GraphCanceled
):
199 class MyIter(_MyIter
):
205 msg
= self
._create
_stream
_beginning
_message
(self
._stream
)
207 msg
= self
._create
_packet
_beginning
_message
(self
._packet
)
209 msg
= self
._create
_packet
_end
_message
(self
._packet
)
211 msg
= self
._create
_stream
_end
_message
(self
._stream
)
213 msg
= self
._create
_event
_message
(self
._ec
, self
._packet
)
218 class MySource(bt2
._UserSourceComponent
,
219 message_iterator_class
=MyIter
):
220 def __init__(self
, params
):
221 self
._add
_output
_port
('out')
223 class MySink(bt2
._UserSinkComponent
):
224 def __init__(self
, params
):
225 self
._input
_port
= self
._add
_input
_port
('in')
228 def _consume(comp_self
):
229 msg
= next(comp_self
._msg
_iter
)
231 if comp_self
._at
== 0:
232 self
.assertIsInstance(msg
, bt2
.message
._StreamBeginningMessage
)
233 elif comp_self
._at
== 1:
234 self
.assertIsInstance(msg
, bt2
.message
._PacketBeginningMessage
)
235 elif comp_self
._at
>= 2 and comp_self
._at
<= 6:
236 self
.assertIsInstance(msg
, bt2
.message
._EventMessage
)
237 self
.assertEqual(msg
.event
.cls
.name
, 'salut')
238 elif comp_self
._at
== 7:
239 self
.assertIsInstance(msg
, bt2
.message
._PacketEndMessage
)
240 elif comp_self
._at
== 8:
241 self
.assertIsInstance(msg
, bt2
.message
._StreamEndMessage
)
245 def _graph_is_configured(self
):
246 self
._msg
_iter
= self
._input
_port
.create_message_iterator()
248 src
= self
._graph
.add_component(MySource
, 'src')
249 sink
= self
._graph
.add_component(MySink
, 'sink')
250 conn
= self
._graph
.connect_ports(src
.output_ports
['out'],
251 sink
.input_ports
['in'])
254 def test_run_again(self
):
255 class MyIter(_MyIter
):
261 msg
= self
._create
_stream
_beginning
_message
(self
._stream
)
263 msg
= self
._create
_packet
_beginning
_message
(self
._packet
)
265 msg
= self
._create
_event
_message
(self
._ec
, self
._packet
)
270 class MySource(bt2
._UserSourceComponent
,
271 message_iterator_class
=MyIter
):
272 def __init__(self
, params
):
273 self
._add
_output
_port
('out')
275 class MySink(bt2
._UserSinkComponent
):
276 def __init__(self
, params
):
277 self
._input
_port
= self
._add
_input
_port
('in')
280 def _consume(comp_self
):
281 msg
= next(comp_self
._msg
_iter
)
282 if comp_self
._at
== 0:
283 self
.assertIsInstance(msg
, bt2
.message
._StreamBeginningMessage
)
284 elif comp_self
._at
== 1:
285 self
.assertIsInstance(msg
, bt2
.message
._PacketBeginningMessage
)
286 elif comp_self
._at
== 2:
287 self
.assertIsInstance(msg
, bt2
.message
._EventMessage
)
294 def _graph_is_configured(self
):
295 self
._msg
_iter
= self
._input
_port
.create_message_iterator()
297 src
= self
._graph
.add_component(MySource
, 'src')
298 sink
= self
._graph
.add_component(MySink
, 'sink')
299 conn
= self
._graph
.connect_ports(src
.output_ports
['out'],
300 sink
.input_ports
['in'])
302 with self
.assertRaises(bt2
.TryAgain
):
305 def test_run_error(self
):
306 raised_in_sink
= False
308 class MyIter(_MyIter
):
310 # If this gets called after the sink raised an exception, it is
312 nonlocal raised_in_sink
313 assert raised_in_sink
is False
316 msg
= self
._create
_stream
_beginning
_message
(self
._stream
)
318 msg
= self
._create
_packet
_beginning
_message
(self
._packet
)
319 elif self
._at
== 2 or self
._at
== 3:
320 msg
= self
._create
_event
_message
(self
._ec
, self
._packet
)
326 class MySource(bt2
._UserSourceComponent
,
327 message_iterator_class
=MyIter
):
328 def __init__(self
, params
):
329 self
._add
_output
_port
('out')
331 class MySink(bt2
._UserSinkComponent
):
332 def __init__(self
, params
):
333 self
._input
_port
= self
._add
_input
_port
('in')
336 def _consume(comp_self
):
337 msg
= next(comp_self
._msg
_iter
)
338 if comp_self
._at
== 0:
339 self
.assertIsInstance(msg
, bt2
.message
._StreamBeginningMessage
)
340 elif comp_self
._at
== 1:
341 self
.assertIsInstance(msg
, bt2
.message
._PacketBeginningMessage
)
342 elif comp_self
._at
== 2:
343 self
.assertIsInstance(msg
, bt2
.message
._EventMessage
)
344 elif comp_self
._at
== 3:
345 nonlocal raised_in_sink
346 raised_in_sink
= True
347 raise RuntimeError('error!')
351 def _graph_is_configured(self
):
352 self
._msg
_iter
= self
._input
_port
.create_message_iterator()
354 src
= self
._graph
.add_component(MySource
, 'src')
355 sink
= self
._graph
.add_component(MySink
, 'sink')
356 conn
= self
._graph
.connect_ports(src
.output_ports
['out'],
357 sink
.input_ports
['in'])
359 with self
.assertRaises(bt2
.Error
):
362 def test_listeners(self
):
363 class MyIter(bt2
._UserMessageIterator
):
367 class MySource(bt2
._UserSourceComponent
,
368 message_iterator_class
=MyIter
):
369 def __init__(self
, params
):
370 self
._add
_output
_port
('out')
371 self
._add
_output
_port
('zero')
373 class MySink(bt2
._UserSinkComponent
):
374 def __init__(self
, params
):
375 self
._add
_input
_port
('in')
380 def _port_connected(self
, port
, other_port
):
381 self
._add
_input
_port
('taste')
383 def port_added_listener(component
, port
):
385 calls
.append((port_added_listener
, component
, port
))
387 def ports_connected_listener(upstream_component
, upstream_port
,
388 downstream_component
, downstream_port
):
390 calls
.append((ports_connected_listener
,
391 upstream_component
, upstream_port
,
392 downstream_component
, downstream_port
))
395 self
._graph
.add_port_added_listener(port_added_listener
)
396 self
._graph
.add_ports_connected_listener(ports_connected_listener
)
397 src
= self
._graph
.add_component(MySource
, 'src')
398 sink
= self
._graph
.add_component(MySink
, 'sink')
399 self
._graph
.connect_ports(src
.output_ports
['out'],
400 sink
.input_ports
['in'])
402 self
.assertEqual(len(calls
), 5)
404 self
.assertIs(calls
[0][0], port_added_listener
)
405 self
.assertEqual(calls
[0][1].name
, 'src')
406 self
.assertEqual(calls
[0][2].name
, 'out')
408 self
.assertIs(calls
[1][0], port_added_listener
)
409 self
.assertEqual(calls
[1][1].name
, 'src')
410 self
.assertEqual(calls
[1][2].name
, 'zero')
412 self
.assertIs(calls
[2][0], port_added_listener
)
413 self
.assertEqual(calls
[2][1].name
, 'sink')
414 self
.assertEqual(calls
[2][2].name
, 'in')
416 self
.assertIs(calls
[3][0], port_added_listener
)
417 self
.assertEqual(calls
[3][1].name
, 'sink')
418 self
.assertEqual(calls
[3][2].name
, 'taste')
420 self
.assertIs(calls
[4][0], ports_connected_listener
)
421 self
.assertEqual(calls
[4][1].name
, 'src')
422 self
.assertEqual(calls
[4][2].name
, 'out')
423 self
.assertEqual(calls
[4][3].name
, 'sink')
424 self
.assertEqual(calls
[4][4].name
, 'in')
426 def test_invalid_listeners(self
):
427 class MyIter(bt2
._UserMessageIterator
):
431 class MySource(bt2
._UserSourceComponent
,
432 message_iterator_class
=MyIter
):
433 def __init__(self
, params
):
434 self
._add
_output
_port
('out')
435 self
._add
_output
_port
('zero')
437 class MySink(bt2
._UserSinkComponent
):
438 def __init__(self
, params
):
439 self
._add
_input
_port
('in')
444 def _port_connected(self
, port
, other_port
):
445 self
._add
_input
_port
('taste')
447 with self
.assertRaises(TypeError):
448 self
._graph
.add_port_added_listener(1234)
449 with self
.assertRaises(TypeError):
450 self
._graph
.add_ports_connected_listener(1234)
452 def test_raise_in_component_init(self
):
453 class MySink(bt2
._UserSinkComponent
):
454 def __init__(self
, params
):
455 raise ValueError('oops!')
462 with self
.assertRaises(bt2
.Error
):
463 graph
.add_component(MySink
, 'comp')
465 def test_raise_in_port_added_listener(self
):
466 class MySink(bt2
._UserSinkComponent
):
467 def __init__(self
, params
):
468 self
._add
_input
_port
('in')
473 def port_added_listener(component
, port
):
474 raise ValueError('oh noes!')
477 graph
.add_port_added_listener(port_added_listener
)
479 with self
.assertRaises(bt2
.Error
):
480 graph
.add_component(MySink
, 'comp')
482 def test_raise_in_ports_connected_listener(self
):
483 class MyIter(bt2
._UserMessageIterator
):
487 class MySource(bt2
._UserSourceComponent
,
488 message_iterator_class
=MyIter
):
489 def __init__(self
, params
):
490 self
._add
_output
_port
('out')
492 class MySink(bt2
._UserSinkComponent
):
493 def __init__(self
, params
):
494 self
._add
_input
_port
('in')
499 def ports_connected_listener(upstream_component
, upstream_port
,
500 downstream_component
, downstream_port
):
501 raise ValueError('oh noes!')
504 graph
.add_ports_connected_listener(ports_connected_listener
)
505 up
= graph
.add_component(MySource
, 'down')
506 down
= graph
.add_component(MySink
, 'up')
508 with self
.assertRaises(bt2
.Error
):
509 graph
.connect_ports(up
.output_ports
['out'], down
.input_ports
['in'])
This page took 0.040304 seconds and 4 git commands to generate.