X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=tests%2Fbindings%2Fpython%2Fbt2%2Ftest_message_iterator.py;h=b331e4a40996188dd88cc6f0872d2451370d9549;hb=8d8b141db4c46135a35be19e4a1c192f6a36d67b;hp=ce968ea851cae8f643aab9bc38f446125b983cd7;hpb=7f0c21bb99a20d28739bee293fa4066989b2f3d6;p=babeltrace.git diff --git a/tests/bindings/python/bt2/test_message_iterator.py b/tests/bindings/python/bt2/test_message_iterator.py index ce968ea8..b331e4a4 100644 --- a/tests/bindings/python/bt2/test_message_iterator.py +++ b/tests/bindings/python/bt2/test_message_iterator.py @@ -21,13 +21,14 @@ import bt2 import sys from utils import TestOutputPortMessageIterator from bt2 import port as bt2_port +from bt2 import message_iterator as bt2_message_iterator class SimpleSink(bt2._UserSinkComponent): # Straightforward sink that creates one input port (`in`) and consumes from # it. - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_consume(self): @@ -61,14 +62,14 @@ class UserMessageIteratorTestCase(unittest.TestCase): the_output_port_from_iter = None class MyIter(bt2._UserMessageIterator): - def __init__(self, self_port_output): + def __init__(self, config, self_port_output): nonlocal initialized nonlocal the_output_port_from_iter initialized = True the_output_port_from_iter = self_port_output class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): nonlocal the_output_port_from_source the_output_port_from_source = self._add_output_port('out', 'user data') @@ -83,16 +84,16 @@ class UserMessageIteratorTestCase(unittest.TestCase): def test_create_from_message_iterator(self): class MySourceIter(bt2._UserMessageIterator): - def __init__(self, self_port_output): + def __init__(self, config, self_port_output): nonlocal src_iter_initialized src_iter_initialized = True class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') class MyFilterIter(bt2._UserMessageIterator): - def __init__(self, self_port_output): + def __init__(self, config, self_port_output): nonlocal flt_iter_initialized flt_iter_initialized = True self._up_iter = self._create_input_port_message_iterator( @@ -103,7 +104,7 @@ class UserMessageIteratorTestCase(unittest.TestCase): return next(self._up_iter) class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') self._add_output_port('out') @@ -120,15 +121,15 @@ class UserMessageIteratorTestCase(unittest.TestCase): # and _UserMessageIterator._create_input_port_message_iterator, as they # are both used in the graph. class MySourceIter(bt2._UserMessageIterator): - def __init__(self, self_port_output): + def __init__(self, config, self_port_output): raise ValueError('Very bad error') class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') class MyFilterIter(bt2._UserMessageIterator): - def __init__(self, self_port_output): + def __init__(self, config, self_port_output): # This is expected to raise because of the error in # MySourceIter.__init__. self._create_input_port_message_iterator( @@ -136,7 +137,7 @@ class UserMessageIteratorTestCase(unittest.TestCase): ) class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') self._add_output_port('out') @@ -160,7 +161,7 @@ class UserMessageIteratorTestCase(unittest.TestCase): finalized = True class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') finalized = False @@ -169,14 +170,79 @@ class UserMessageIteratorTestCase(unittest.TestCase): del graph self.assertTrue(finalized) + def test_config_parameter(self): + class MyIter(bt2._UserMessageIterator): + def __init__(self, config, port): + nonlocal config_type + config_type = type(config) + + class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): + def __init__(self, config, params, obj): + self._add_output_port('out') + + config_type = None + graph = _create_graph(MySource, SimpleSink) + graph.run() + self.assertIs(config_type, bt2_message_iterator._MessageIteratorConfiguration) + + def _test_config_can_seek_forward(self, set_can_seek_forward): + class MyIter(bt2._UserMessageIterator): + def __init__(self, config, port): + if set_can_seek_forward: + config.can_seek_forward = True + + class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): + def __init__(self, config, params, obj): + self._add_output_port('out') + + class MySink(bt2._UserSinkComponent): + def __init__(self, config, params, obj): + self._add_input_port('in') + + def _user_graph_is_configured(self): + self._msg_iter = self._create_input_port_message_iterator( + self._input_ports['in'] + ) + + def _user_consume(self): + nonlocal can_seek_forward + can_seek_forward = self._msg_iter.can_seek_forward + + can_seek_forward = None + graph = _create_graph(MySource, MySink) + graph.run_once() + self.assertIs(can_seek_forward, set_can_seek_forward) + + def test_config_can_seek_forward_default(self): + self._test_config_can_seek_forward(False) + + def test_config_can_seek_forward(self): + self._test_config_can_seek_forward(True) + + def test_config_can_seek_forward_wrong_type(self): + class MyIter(bt2._UserMessageIterator): + def __init__(self, config, port): + config.can_seek_forward = 1 + + class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): + def __init__(self, config, params, obj): + self._add_output_port('out') + + graph = _create_graph(MySource, SimpleSink) + with self.assertRaises(bt2._Error) as ctx: + graph.run() + + root_cause = ctx.exception[0] + self.assertIn("TypeError: 'int' is not a 'bool' object", root_cause.message) + def test_component(self): class MyIter(bt2._UserMessageIterator): - def __init__(self, self_port_output): + def __init__(self, config, self_port_output): nonlocal salut salut = self._component._salut class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') self._salut = 23 @@ -187,7 +253,7 @@ class UserMessageIteratorTestCase(unittest.TestCase): def test_port(self): class MyIter(bt2._UserMessageIterator): - def __init__(self_iter, self_port_output): + def __init__(self_iter, config, self_port_output): nonlocal called called = True port = self_iter._port @@ -196,7 +262,7 @@ class UserMessageIteratorTestCase(unittest.TestCase): self.assertEqual(self_port_output.addr, port.addr) class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') called = False @@ -206,12 +272,12 @@ class UserMessageIteratorTestCase(unittest.TestCase): def test_addr(self): class MyIter(bt2._UserMessageIterator): - def __init__(self, self_port_output): + def __init__(self, config, self_port_output): nonlocal addr addr = self.addr class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') addr = None @@ -224,7 +290,7 @@ class UserMessageIteratorTestCase(unittest.TestCase): # and can be re-used. def test_reuse_message(self): class MyIter(bt2._UserMessageIterator): - def __init__(self, port): + def __init__(self, config, port): tc, sc, ec = port.user_data trace = tc() stream = trace.create_stream(sc) @@ -244,7 +310,7 @@ class UserMessageIteratorTestCase(unittest.TestCase): return self._msgs.pop(0) class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): tc = self._create_trace_class() sc = tc.create_stream_class(supports_packets=True) ec = sc.create_event_class() @@ -276,7 +342,7 @@ class UserMessageIteratorTestCase(unittest.TestCase): raise bt2.TryAgain class MySource(bt2._UserSourceComponent, message_iterator_class=MyIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_output_port('out') class MyFilterIter(bt2._UserMessageIterator): @@ -296,7 +362,7 @@ class UserMessageIteratorTestCase(unittest.TestCase): return self._upstream_iter.can_seek_beginning() class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): input_port = self._add_input_port('in') self._add_output_port('out', input_port) @@ -314,9 +380,15 @@ class UserMessageIteratorTestCase(unittest.TestCase): next(it) -def _setup_seek_test(sink_cls, user_seek_beginning=None, user_can_seek_beginning=None): +def _setup_seek_test( + sink_cls, + user_seek_beginning=None, + user_can_seek_beginning=None, + user_seek_ns_from_origin=None, + user_can_seek_ns_from_origin=None, +): class MySourceIter(bt2._UserMessageIterator): - def __init__(self, port): + def __init__(self, config, port): tc, sc, ec = port.user_data trace = tc() stream = trace.create_stream(sc) @@ -346,8 +418,14 @@ def _setup_seek_test(sink_cls, user_seek_beginning=None, user_can_seek_beginning if user_can_seek_beginning is not None: MySourceIter._user_can_seek_beginning = user_can_seek_beginning + if user_seek_ns_from_origin is not None: + MySourceIter._user_seek_ns_from_origin = user_seek_ns_from_origin + + if user_can_seek_ns_from_origin is not None: + MySourceIter._user_can_seek_ns_from_origin = user_can_seek_ns_from_origin + class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): tc = self._create_trace_class() sc = tc.create_stream_class(supports_packets=True) ec = sc.create_event_class() @@ -355,7 +433,7 @@ def _setup_seek_test(sink_cls, user_seek_beginning=None, user_can_seek_beginning self._add_output_port('out', (tc, sc, ec)) class MyFilterIter(bt2._UserMessageIterator): - def __init__(self, port): + def __init__(self, config, port): self._upstream_iter = self._create_input_port_message_iterator( self._component._input_ports['in'] ) @@ -369,8 +447,14 @@ def _setup_seek_test(sink_cls, user_seek_beginning=None, user_can_seek_beginning def _user_seek_beginning(self): self._upstream_iter.seek_beginning() + def _user_can_seek_ns_from_origin(self, ns_from_origin): + return self._upstream_iter.can_seek_ns_from_origin(ns_from_origin) + + def _user_seek_ns_from_origin(self, ns_from_origin): + self._upstream_iter.seek_ns_from_origin(ns_from_origin) + class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') self._add_output_port('out') @@ -380,7 +464,7 @@ def _setup_seek_test(sink_cls, user_seek_beginning=None, user_can_seek_beginning class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): def test_can_seek_beginning(self): class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_graph_is_configured(self): @@ -414,7 +498,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): # Test an iterator without a _user_can_seek_beginning method, but with # a _user_seek_beginning method. class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_graph_is_configured(self): @@ -438,7 +522,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): # Test an iterator without a _user_can_seek_beginning method, without # a _user_seek_beginning method. class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_graph_is_configured(self): @@ -457,7 +541,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): def test_can_seek_beginning_user_error(self): class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_graph_is_configured(self): @@ -484,7 +568,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): def test_can_seek_beginning_wrong_return_value(self): class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_graph_is_configured(self): @@ -511,7 +595,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): def test_seek_beginning(self): class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_graph_is_configured(self): @@ -555,7 +639,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): def test_seek_beginning_user_error(self): class MySink(bt2._UserSinkComponent): - def __init__(self, params, obj): + def __init__(self, config, params, obj): self._add_input_port('in') def _user_graph_is_configured(self): @@ -575,5 +659,209 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): graph.run_once() +class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase): + def test_can_seek_ns_from_origin(self): + class MySink(bt2._UserSinkComponent): + def __init__(self, config, params, obj): + self._add_input_port('in') + + def _user_graph_is_configured(self): + self._msg_iter = self._create_input_port_message_iterator( + self._input_ports['in'] + ) + + def _user_consume(self): + nonlocal can_seek_ns_from_origin + nonlocal test_ns_from_origin + can_seek_ns_from_origin = self._msg_iter.can_seek_ns_from_origin( + test_ns_from_origin + ) + + def _user_can_seek_ns_from_origin(iter_self, ns_from_origin): + nonlocal input_port_iter_can_seek_ns_from_origin + nonlocal test_ns_from_origin + self.assertEqual(ns_from_origin, test_ns_from_origin) + return input_port_iter_can_seek_ns_from_origin + + graph = _setup_seek_test( + MySink, user_can_seek_ns_from_origin=_user_can_seek_ns_from_origin + ) + + input_port_iter_can_seek_ns_from_origin = True + can_seek_ns_from_origin = None + test_ns_from_origin = 1 + graph.run_once() + self.assertIs(can_seek_ns_from_origin, True) + + input_port_iter_can_seek_ns_from_origin = False + can_seek_ns_from_origin = None + test_ns_from_origin = 2 + graph.run_once() + self.assertIs(can_seek_ns_from_origin, False) + + def test_no_can_seek_ns_from_origin_with_seek_ns_from_origin(self): + # Test an iterator without a _user_can_seek_ns_from_origin method, but + # with a _user_seek_ns_from_origin method. + class MySink(bt2._UserSinkComponent): + def __init__(self, config, params, obj): + self._add_input_port('in') + + def _user_graph_is_configured(self): + self._msg_iter = self._create_input_port_message_iterator( + self._input_ports['in'] + ) + + def _user_consume(self): + nonlocal can_seek_ns_from_origin + nonlocal test_ns_from_origin + can_seek_ns_from_origin = self._msg_iter.can_seek_ns_from_origin( + test_ns_from_origin + ) + + def _user_seek_ns_from_origin(self): + pass + + graph = _setup_seek_test( + MySink, user_seek_ns_from_origin=_user_seek_ns_from_origin + ) + can_seek_ns_from_origin = None + test_ns_from_origin = 2 + graph.run_once() + self.assertIs(can_seek_ns_from_origin, True) + + def test_no_can_seek_ns_from_origin_with_seek_beginning(self): + # Test an iterator without a _user_can_seek_ns_from_origin method, but + # with a _user_seek_beginning method. + class MySink(bt2._UserSinkComponent): + def __init__(self, config, params, obj): + self._add_input_port('in') + + def _user_graph_is_configured(self): + self._msg_iter = self._create_input_port_message_iterator( + self._input_ports['in'] + ) + + def _user_consume(self): + nonlocal can_seek_ns_from_origin + nonlocal test_ns_from_origin + can_seek_ns_from_origin = self._msg_iter.can_seek_ns_from_origin( + test_ns_from_origin + ) + + def _user_seek_beginning(self): + pass + + graph = _setup_seek_test(MySink, user_seek_beginning=_user_seek_beginning) + can_seek_ns_from_origin = None + test_ns_from_origin = 2 + graph.run_once() + self.assertIs(can_seek_ns_from_origin, True) + + def test_no_can_seek_ns_from_origin(self): + # Test an iterator without a _user_can_seek_ns_from_origin method + # and no other related method. + class MySink(bt2._UserSinkComponent): + def __init__(self, config, params, obj): + self._add_input_port('in') + + def _user_graph_is_configured(self): + self._msg_iter = self._create_input_port_message_iterator( + self._input_ports['in'] + ) + + def _user_consume(self): + nonlocal can_seek_ns_from_origin + nonlocal test_ns_from_origin + can_seek_ns_from_origin = self._msg_iter.can_seek_ns_from_origin( + test_ns_from_origin + ) + + graph = _setup_seek_test(MySink) + can_seek_ns_from_origin = None + test_ns_from_origin = 2 + graph.run_once() + self.assertIs(can_seek_ns_from_origin, False) + + def test_can_seek_ns_from_origin_user_error(self): + class MySink(bt2._UserSinkComponent): + def __init__(self, config, params, obj): + self._add_input_port('in') + + def _user_graph_is_configured(self): + self._msg_iter = self._create_input_port_message_iterator( + self._input_ports['in'] + ) + + def _user_consume(self): + # This is expected to raise. + self._msg_iter.can_seek_ns_from_origin(2) + + def _user_can_seek_ns_from_origin(self, ns_from_origin): + raise ValueError('Joutel') + + graph = _setup_seek_test( + MySink, user_can_seek_ns_from_origin=_user_can_seek_ns_from_origin + ) + + with self.assertRaises(bt2._Error) as ctx: + graph.run_once() + + cause = ctx.exception[0] + self.assertIn('ValueError: Joutel', cause.message) + + def test_can_seek_ns_from_origin_wrong_return_value(self): + class MySink(bt2._UserSinkComponent): + def __init__(self, config, params, obj): + self._add_input_port('in') + + def _user_graph_is_configured(self): + self._msg_iter = self._create_input_port_message_iterator( + self._input_ports['in'] + ) + + def _user_consume(self): + # This is expected to raise. + self._msg_iter.can_seek_ns_from_origin(2) + + def _user_can_seek_ns_from_origin(self, ns_from_origin): + return 'Nitchequon' + + graph = _setup_seek_test( + MySink, user_can_seek_ns_from_origin=_user_can_seek_ns_from_origin + ) + + with self.assertRaises(bt2._Error) as ctx: + graph.run_once() + + cause = ctx.exception[0] + self.assertIn("TypeError: 'str' is not a 'bool' object", cause.message) + + def test_seek_ns_from_origin(self): + class MySink(bt2._UserSinkComponent): + def __init__(self, config, params, obj): + self._add_input_port('in') + + def _user_graph_is_configured(self): + self._msg_iter = self._create_input_port_message_iterator( + self._input_ports['in'] + ) + + def _user_consume(self): + self._msg_iter.seek_ns_from_origin(17) + + def _user_seek_ns_from_origin(self, ns_from_origin): + nonlocal actual_ns_from_origin + actual_ns_from_origin = ns_from_origin + + msg = None + graph = _setup_seek_test( + MySink, user_seek_ns_from_origin=_user_seek_ns_from_origin + ) + + actual_ns_from_origin = None + graph.run_once() + self.assertEqual(actual_ns_from_origin, 17) + + if __name__ == '__main__': unittest.main()