bt2: Adapt test_message_iterator.py and make it pass
[babeltrace.git] / tests / bindings / python / bt2 / test_message_iterator.py
CommitLineData
ae0bfae8 1from bt2 import value
0d0edb5e 2import collections
f6a5e476
PP
3import unittest
4import copy
5import bt2
6
7
fa4c33e3 8class UserMessageIteratorTestCase(unittest.TestCase):
f6a5e476
PP
9 @staticmethod
10 def _create_graph(src_comp_cls):
11 class MySink(bt2._UserSinkComponent):
12 def __init__(self, params):
13 self._add_input_port('in')
14
15 def _consume(self):
fa4c33e3 16 next(self._msg_iter)
f6a5e476 17
a4dcfa96
SM
18 def _graph_is_configured(self):
19 self._msg_iter = self._input_ports['in'].create_message_iterator()
f6a5e476
PP
20
21 graph = bt2.Graph()
22 src_comp = graph.add_component(src_comp_cls, 'src')
23 sink_comp = graph.add_component(MySink, 'sink')
24 graph.connect_ports(src_comp.output_ports['out'],
25 sink_comp.input_ports['in'])
26 return graph
27
28 def test_init(self):
a4dcfa96
SM
29 the_output_port_from_source = None
30 the_output_port_from_iter = None
31
fa4c33e3 32 class MyIter(bt2._UserMessageIterator):
a4dcfa96 33 def __init__(self, self_port_output):
f6a5e476 34 nonlocal initialized
a4dcfa96 35 nonlocal the_output_port_from_iter
f6a5e476 36 initialized = True
a4dcfa96 37 the_output_port_from_iter = self_port_output
f6a5e476
PP
38
39 class MySource(bt2._UserSourceComponent,
fa4c33e3 40 message_iterator_class=MyIter):
f6a5e476 41 def __init__(self, params):
a4dcfa96
SM
42 nonlocal the_output_port_from_source
43 the_output_port_from_source = self._add_output_port('out')
f6a5e476
PP
44
45 initialized = False
46 graph = self._create_graph(MySource)
a4dcfa96 47 graph.run()
f6a5e476 48 self.assertTrue(initialized)
a4dcfa96 49 self.assertEqual(the_output_port_from_source.addr, the_output_port_from_iter.addr)
f6a5e476
PP
50
51 def test_finalize(self):
fa4c33e3 52 class MyIter(bt2._UserMessageIterator):
f6a5e476
PP
53 def _finalize(self):
54 nonlocal finalized
55 finalized = True
56
57 class MySource(bt2._UserSourceComponent,
fa4c33e3 58 message_iterator_class=MyIter):
f6a5e476
PP
59 def __init__(self, params):
60 self._add_output_port('out')
61
62 finalized = False
63 graph = self._create_graph(MySource)
a4dcfa96 64 graph.run()
f6a5e476
PP
65 del graph
66 self.assertTrue(finalized)
67
68 def test_component(self):
fa4c33e3 69 class MyIter(bt2._UserMessageIterator):
a4dcfa96 70 def __init__(self, self_port_output):
f6a5e476
PP
71 nonlocal salut
72 salut = self._component._salut
73
74 class MySource(bt2._UserSourceComponent,
fa4c33e3 75 message_iterator_class=MyIter):
f6a5e476
PP
76 def __init__(self, params):
77 self._add_output_port('out')
78 self._salut = 23
79
80 salut = None
81 graph = self._create_graph(MySource)
a4dcfa96 82 graph.run()
f6a5e476
PP
83 self.assertEqual(salut, 23)
84
85 def test_addr(self):
fa4c33e3 86 class MyIter(bt2._UserMessageIterator):
a4dcfa96 87 def __init__(self, self_port_output):
f6a5e476
PP
88 nonlocal addr
89 addr = self.addr
90
91 class MySource(bt2._UserSourceComponent,
fa4c33e3 92 message_iterator_class=MyIter):
f6a5e476
PP
93 def __init__(self, params):
94 self._add_output_port('out')
95
96 addr = None
97 graph = self._create_graph(MySource)
a4dcfa96 98 graph.run()
f6a5e476
PP
99 self.assertIsNotNone(addr)
100 self.assertNotEqual(addr, 0)
101
102
fa4c33e3 103class OutputPortMessageIteratorTestCase(unittest.TestCase):
0d0edb5e 104 def test_component(self):
fa4c33e3 105 class MyIter(bt2._UserMessageIterator):
a4dcfa96 106 def __init__(self, self_port_output):
0d0edb5e
PP
107 self._at = 0
108
0d0edb5e 109 def __next__(self):
a4dcfa96 110 if self._at == 7:
0d0edb5e
PP
111 raise bt2.Stop
112
a4dcfa96
SM
113 if self._at == 0:
114 msg = self._create_stream_beginning_message(test_obj._stream)
115 elif self._at == 1:
116 msg = self._create_packet_beginning_message(test_obj._packet)
117 elif self._at == 5:
118 msg = self._create_packet_end_message(test_obj._packet)
119 elif self._at == 6:
120 msg = self._create_stream_end_message(test_obj._stream)
121 else:
122 msg = self._create_event_message(test_obj._event_class, test_obj._packet)
123 msg.event.payload_field['my_int'] = self._at * 3
124
0d0edb5e 125 self._at += 1
fa4c33e3 126 return msg
0d0edb5e
PP
127
128 class MySource(bt2._UserSourceComponent,
fa4c33e3 129 message_iterator_class=MyIter):
0d0edb5e
PP
130 def __init__(self, params):
131 self._add_output_port('out')
132
a4dcfa96
SM
133 trace_class = self._create_trace_class()
134 stream_class = trace_class.create_stream_class()
135
136 # Create payload field class
137 my_int_ft = trace_class.create_signed_integer_field_class(32)
138 payload_ft = trace_class.create_structure_field_class()
139 payload_ft += collections.OrderedDict([
140 ('my_int', my_int_ft),
141 ])
142
143 event_class = stream_class.create_event_class(name='salut', payload_field_class=payload_ft)
144
145 trace = trace_class()
146 stream = trace.create_stream(stream_class)
147 packet = stream.create_packet()
148
149 test_obj._event_class = event_class
150 test_obj._stream = stream
151 test_obj._packet = packet
152
153 test_obj = self
0d0edb5e
PP
154 graph = bt2.Graph()
155 src = graph.add_component(MySource, 'src')
a4dcfa96 156 msg_iter = graph.create_output_port_message_iterator(src.output_ports['out'])
0d0edb5e 157
fa4c33e3 158 for at, msg in enumerate(msg_iter):
a4dcfa96
SM
159 if at == 0:
160 self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
161 elif at == 1:
162 self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
163 elif at == 5:
164 self.assertIsInstance(msg, bt2.message._PacketEndMessage)
165 elif at == 6:
166 self.assertIsInstance(msg, bt2.message._StreamEndMessage)
167 else:
168 self.assertIsInstance(msg, bt2.message._EventMessage)
169 self.assertEqual(msg.event.event_class.name, 'salut')
170 field = msg.event.payload_field['my_int']
171 self.assertEqual(field, at * 3)
This page took 0.037437 seconds and 4 git commands to generate.