bt2: Adapt test_message_iterator.py and make it pass
[babeltrace.git] / tests / bindings / python / bt2 / test_message_iterator.py
CommitLineData
c4239792 1from bt2 import value
548bb510 2import collections
811644b8
PP
3import unittest
4import copy
5import bt2
6
7
5602ef81 8class UserMessageIteratorTestCase(unittest.TestCase):
811644b8
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):
5602ef81 16 next(self._msg_iter)
811644b8 17
c5f330cd
SM
18 def _graph_is_configured(self):
19 self._msg_iter = self._input_ports['in'].create_message_iterator()
811644b8
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):
c5f330cd
SM
29 the_output_port_from_source = None
30 the_output_port_from_iter = None
31
5602ef81 32 class MyIter(bt2._UserMessageIterator):
c5f330cd 33 def __init__(self, self_port_output):
811644b8 34 nonlocal initialized
c5f330cd 35 nonlocal the_output_port_from_iter
811644b8 36 initialized = True
c5f330cd 37 the_output_port_from_iter = self_port_output
811644b8
PP
38
39 class MySource(bt2._UserSourceComponent,
5602ef81 40 message_iterator_class=MyIter):
811644b8 41 def __init__(self, params):
c5f330cd
SM
42 nonlocal the_output_port_from_source
43 the_output_port_from_source = self._add_output_port('out')
811644b8
PP
44
45 initialized = False
46 graph = self._create_graph(MySource)
c5f330cd 47 graph.run()
811644b8 48 self.assertTrue(initialized)
c5f330cd 49 self.assertEqual(the_output_port_from_source.addr, the_output_port_from_iter.addr)
811644b8
PP
50
51 def test_finalize(self):
5602ef81 52 class MyIter(bt2._UserMessageIterator):
811644b8
PP
53 def _finalize(self):
54 nonlocal finalized
55 finalized = True
56
57 class MySource(bt2._UserSourceComponent,
5602ef81 58 message_iterator_class=MyIter):
811644b8
PP
59 def __init__(self, params):
60 self._add_output_port('out')
61
62 finalized = False
63 graph = self._create_graph(MySource)
c5f330cd 64 graph.run()
811644b8
PP
65 del graph
66 self.assertTrue(finalized)
67
68 def test_component(self):
5602ef81 69 class MyIter(bt2._UserMessageIterator):
c5f330cd 70 def __init__(self, self_port_output):
811644b8
PP
71 nonlocal salut
72 salut = self._component._salut
73
74 class MySource(bt2._UserSourceComponent,
5602ef81 75 message_iterator_class=MyIter):
811644b8
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)
c5f330cd 82 graph.run()
811644b8
PP
83 self.assertEqual(salut, 23)
84
85 def test_addr(self):
5602ef81 86 class MyIter(bt2._UserMessageIterator):
c5f330cd 87 def __init__(self, self_port_output):
811644b8
PP
88 nonlocal addr
89 addr = self.addr
90
91 class MySource(bt2._UserSourceComponent,
5602ef81 92 message_iterator_class=MyIter):
811644b8
PP
93 def __init__(self, params):
94 self._add_output_port('out')
95
96 addr = None
97 graph = self._create_graph(MySource)
c5f330cd 98 graph.run()
811644b8
PP
99 self.assertIsNotNone(addr)
100 self.assertNotEqual(addr, 0)
101
102
5602ef81 103class OutputPortMessageIteratorTestCase(unittest.TestCase):
548bb510 104 def test_component(self):
5602ef81 105 class MyIter(bt2._UserMessageIterator):
c5f330cd 106 def __init__(self, self_port_output):
548bb510
PP
107 self._at = 0
108
548bb510 109 def __next__(self):
c5f330cd 110 if self._at == 7:
548bb510
PP
111 raise bt2.Stop
112
c5f330cd
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
548bb510 125 self._at += 1
5602ef81 126 return msg
548bb510
PP
127
128 class MySource(bt2._UserSourceComponent,
5602ef81 129 message_iterator_class=MyIter):
548bb510
PP
130 def __init__(self, params):
131 self._add_output_port('out')
132
c5f330cd
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
548bb510
PP
154 graph = bt2.Graph()
155 src = graph.add_component(MySource, 'src')
c5f330cd 156 msg_iter = graph.create_output_port_message_iterator(src.output_ports['out'])
548bb510 157
5602ef81 158 for at, msg in enumerate(msg_iter):
c5f330cd
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.038684 seconds and 4 git commands to generate.