Commit | Line | Data |
---|---|---|
d2d857a8 MJ |
1 | |
2 | # | |
3 | # Copyright (C) 2019 EfficiOS Inc. | |
4 | # | |
5 | # This program is free software; you can redistribute it and/or | |
6 | # modify it under the terms of the GNU General Public License | |
7 | # as published by the Free Software Foundation; only version 2 | |
8 | # of the License. | |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program; if not, write to the Free Software | |
17 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | # | |
19 | ||
c4239792 | 20 | from bt2 import value |
548bb510 | 21 | import collections |
811644b8 PP |
22 | import unittest |
23 | import copy | |
24 | import bt2 | |
25 | ||
26 | ||
5602ef81 | 27 | class UserMessageIteratorTestCase(unittest.TestCase): |
811644b8 PP |
28 | @staticmethod |
29 | def _create_graph(src_comp_cls): | |
30 | class MySink(bt2._UserSinkComponent): | |
31 | def __init__(self, params): | |
32 | self._add_input_port('in') | |
33 | ||
34 | def _consume(self): | |
5602ef81 | 35 | next(self._msg_iter) |
811644b8 | 36 | |
c5f330cd SM |
37 | def _graph_is_configured(self): |
38 | self._msg_iter = self._input_ports['in'].create_message_iterator() | |
811644b8 PP |
39 | |
40 | graph = bt2.Graph() | |
41 | src_comp = graph.add_component(src_comp_cls, 'src') | |
42 | sink_comp = graph.add_component(MySink, 'sink') | |
43 | graph.connect_ports(src_comp.output_ports['out'], | |
44 | sink_comp.input_ports['in']) | |
45 | return graph | |
46 | ||
47 | def test_init(self): | |
c5f330cd SM |
48 | the_output_port_from_source = None |
49 | the_output_port_from_iter = None | |
50 | ||
5602ef81 | 51 | class MyIter(bt2._UserMessageIterator): |
c5f330cd | 52 | def __init__(self, self_port_output): |
811644b8 | 53 | nonlocal initialized |
c5f330cd | 54 | nonlocal the_output_port_from_iter |
811644b8 | 55 | initialized = True |
c5f330cd | 56 | the_output_port_from_iter = self_port_output |
811644b8 PP |
57 | |
58 | class MySource(bt2._UserSourceComponent, | |
5602ef81 | 59 | message_iterator_class=MyIter): |
811644b8 | 60 | def __init__(self, params): |
c5f330cd | 61 | nonlocal the_output_port_from_source |
2e00bc76 | 62 | the_output_port_from_source = self._add_output_port('out', 'user data') |
811644b8 PP |
63 | |
64 | initialized = False | |
65 | graph = self._create_graph(MySource) | |
c5f330cd | 66 | graph.run() |
811644b8 | 67 | self.assertTrue(initialized) |
c5f330cd | 68 | self.assertEqual(the_output_port_from_source.addr, the_output_port_from_iter.addr) |
2e00bc76 | 69 | self.assertEqual(the_output_port_from_iter.user_data, 'user data') |
811644b8 PP |
70 | |
71 | def test_finalize(self): | |
5602ef81 | 72 | class MyIter(bt2._UserMessageIterator): |
811644b8 PP |
73 | def _finalize(self): |
74 | nonlocal finalized | |
75 | finalized = True | |
76 | ||
77 | class MySource(bt2._UserSourceComponent, | |
5602ef81 | 78 | message_iterator_class=MyIter): |
811644b8 PP |
79 | def __init__(self, params): |
80 | self._add_output_port('out') | |
81 | ||
82 | finalized = False | |
83 | graph = self._create_graph(MySource) | |
c5f330cd | 84 | graph.run() |
811644b8 PP |
85 | del graph |
86 | self.assertTrue(finalized) | |
87 | ||
88 | def test_component(self): | |
5602ef81 | 89 | class MyIter(bt2._UserMessageIterator): |
c5f330cd | 90 | def __init__(self, self_port_output): |
811644b8 PP |
91 | nonlocal salut |
92 | salut = self._component._salut | |
93 | ||
94 | class MySource(bt2._UserSourceComponent, | |
5602ef81 | 95 | message_iterator_class=MyIter): |
811644b8 PP |
96 | def __init__(self, params): |
97 | self._add_output_port('out') | |
98 | self._salut = 23 | |
99 | ||
100 | salut = None | |
101 | graph = self._create_graph(MySource) | |
c5f330cd | 102 | graph.run() |
811644b8 PP |
103 | self.assertEqual(salut, 23) |
104 | ||
105 | def test_addr(self): | |
5602ef81 | 106 | class MyIter(bt2._UserMessageIterator): |
c5f330cd | 107 | def __init__(self, self_port_output): |
811644b8 PP |
108 | nonlocal addr |
109 | addr = self.addr | |
110 | ||
111 | class MySource(bt2._UserSourceComponent, | |
5602ef81 | 112 | message_iterator_class=MyIter): |
811644b8 PP |
113 | def __init__(self, params): |
114 | self._add_output_port('out') | |
115 | ||
116 | addr = None | |
117 | graph = self._create_graph(MySource) | |
c5f330cd | 118 | graph.run() |
811644b8 PP |
119 | self.assertIsNotNone(addr) |
120 | self.assertNotEqual(addr, 0) | |
121 | ||
122 | ||
5602ef81 | 123 | class OutputPortMessageIteratorTestCase(unittest.TestCase): |
548bb510 | 124 | def test_component(self): |
5602ef81 | 125 | class MyIter(bt2._UserMessageIterator): |
c5f330cd | 126 | def __init__(self, self_port_output): |
548bb510 PP |
127 | self._at = 0 |
128 | ||
548bb510 | 129 | def __next__(self): |
c5f330cd | 130 | if self._at == 7: |
548bb510 PP |
131 | raise bt2.Stop |
132 | ||
c5f330cd SM |
133 | if self._at == 0: |
134 | msg = self._create_stream_beginning_message(test_obj._stream) | |
135 | elif self._at == 1: | |
136 | msg = self._create_packet_beginning_message(test_obj._packet) | |
137 | elif self._at == 5: | |
138 | msg = self._create_packet_end_message(test_obj._packet) | |
139 | elif self._at == 6: | |
140 | msg = self._create_stream_end_message(test_obj._stream) | |
141 | else: | |
142 | msg = self._create_event_message(test_obj._event_class, test_obj._packet) | |
143 | msg.event.payload_field['my_int'] = self._at * 3 | |
144 | ||
548bb510 | 145 | self._at += 1 |
5602ef81 | 146 | return msg |
548bb510 PP |
147 | |
148 | class MySource(bt2._UserSourceComponent, | |
5602ef81 | 149 | message_iterator_class=MyIter): |
548bb510 PP |
150 | def __init__(self, params): |
151 | self._add_output_port('out') | |
152 | ||
c5f330cd SM |
153 | trace_class = self._create_trace_class() |
154 | stream_class = trace_class.create_stream_class() | |
155 | ||
156 | # Create payload field class | |
157 | my_int_ft = trace_class.create_signed_integer_field_class(32) | |
158 | payload_ft = trace_class.create_structure_field_class() | |
159 | payload_ft += collections.OrderedDict([ | |
160 | ('my_int', my_int_ft), | |
161 | ]) | |
162 | ||
163 | event_class = stream_class.create_event_class(name='salut', payload_field_class=payload_ft) | |
164 | ||
165 | trace = trace_class() | |
166 | stream = trace.create_stream(stream_class) | |
167 | packet = stream.create_packet() | |
168 | ||
169 | test_obj._event_class = event_class | |
170 | test_obj._stream = stream | |
171 | test_obj._packet = packet | |
172 | ||
173 | test_obj = self | |
548bb510 PP |
174 | graph = bt2.Graph() |
175 | src = graph.add_component(MySource, 'src') | |
c5f330cd | 176 | msg_iter = graph.create_output_port_message_iterator(src.output_ports['out']) |
548bb510 | 177 | |
5602ef81 | 178 | for at, msg in enumerate(msg_iter): |
c5f330cd SM |
179 | if at == 0: |
180 | self.assertIsInstance(msg, bt2.message._StreamBeginningMessage) | |
181 | elif at == 1: | |
182 | self.assertIsInstance(msg, bt2.message._PacketBeginningMessage) | |
183 | elif at == 5: | |
184 | self.assertIsInstance(msg, bt2.message._PacketEndMessage) | |
185 | elif at == 6: | |
186 | self.assertIsInstance(msg, bt2.message._StreamEndMessage) | |
187 | else: | |
188 | self.assertIsInstance(msg, bt2.message._EventMessage) | |
e8ac1aae | 189 | self.assertEqual(msg.event.cls.name, 'salut') |
c5f330cd SM |
190 | field = msg.event.payload_field['my_int'] |
191 | self.assertEqual(field, at * 3) |