예제 #1
0
def test_sending_signals_between_buses():
    bus1 = MessageBus().connect_sync()
    bus2 = MessageBus().connect_sync()

    add_match_msg = Message(destination='org.freedesktop.DBus',
                            path='/org/freedesktop/DBus',
                            interface='org.freedesktop.DBus',
                            member='AddMatch',
                            signature='s',
                            body=[f'sender={bus2.unique_name}'])

    bus1.call_sync(add_match_msg)

    main = GLib.MainLoop()

    def wait_for_message():
        ret = None

        def message_handler(signal):
            nonlocal ret
            if signal.sender == bus2.unique_name:
                ret = signal
                bus1.remove_message_handler(message_handler)
                main.quit()

        bus1.add_message_handler(message_handler)
        main.run()
        return ret

    bus2.send(
        Message.new_signal('/org/test/path', 'org.test.interface', 'SomeSignal', 's', ['a signal']))

    signal = wait_for_message()

    assert signal.message_type == MessageType.SIGNAL
    assert signal.path == '/org/test/path'
    assert signal.interface == 'org.test.interface'
    assert signal.member == 'SomeSignal'
    assert signal.signature == 's'
    assert signal.body == ['a signal']
예제 #2
0
async def test_sending_signals_between_buses():
    async with MessageBus().connect() as bus1, \
            MessageBus().connect() as bus2:

        add_match_msg = Message(destination='org.freedesktop.DBus',
                                path='/org/freedesktop/DBus',
                                interface='org.freedesktop.DBus',
                                member='AddMatch',
                                signature='s',
                                body=[f'sender={bus2.unique_name}'])

        await bus1.call(add_match_msg)

        async def wait_for_message():
            future = ValueEvent()

            def message_handler(signal):
                if signal.sender == bus2.unique_name:
                    bus1.remove_message_handler(message_handler)
                    future.set_result(signal)

            bus1.add_message_handler(message_handler)
            return await future

        bus2.send(
            Message.new_signal('/org/test/path', 'org.test.interface',
                               'SomeSignal', 's', ['a signal']))

        signal = await wait_for_message()

        assert signal.message_type == MessageType.SIGNAL
        assert signal.path == '/org/test/path'
        assert signal.interface == 'org.test.interface'
        assert signal.member == 'SomeSignal'
        assert signal.signature == 's'
        assert signal.body == ['a signal']