X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=tests%2Fbindings%2Fpython%2Fbt2%2Ftest_message_iterator.py;h=a40283b34f5ffcaa75b5e2d6245e03f3748385e7;hb=ffecc00e2725dea0af6a7e0cfb10a95b8f56ee68;hp=39d808887988a6111ea97a78a6c4b957898fcc29;hpb=59225a3e0e13a9c674234755e55055d9ff68d635;p=babeltrace.git diff --git a/tests/bindings/python/bt2/test_message_iterator.py b/tests/bindings/python/bt2/test_message_iterator.py index 39d80888..a40283b3 100644 --- a/tests/bindings/python/bt2/test_message_iterator.py +++ b/tests/bindings/python/bt2/test_message_iterator.py @@ -1,26 +1,14 @@ +# SPDX-License-Identifier: GPL-2.0-only # # Copyright (C) 2019 EfficiOS Inc. # -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; only version 2 -# of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# import unittest 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): @@ -34,9 +22,7 @@ class SimpleSink(bt2._UserSinkComponent): next(self._msg_iter) def _user_graph_is_configured(self): - self._msg_iter = self._create_input_port_message_iterator( - self._input_ports['in'] - ) + self._msg_iter = self._create_message_iterator(self._input_ports['in']) def _create_graph(src_comp_cls, sink_comp_cls, flt_comp_cls=None): @@ -61,7 +47,7 @@ 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 @@ -83,7 +69,7 @@ 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 @@ -92,10 +78,10 @@ class UserMessageIteratorTestCase(unittest.TestCase): 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( + self._up_iter = self._create_message_iterator( self._component._input_ports['in'] ) @@ -114,13 +100,74 @@ class UserMessageIteratorTestCase(unittest.TestCase): self.assertTrue(src_iter_initialized) self.assertTrue(flt_iter_initialized) + # Test that creating a message iterator from a sink component on a + # non-connected inport port raises. + def test_create_from_sink_component_unconnected_port_raises(self): + class MySink(bt2._UserSinkComponent): + def __init__(comp_self, config, params, obj): + comp_self._input_port = comp_self._add_input_port('in') + + def _user_graph_is_configured(comp_self): + with self.assertRaisesRegex(ValueError, 'input port is not connected'): + comp_self._create_message_iterator(comp_self._input_port) + + nonlocal seen + seen = True + + def _user_consume(self): + raise bt2.Stop + + seen = False + graph = bt2.Graph() + graph.add_component(MySink, 'snk') + graph.run() + self.assertTrue(seen) + + # Test that creating a message iterator from a message iteartor on a + # non-connected inport port raises. + def test_create_from_message_iterator_unconnected_port_raises(self): + class MyFilterIter(bt2._UserMessageIterator): + def __init__(iter_self, config, port): + input_port = iter_self._component._input_ports['in'] + + with self.assertRaisesRegex(ValueError, 'input port is not connected'): + iter_self._create_message_iterator(input_port) + + nonlocal seen + seen = True + + class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter): + def __init__(comp_self, config, params, obj): + comp_self._add_input_port('in') + comp_self._add_output_port('out') + + class MySink(bt2._UserSinkComponent): + def __init__(comp_self, config, params, obj): + comp_self._input_port = comp_self._add_input_port('in') + + def _user_graph_is_configured(comp_self): + comp_self._input_iter = comp_self._create_message_iterator( + comp_self._input_port + ) + + def _user_consume(self): + raise bt2.Stop + + seen = False + graph = bt2.Graph() + flt = graph.add_component(MyFilter, 'flt') + snk = graph.add_component(MySink, 'snk') + graph.connect_ports(flt.output_ports['out'], snk.input_ports['in']) + graph.run() + self.assertTrue(seen) + def test_create_user_error(self): # This tests both error handling by - # _UserSinkComponent._create_input_port_message_iterator - # and _UserMessageIterator._create_input_port_message_iterator, as they + # _UserSinkComponent._create_message_iterator + # and _UserMessageIterator._create_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): @@ -128,12 +175,10 @@ class UserMessageIteratorTestCase(unittest.TestCase): 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( - self._component._input_ports['in'] - ) + self._create_message_iterator(self._component._input_ports['in']) class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter): def __init__(self, config, params, obj): @@ -169,9 +214,72 @@ 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_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 @@ -187,7 +295,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 @@ -206,7 +314,7 @@ 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 @@ -224,7 +332,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) @@ -282,9 +390,7 @@ class UserMessageIteratorTestCase(unittest.TestCase): class MyFilterIter(bt2._UserMessageIterator): def __init__(self, port): input_port = port.user_data - self._upstream_iter = self._create_input_port_message_iterator( - input_port - ) + self._upstream_iter = self._create_message_iterator(input_port) def __next__(self): return next(self._upstream_iter) @@ -313,6 +419,61 @@ class UserMessageIteratorTestCase(unittest.TestCase): with self.assertRaises(bt2.TryAgain): next(it) + def test_error_in_iterator_with_cycle_after_having_created_upstream_iterator(self): + # Test a failure that triggered an abort in libbabeltrace2, in this situation: + # + # - The filter iterator creates an upstream iterator. + # - The filter iterator creates a reference cycle, including itself. + # - An exception is raised, causing the filter iterator's + # initialization method to fail. + class MySourceIter(bt2._UserMessageIterator): + pass + + class MySource(bt2._UserSourceComponent, message_iterator_class=MySourceIter): + def __init__(self, config, params, obj): + self._add_output_port('out') + + class MyFilterIter(bt2._UserMessageIterator): + def __init__(self, config, port): + # First, create an upstream iterator. + self._upstream_iter = self._create_message_iterator( + self._component._input_ports['in'] + ) + + # Then, voluntarily make a reference cycle that will keep this + # Python object alive, which will keep the upstream iterator + # Babeltrace object alive. + self._self = self + + # Finally, raise an exception to make __init__ fail. + raise ValueError('woops') + + class MyFilter(bt2._UserFilterComponent, message_iterator_class=MyFilterIter): + def __init__(self, config, params, obj): + self._in = self._add_input_port('in') + self._out = self._add_output_port('out') + + class MySink(bt2._UserSinkComponent): + def __init__(self, config, params, obj): + self._input_port = self._add_input_port('in') + + def _user_graph_is_configured(self): + self._upstream_iter = self._create_message_iterator(self._input_port) + + def _user_consume(self): + # We should not reach this. + assert False + + g = bt2.Graph() + src = g.add_component(MySource, 'src') + flt = g.add_component(MyFilter, 'flt') + snk = g.add_component(MySink, 'snk') + g.connect_ports(src.output_ports['out'], flt.input_ports['in']) + g.connect_ports(flt.output_ports['out'], snk.input_ports['in']) + + with self.assertRaisesRegex(bt2._Error, 'ValueError: woops'): + g.run() + def _setup_seek_test( sink_cls, @@ -320,9 +481,10 @@ def _setup_seek_test( user_can_seek_beginning=None, user_seek_ns_from_origin=None, user_can_seek_ns_from_origin=None, + can_seek_forward=False, ): 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) @@ -337,6 +499,7 @@ def _setup_seek_test( self._create_stream_end_message(stream), ] self._at = 0 + config.can_seek_forward = can_seek_forward def __next__(self): if self._at < len(self._msgs): @@ -367,10 +530,11 @@ def _setup_seek_test( self._add_output_port('out', (tc, sc, ec)) class MyFilterIter(bt2._UserMessageIterator): - def __init__(self, port): - self._upstream_iter = self._create_input_port_message_iterator( + def __init__(self, config, port): + self._upstream_iter = self._create_message_iterator( self._component._input_ports['in'] ) + config.can_seek_forward = self._upstream_iter.can_seek_forward def __next__(self): return next(self._upstream_iter) @@ -396,15 +560,20 @@ def _setup_seek_test( class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): + def test_can_seek_beginning_without_seek_beginning(self): + with self.assertRaisesRegex( + bt2._IncompleteUserClass, + "cannot create component class 'MySource': message iterator class implements _user_can_seek_beginning but not _user_seek_beginning", + ): + _setup_seek_test(SimpleSink, user_can_seek_beginning=lambda: None) + def test_can_seek_beginning(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'] - ) + self._msg_iter = self._create_message_iterator(self._input_ports['in']) def _user_consume(self): nonlocal can_seek_beginning @@ -415,7 +584,9 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): return input_port_iter_can_seek_beginning graph = _setup_seek_test( - MySink, user_can_seek_beginning=_user_can_seek_beginning + MySink, + user_can_seek_beginning=_user_can_seek_beginning, + user_seek_beginning=lambda: None, ) input_port_iter_can_seek_beginning = True @@ -436,9 +607,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): self._add_input_port('in') def _user_graph_is_configured(self): - self._msg_iter = self._create_input_port_message_iterator( - self._input_ports['in'] - ) + self._msg_iter = self._create_message_iterator(self._input_ports['in']) def _user_consume(self): nonlocal can_seek_beginning @@ -460,9 +629,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): self._add_input_port('in') def _user_graph_is_configured(self): - self._msg_iter = self._create_input_port_message_iterator( - self._input_ports['in'] - ) + self._msg_iter = self._create_message_iterator(self._input_ports['in']) def _user_consume(self): nonlocal can_seek_beginning @@ -479,9 +646,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): self._add_input_port('in') def _user_graph_is_configured(self): - self._msg_iter = self._create_input_port_message_iterator( - self._input_ports['in'] - ) + self._msg_iter = self._create_message_iterator(self._input_ports['in']) def _user_consume(self): # This is expected to raise. @@ -491,7 +656,9 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): raise ValueError('moustiquaire') graph = _setup_seek_test( - MySink, user_can_seek_beginning=_user_can_seek_beginning + MySink, + user_can_seek_beginning=_user_can_seek_beginning, + user_seek_beginning=lambda: None, ) with self.assertRaises(bt2._Error) as ctx: @@ -506,9 +673,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): self._add_input_port('in') def _user_graph_is_configured(self): - self._msg_iter = self._create_input_port_message_iterator( - self._input_ports['in'] - ) + self._msg_iter = self._create_message_iterator(self._input_ports['in']) def _user_consume(self): # This is expected to raise. @@ -518,7 +683,9 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): return 'Amqui' graph = _setup_seek_test( - MySink, user_can_seek_beginning=_user_can_seek_beginning + MySink, + user_can_seek_beginning=_user_can_seek_beginning, + user_seek_beginning=lambda: None, ) with self.assertRaises(bt2._Error) as ctx: @@ -533,9 +700,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): self._add_input_port('in') def _user_graph_is_configured(self): - self._msg_iter = self._create_input_port_message_iterator( - self._input_ports['in'] - ) + self._msg_iter = self._create_message_iterator(self._input_ports['in']) def _user_consume(self): nonlocal do_seek_beginning @@ -577,9 +742,7 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): self._add_input_port('in') def _user_graph_is_configured(self): - self._msg_iter = self._create_input_port_message_iterator( - self._input_ports['in'] - ) + self._msg_iter = self._create_message_iterator(self._input_ports['in']) def _user_consume(self): self._msg_iter.seek_beginning() @@ -594,127 +757,282 @@ class UserMessageIteratorSeekBeginningTestCase(unittest.TestCase): 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 test_can_seek_ns_from_origin_without_seek_ns_from_origin(self): + # Test the case where: + # + # - can_seek_ns_from_origin: Returns True (don't really care, as long + # as it's provided) + # - seek_ns_from_origin provided: No + # - can the iterator seek beginning: Don't care + # - can the iterator seek forward: Don't care + for can_seek_ns_from_origin in (False, True): + for iter_can_seek_beginning in (False, True): + for iter_can_seek_forward in (False, True): + with self.assertRaisesRegex( + bt2._IncompleteUserClass, + "cannot create component class 'MySource': message iterator class implements _user_can_seek_ns_from_origin but not _user_seek_ns_from_origin", + ): + self._can_seek_ns_from_origin_test( + None, + user_can_seek_ns_from_origin_ret_val=True, + user_seek_ns_from_origin_provided=False, + iter_can_seek_beginning=iter_can_seek_beginning, + iter_can_seek_forward=iter_can_seek_forward, + ) + + def test_can_seek_ns_from_origin_returns_true(self): + # Test the case where: + # + # - can_seek_ns_from_origin: returns True + # - seek_ns_from_origin provided: Yes + # - can the iterator seek beginning: Don't care + # - can the iterator seek forward: Don't care + # + # We expect iter.can_seek_ns_from_origin to return True. + for iter_can_seek_beginning in (False, True): + for iter_can_seek_forward in (False, True): + self._can_seek_ns_from_origin_test( + expected_outcome=True, + user_can_seek_ns_from_origin_ret_val=True, + user_seek_ns_from_origin_provided=True, + iter_can_seek_beginning=iter_can_seek_beginning, + iter_can_seek_forward=iter_can_seek_forward, ) - 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 test_can_seek_ns_from_origin_returns_false_can_seek_beginning_forward_seekable( + self, + ): + # Test the case where: + # + # - can_seek_ns_from_origin: returns False + # - seek_ns_from_origin provided: Yes + # - can the iterator seek beginning: Yes + # - can the iterator seek forward: Yes + # + # We expect iter.can_seek_ns_from_origin to return True. + self._can_seek_ns_from_origin_test( + expected_outcome=True, + user_can_seek_ns_from_origin_ret_val=False, + user_seek_ns_from_origin_provided=True, + iter_can_seek_beginning=True, + iter_can_seek_forward=True, + ) + + def test_can_seek_ns_from_origin_returns_false_can_seek_beginning_not_forward_seekable( + self, + ): + # Test the case where: + # + # - can_seek_ns_from_origin: returns False + # - seek_ns_from_origin provided: Yes + # - can the iterator seek beginning: Yes + # - can the iterator seek forward: No + # + # We expect iter.can_seek_ns_from_origin to return False. + self._can_seek_ns_from_origin_test( + expected_outcome=False, + user_can_seek_ns_from_origin_ret_val=False, + user_seek_ns_from_origin_provided=True, + iter_can_seek_beginning=True, + iter_can_seek_forward=False, + ) + + def test_can_seek_ns_from_origin_returns_false_cant_seek_beginning_forward_seekable( + self, + ): + # Test the case where: + # + # - can_seek_ns_from_origin: returns False + # - seek_ns_from_origin provided: Yes + # - can the iterator seek beginning: No + # - can the iterator seek forward: Yes + # + # We expect iter.can_seek_ns_from_origin to return False. + self._can_seek_ns_from_origin_test( + expected_outcome=False, + user_can_seek_ns_from_origin_ret_val=False, + user_seek_ns_from_origin_provided=True, + iter_can_seek_beginning=False, + iter_can_seek_forward=True, + ) + + def test_can_seek_ns_from_origin_returns_false_cant_seek_beginning_not_forward_seekable( + self, + ): + # Test the case where: + # + # - can_seek_ns_from_origin: returns False + # - seek_ns_from_origin provided: Yes + # - can the iterator seek beginning: No + # - can the iterator seek forward: No + # + # We expect iter.can_seek_ns_from_origin to return False. + self._can_seek_ns_from_origin_test( + expected_outcome=False, + user_can_seek_ns_from_origin_ret_val=False, + user_seek_ns_from_origin_provided=True, + iter_can_seek_beginning=False, + iter_can_seek_forward=False, + ) + + def test_no_can_seek_ns_from_origin_seek_ns_from_origin(self): + # Test the case where: + # + # - can_seek_ns_from_origin: Not provided + # - seek_ns_from_origin provided: Yes + # - can the iterator seek beginning: Don't care + # - can the iterator seek forward: Don't care + # + # We expect iter.can_seek_ns_from_origin to return True. + for iter_can_seek_beginning in (False, True): + for iter_can_seek_forward in (False, True): + self._can_seek_ns_from_origin_test( + expected_outcome=True, + user_can_seek_ns_from_origin_ret_val=None, + user_seek_ns_from_origin_provided=True, + iter_can_seek_beginning=iter_can_seek_beginning, + iter_can_seek_forward=iter_can_seek_forward, ) - 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 + def test_no_can_seek_ns_from_origin_no_seek_ns_from_origin_can_seek_beginning_forward_seekable( + self, + ): + # Test the case where: + # + # - can_seek_ns_from_origin: Not provided + # - seek_ns_from_origin provided: Not provided + # - can the iterator seek beginning: Yes + # - can the iterator seek forward: Yes + # + # We expect iter.can_seek_ns_from_origin to return True. + self._can_seek_ns_from_origin_test( + expected_outcome=True, + user_can_seek_ns_from_origin_ret_val=None, + user_seek_ns_from_origin_provided=False, + iter_can_seek_beginning=True, + iter_can_seek_forward=True, + ) - graph = _setup_seek_test( - MySink, user_can_seek_ns_from_origin=_user_can_seek_ns_from_origin + def test_no_can_seek_ns_from_origin_no_seek_ns_from_origin_can_seek_beginning_not_forward_seekable( + self, + ): + # Test the case where: + # + # - can_seek_ns_from_origin: Not provided + # - seek_ns_from_origin provided: Not provided + # - can the iterator seek beginning: Yes + # - can the iterator seek forward: No + # + # We expect iter.can_seek_ns_from_origin to return False. + self._can_seek_ns_from_origin_test( + expected_outcome=False, + user_can_seek_ns_from_origin_ret_val=None, + user_seek_ns_from_origin_provided=False, + iter_can_seek_beginning=True, + iter_can_seek_forward=False, ) - 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) + def test_no_can_seek_ns_from_origin_no_seek_ns_from_origin_cant_seek_beginning_forward_seekable( + self, + ): + # Test the case where: + # + # - can_seek_ns_from_origin: Not provided + # - seek_ns_from_origin provided: Not provided + # - can the iterator seek beginning: No + # - can the iterator seek forward: Yes + # + # We expect iter.can_seek_ns_from_origin to return False. + self._can_seek_ns_from_origin_test( + expected_outcome=False, + user_can_seek_ns_from_origin_ret_val=None, + user_seek_ns_from_origin_provided=False, + iter_can_seek_beginning=False, + iter_can_seek_forward=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_no_seek_ns_from_origin_cant_seek_beginning_not_forward_seekable( + self, + ): + # Test the case where: + # + # - can_seek_ns_from_origin: Not provided + # - seek_ns_from_origin provided: Not provided + # - can the iterator seek beginning: No + # - can the iterator seek forward: No + # + # We expect iter.can_seek_ns_from_origin to return False. + self._can_seek_ns_from_origin_test( + expected_outcome=False, + user_can_seek_ns_from_origin_ret_val=None, + user_seek_ns_from_origin_provided=False, + iter_can_seek_beginning=False, + iter_can_seek_forward=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. + def _can_seek_ns_from_origin_test( + self, + expected_outcome, + user_can_seek_ns_from_origin_ret_val, + user_seek_ns_from_origin_provided, + iter_can_seek_beginning, + iter_can_seek_forward, + ): 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'] - ) + self._msg_iter = self._create_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 + passed_ns_from_origin ) - def _user_seek_ns_from_origin(self): - pass + if user_can_seek_ns_from_origin_ret_val is not None: - 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 user_can_seek_ns_from_origin(self, ns_from_origin): + nonlocal received_ns_from_origin + received_ns_from_origin = ns_from_origin + return user_can_seek_ns_from_origin_ret_val - 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') + else: + user_can_seek_ns_from_origin = None - def _user_graph_is_configured(self): - self._msg_iter = self._create_input_port_message_iterator( - self._input_ports['in'] - ) + if user_seek_ns_from_origin_provided: - 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, ns_from_origin): + pass - def _user_seek_beginning(self): - pass + else: + user_seek_ns_from_origin = None - 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) + if iter_can_seek_beginning: - 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_seek_beginning(self): + pass - def _user_graph_is_configured(self): - self._msg_iter = self._create_input_port_message_iterator( - self._input_ports['in'] - ) + else: + user_seek_beginning = None - 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, + user_can_seek_ns_from_origin=user_can_seek_ns_from_origin, + user_seek_ns_from_origin=user_seek_ns_from_origin, + user_seek_beginning=user_seek_beginning, + can_seek_forward=iter_can_seek_forward, + ) - graph = _setup_seek_test(MySink) + passed_ns_from_origin = 77 + received_ns_from_origin = None can_seek_ns_from_origin = None - test_ns_from_origin = 2 graph.run_once() - self.assertIs(can_seek_ns_from_origin, False) + self.assertIs(can_seek_ns_from_origin, expected_outcome) + + if user_can_seek_ns_from_origin_ret_val is not None: + self.assertEqual(received_ns_from_origin, passed_ns_from_origin) def test_can_seek_ns_from_origin_user_error(self): class MySink(bt2._UserSinkComponent): @@ -722,9 +1040,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase): self._add_input_port('in') def _user_graph_is_configured(self): - self._msg_iter = self._create_input_port_message_iterator( - self._input_ports['in'] - ) + self._msg_iter = self._create_message_iterator(self._input_ports['in']) def _user_consume(self): # This is expected to raise. @@ -734,7 +1050,9 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase): raise ValueError('Joutel') graph = _setup_seek_test( - MySink, user_can_seek_ns_from_origin=_user_can_seek_ns_from_origin + MySink, + user_can_seek_ns_from_origin=_user_can_seek_ns_from_origin, + user_seek_ns_from_origin=lambda: None, ) with self.assertRaises(bt2._Error) as ctx: @@ -749,9 +1067,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase): self._add_input_port('in') def _user_graph_is_configured(self): - self._msg_iter = self._create_input_port_message_iterator( - self._input_ports['in'] - ) + self._msg_iter = self._create_message_iterator(self._input_ports['in']) def _user_consume(self): # This is expected to raise. @@ -761,7 +1077,9 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase): return 'Nitchequon' graph = _setup_seek_test( - MySink, user_can_seek_ns_from_origin=_user_can_seek_ns_from_origin + MySink, + user_can_seek_ns_from_origin=_user_can_seek_ns_from_origin, + user_seek_ns_from_origin=lambda: None, ) with self.assertRaises(bt2._Error) as ctx: @@ -776,9 +1094,7 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase): self._add_input_port('in') def _user_graph_is_configured(self): - self._msg_iter = self._create_input_port_message_iterator( - self._input_ports['in'] - ) + self._msg_iter = self._create_message_iterator(self._input_ports['in']) def _user_consume(self): self._msg_iter.seek_ns_from_origin(17) @@ -787,7 +1103,6 @@ class UserMessageIteratorSeekNsFromOriginTestCase(unittest.TestCase): 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 )