def test_add_interrupter(self): class MyIter(bt2._UserMessageIterator): def __next__(self): raise TypeError 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_consume(self): next(self._msg_iter) def _user_graph_is_configured(self): self._msg_iter = self._create_input_port_message_iterator( self._input_ports['in']) # add two interrupters, set one of them interrupter1 = bt2.Interrupter() interrupter2 = bt2.Interrupter() self._graph.add_interrupter(interrupter1) src = self._graph.add_component(MySource, 'src') sink = self._graph.add_component(MySink, 'sink') self._graph.connect_ports(src.output_ports['out'], sink.input_ports['in']) self._graph.add_interrupter(interrupter2) with self.assertRaises(bt2._Error): self._graph.run() interrupter2.set() with self.assertRaises(bt2.TryAgain): self._graph.run() interrupter2.reset() with self.assertRaises(bt2._Error): self._graph.run()
def test_query_add_interrupter(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass @classmethod def _user_query(cls, priv_query_exec, obj, params, method_obj): nonlocal interrupter2 test_self.assertFalse(query_exec.is_interrupted) interrupter2.set() test_self.assertTrue(query_exec.is_interrupted) interrupter2.reset() test_self.assertFalse(query_exec.is_interrupted) interrupter1 = bt2.Interrupter() interrupter2 = bt2.Interrupter() test_self = self query_exec = bt2.QueryExecutor(MySink, 'obj', [17, 23]) query_exec.add_interrupter(interrupter1) query_exec.add_interrupter(interrupter2) query_exec.query()
def setUp(self): self._interrupter = bt2.Interrupter()