Пример #1
0
    def test_hierarchy_sending(self):

        send_scope = Scope("/this/is/a/test")
        super_scopes = send_scope.super_scopes(True)

        out_connector = self._get_out_connector(send_scope, activate=False)
        out_configurator = rsb.eventprocessing.OutRouteConfigurator(
            connectors=[out_connector])
        informer = create_informer(send_scope,
                                   data_type=str,
                                   configurator=out_configurator)

        # set up listeners on the complete hierarchy
        listeners = []
        receivers = []
        for scope in super_scopes:

            in_connector = self._get_in_connector(scope, activate=False)
            in_configurator = rsb.eventprocessing.InRouteConfigurator(
                connectors=[in_connector])

            listener = create_listener(scope, configurator=in_configurator)
            listeners.append(listener)

            receiver = SettingReceiver(scope)

            listener.add_handler(receiver)

            receivers.append(receiver)

        data = "a string to test"
        informer.publish_data(data)

        for receiver in receivers:
            with receiver.result_condition:
                while receiver.result_event is None:
                    receiver.result_condition.wait(10)
                if receiver.result_event is None:
                    pytest.fail(
                        "Listener on scope {} did not receive an event".format(
                            receiver.scope))
                assert receiver.result_event.data == data

        for listener in listeners:
            listener.deactivate()
        informer.deactivate()
    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