class InformerTest(unittest.TestCase): def setUp(self): self.defaultScope = Scope("/a/test") self.informer = Informer(self.defaultScope, rsb.getDefaultParticipantConfig(), dataType=str) def tearDown(self): self.informer.deactivate() def testSendEventWrongScope(self): # Error: unrelated scope e = Event(scope=Scope("/blubb"), data='foo', type=self.informer.type) self.assertRaises(ValueError, self.informer.publishEvent, e) # OK: identical scope e = Event(scope=self.defaultScope, data='foo', type=self.informer.type) self.informer.publishEvent(e) # OK: sub-scope e = Event(scope=self.defaultScope.concat(Scope('/sub')), data='foo', type=self.informer.type) self.informer.publishEvent(e) def testSendEventWrongType(self): # Wrong type e = Event(scope=self.defaultScope, data=5) self.assertRaises(ValueError, self.informer.publishEvent, e) # Wrong type self.assertRaises(ValueError, self.informer.publishData, 5.0) # OK self.informer.publishData('bla')
def test_match(self): scope = Scope("/bla") f = rsb.filter.ScopeFilter(scope) assert scope == f.scope e = rsb.Event() e.scope = scope assert f.match(e) e.scope = scope.concat(Scope("/sub/scope")) assert f.match(e) e.scope = Scope("/blubbbbbb") assert not f.match(e)
def testMatch(self): scope = Scope("/bla") f = rsb.filter.ScopeFilter(scope) self.assertEqual(scope, f.getScope()) e = rsb.Event() e.scope = scope self.assertTrue(f.match(e)) e.scope = scope.concat(Scope("/sub/scope")) self.assertTrue(f.match(e)) e.scope = Scope("/blubbbbbb") self.assertFalse(f.match(e))
class TestInformer: @pytest.fixture(autouse=True) def set_up(self): self.default_scope = Scope("/a/test") self.informer = Informer(self.default_scope, rsb.get_default_participant_config(), data_type=str) yield self.informer.deactivate() def test_send_event_wrong_scope(self): # Error: unrelated scope e = Event(scope=Scope("/blubb"), data='foo', data_type=self.informer.data_type) with pytest.raises(ValueError): self.informer.publish_event(e) # OK: identical scope e = Event(scope=self.default_scope, data='foo', data_type=self.informer.data_type) self.informer.publish_event(e) # OK: sub-scope e = Event(scope=self.default_scope.concat(Scope('/sub')), data='foo', data_type=self.informer.data_type) self.informer.publish_event(e) def test_send_event_wrong_type(self): # Wrong type e = Event(scope=self.default_scope, data=5) with pytest.raises(ValueError): self.informer.publish_event(e) # Wrong type with pytest.raises(ValueError): self.informer.publish_data(5.0) # OK self.informer.publish_data('bla')
def testNotifyHierarchy(self): bus = Bus() targetScope = Scope("/this/is/a/test") scopes = targetScope.superScopes(True) sinksByScope = {} for scope in scopes: sinksByScope[scope] = StubSink(scope) bus.addSink(sinksByScope[scope]) notNotifiedSiblingSink = StubSink(Scope("/not/notified")) bus.addSink(notNotifiedSiblingSink) notNotifiedChildSink = StubSink(targetScope.concat(Scope("/child"))) bus.addSink(notNotifiedChildSink) event = Event(scope=targetScope) bus.handle(event) for scope, sink in sinksByScope.items(): self.assertTrue(event in sink.events) self.assertEqual(1, len(sink.events))
def test_notify_hierarchy(self): bus = Bus() target_scope = Scope("/this/is/a/test") scopes = target_scope.super_scopes(True) sinks_by_scope = {} for scope in scopes: sinks_by_scope[scope] = StubSink(scope) bus.add_sink(sinks_by_scope[scope]) not_notified_sibling_sink = StubSink(Scope("/not/notified")) bus.add_sink(not_notified_sibling_sink) not_notified_child_sink = StubSink(target_scope.concat( Scope("/child"))) bus.add_sink(not_notified_child_sink) event = Event(scope=target_scope) bus.handle(event) for scope, sink in list(sinks_by_scope.items()): assert event in sink.events assert len(sink.events) == 1