tests: Move data files to a common directory
[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 42 nonlocal the_output_port_from_source
2e00bc76 43 the_output_port_from_source = self._add_output_port('out', 'user data')
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)
2e00bc76 50 self.assertEqual(the_output_port_from_iter.user_data, 'user data')
811644b8
PP
51
52 def test_finalize(self):
5602ef81 53 class MyIter(bt2._UserMessageIterator):
811644b8
PP
54 def _finalize(self):
55 nonlocal finalized
56 finalized = True
57
58 class MySource(bt2._UserSourceComponent,
5602ef81 59 message_iterator_class=MyIter):
811644b8
PP
60 def __init__(self, params):
61 self._add_output_port('out')
62
63 finalized = False
64 graph = self._create_graph(MySource)
c5f330cd 65 graph.run()
811644b8
PP
66 del graph
67 self.assertTrue(finalized)
68
69 def test_component(self):
5602ef81 70 class MyIter(bt2._UserMessageIterator):
c5f330cd 71 def __init__(self, self_port_output):
811644b8
PP
72 nonlocal salut
73 salut = self._component._salut
74
75 class MySource(bt2._UserSourceComponent,
5602ef81 76 message_iterator_class=MyIter):
811644b8
PP
77 def __init__(self, params):
78 self._add_output_port('out')
79 self._salut = 23
80
81 salut = None
82 graph = self._create_graph(MySource)
c5f330cd 83 graph.run()
811644b8
PP
84 self.assertEqual(salut, 23)
85
86 def test_addr(self):
5602ef81 87 class MyIter(bt2._UserMessageIterator):
c5f330cd 88 def __init__(self, self_port_output):
811644b8
PP
89 nonlocal addr
90 addr = self.addr
91
92 class MySource(bt2._UserSourceComponent,
5602ef81 93 message_iterator_class=MyIter):
811644b8
PP
94 def __init__(self, params):
95 self._add_output_port('out')
96
97 addr = None
98 graph = self._create_graph(MySource)
c5f330cd 99 graph.run()
811644b8
PP
100 self.assertIsNotNone(addr)
101 self.assertNotEqual(addr, 0)
102
103
5602ef81 104class OutputPortMessageIteratorTestCase(unittest.TestCase):
548bb510 105 def test_component(self):
5602ef81 106 class MyIter(bt2._UserMessageIterator):
c5f330cd 107 def __init__(self, self_port_output):
548bb510
PP
108 self._at = 0
109
548bb510 110 def __next__(self):
c5f330cd 111 if self._at == 7:
548bb510
PP
112 raise bt2.Stop
113
c5f330cd
SM
114 if self._at == 0:
115 msg = self._create_stream_beginning_message(test_obj._stream)
116 elif self._at == 1:
117 msg = self._create_packet_beginning_message(test_obj._packet)
118 elif self._at == 5:
119 msg = self._create_packet_end_message(test_obj._packet)
120 elif self._at == 6:
121 msg = self._create_stream_end_message(test_obj._stream)
122 else:
123 msg = self._create_event_message(test_obj._event_class, test_obj._packet)
124 msg.event.payload_field['my_int'] = self._at * 3
125
548bb510 126 self._at += 1
5602ef81 127 return msg
548bb510
PP
128
129 class MySource(bt2._UserSourceComponent,
5602ef81 130 message_iterator_class=MyIter):
548bb510
PP
131 def __init__(self, params):
132 self._add_output_port('out')
133
c5f330cd
SM
134 trace_class = self._create_trace_class()
135 stream_class = trace_class.create_stream_class()
136
137 # Create payload field class
138 my_int_ft = trace_class.create_signed_integer_field_class(32)
139 payload_ft = trace_class.create_structure_field_class()
140 payload_ft += collections.OrderedDict([
141 ('my_int', my_int_ft),
142 ])
143
144 event_class = stream_class.create_event_class(name='salut', payload_field_class=payload_ft)
145
146 trace = trace_class()
147 stream = trace.create_stream(stream_class)
148 packet = stream.create_packet()
149
150 test_obj._event_class = event_class
151 test_obj._stream = stream
152 test_obj._packet = packet
153
154 test_obj = self
548bb510
PP
155 graph = bt2.Graph()
156 src = graph.add_component(MySource, 'src')
c5f330cd 157 msg_iter = graph.create_output_port_message_iterator(src.output_ports['out'])
548bb510 158
5602ef81 159 for at, msg in enumerate(msg_iter):
c5f330cd
SM
160 if at == 0:
161 self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
162 elif at == 1:
163 self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
164 elif at == 5:
165 self.assertIsInstance(msg, bt2.message._PacketEndMessage)
166 elif at == 6:
167 self.assertIsInstance(msg, bt2.message._StreamEndMessage)
168 else:
169 self.assertIsInstance(msg, bt2.message._EventMessage)
e8ac1aae 170 self.assertEqual(msg.event.cls.name, 'salut')
c5f330cd
SM
171 field = msg.event.payload_field['my_int']
172 self.assertEqual(field, at * 3)
This page took 0.04096 seconds and 4 git commands to generate.