+ src = graph.add_component(MySource, 'src')
+ flt = graph.add_component(MyFilter, 'flt')
+ graph.connect_ports(src.output_ports['out'], flt.input_ports['in'])
+ it = graph.create_output_port_message_iterator(flt.output_ports['out'])
+
+ return it, MySourceIter
+
+ def test_can_seek_beginning(self):
+ it, MySourceIter = self._setup_seek_beginning_test()
+
+ def _can_seek_beginning(self):
+ nonlocal can_seek_beginning
+ return can_seek_beginning
+
+ MySourceIter._can_seek_beginning = property(_can_seek_beginning)
+
+ can_seek_beginning = True
+ self.assertTrue(it.can_seek_beginning)
+
+ can_seek_beginning = False
+ self.assertFalse(it.can_seek_beginning)
+
+ # Once can_seek_beginning returns an error, verify that it raises when
+ # _can_seek_beginning has/returns the wrong type.
+
+ # Remove the _can_seek_beginning method, we now rely on the presence of
+ # a _seek_beginning method to know whether the iterator can seek to
+ # beginning or not.
+ del MySourceIter._can_seek_beginning
+ self.assertTrue(it.can_seek_beginning)
+
+ del MySourceIter._seek_beginning
+ self.assertFalse(it.can_seek_beginning)
+
+ def test_seek_beginning(self):
+ it, MySourceIter = self._setup_seek_beginning_test()
+
+ msg = next(it)
+ self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
+ msg = next(it)
+ self.assertIsInstance(msg, bt2.message._PacketBeginningMessage)
+
+ it.seek_beginning()
+
+ msg = next(it)
+ self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
+
+ # Verify that we can seek beginning after having reached the end.
+ #
+ # It currently does not work to seek an output port message iterator
+ # once it's ended, but we should eventually make it work and uncomment
+ # the following snippet.
+ #
+ # try:
+ # while True:
+ # next(it)
+ # except bt2.Stop:
+ # pass
+ #
+ # it.seek_beginning()
+ # msg = next(it)
+ # self.assertIsInstance(msg, bt2.message._StreamBeginningMessage)
+
+ def test_seek_beginning_user_error(self):
+ it, MySourceIter = self._setup_seek_beginning_test()
+
+ def _seek_beginning_error(self):
+ raise ValueError('ouch')
+
+ MySourceIter._seek_beginning = _seek_beginning_error
+
+ with self.assertRaises(bt2.Error):
+ it.seek_beginning()