File: test_signals.py

package info (click to toggle)
python-dbus-next 0.2.3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704 kB
  • sloc: python: 6,018; makefile: 45; xml: 29
file content (212 lines) | stat: -rw-r--r-- 6,808 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from dbus_next.service import ServiceInterface, signal
from dbus_next.aio import MessageBus
from dbus_next import Message
from dbus_next.introspection import Node
from dbus_next.constants import RequestNameReply

import pytest


class ExampleInterface(ServiceInterface):
    def __init__(self):
        super().__init__('test.interface')

    @signal()
    def SomeSignal(self) -> 's':
        return 'hello'

    @signal()
    def SignalMultiple(self) -> 'ss':
        return ['hello', 'world']


@pytest.mark.asyncio
async def test_signals():
    bus1 = await MessageBus().connect()
    bus2 = await MessageBus().connect()

    bus_intr = await bus1.introspect('org.freedesktop.DBus', '/org/freedesktop/DBus')
    bus_obj = bus1.get_proxy_object('org.freedesktop.DBus', '/org/freedesktop/DBus', bus_intr)
    stats = bus_obj.get_interface('org.freedesktop.DBus.Debug.Stats')

    await bus1.request_name('test.signals.name')
    service_interface = ExampleInterface()
    bus1.export('/test/path', service_interface)

    obj = bus2.get_proxy_object('test.signals.name', '/test/path',
                                bus1._introspect_export_path('/test/path'))
    interface = obj.get_interface(service_interface.name)

    async def ping():
        await bus2.call(
            Message(destination=bus1.unique_name,
                    interface='org.freedesktop.DBus.Peer',
                    path='/test/path',
                    member='Ping'))

    err = None

    single_counter = 0

    def single_handler(value):
        try:
            nonlocal single_counter
            nonlocal err
            assert value == 'hello'
            single_counter += 1
        except Exception as e:
            err = e

    multiple_counter = 0

    def multiple_handler(value1, value2):
        nonlocal multiple_counter
        nonlocal err
        try:
            assert value1 == 'hello'
            assert value2 == 'world'
            multiple_counter += 1
        except Exception as e:
            err = e

    await ping()
    match_rules = await stats.call_get_all_match_rules()
    assert bus2.unique_name in match_rules
    bus_match_rules = match_rules[bus2.unique_name]
    # the bus connection itself takes a rule on NameOwnerChange after the high
    # level client is initialized
    assert len(bus_match_rules) == 1
    assert len(bus2._user_message_handlers) == 0

    interface.on_some_signal(single_handler)
    interface.on_signal_multiple(multiple_handler)

    # Interlude: adding a signal handler with `on_[signal]` should add a match rule and
    # message handler. Removing a signal handler with `off_[signal]` should
    # remove the match rule and message handler to avoid memory leaks.
    await ping()
    match_rules = await stats.call_get_all_match_rules()
    assert bus2.unique_name in match_rules
    bus_match_rules = match_rules[bus2.unique_name]
    # test the match rule and user handler has been added
    assert len(bus_match_rules) == 2
    assert "type='signal',interface='test.interface',path='/test/path',sender='test.signals.name'" in bus_match_rules
    assert len(bus2._user_message_handlers) == 1

    service_interface.SomeSignal()
    await ping()
    assert err is None
    assert single_counter == 1

    service_interface.SignalMultiple()
    await ping()
    assert err is None
    assert multiple_counter == 1

    # special case: another bus with the same path and interface but on a
    # different name and connection will trigger the match rule of the first
    # (happens with mpris)
    bus3 = await MessageBus().connect()
    await bus3.request_name('test.signals.name2')
    service_interface2 = ExampleInterface()
    bus3.export('/test/path', service_interface2)

    obj = bus2.get_proxy_object('test.signals.name2', '/test/path',
                                bus3._introspect_export_path('/test/path'))
    # we have to add a dummy handler to add the match rule
    iface2 = obj.get_interface(service_interface2.name)

    def dummy_signal_handler(what):
        pass

    iface2.on_some_signal(dummy_signal_handler)
    await ping()

    service_interface2.SomeSignal()
    await ping()
    # single_counter is not incremented for signals of the second interface
    assert single_counter == 1

    interface.off_some_signal(single_handler)
    interface.off_signal_multiple(multiple_handler)
    iface2.off_some_signal(dummy_signal_handler)

    # After `off_[signal]`, the match rule and user handler should be removed
    await ping()
    match_rules = await stats.call_get_all_match_rules()
    assert bus2.unique_name in match_rules
    bus_match_rules = match_rules[bus2.unique_name]
    assert len(bus_match_rules) == 1
    assert "type='signal',interface='test.interface',path='/test/path',sender='test.signals.name'" not in bus_match_rules
    assert len(bus2._user_message_handlers) == 0

    bus1.disconnect()
    bus2.disconnect()
    bus3.disconnect()


@pytest.mark.asyncio
async def test_signals_with_changing_owners():
    well_known_name = 'test.signals.changing.name'

    bus1 = await MessageBus().connect()
    bus2 = await MessageBus().connect()
    bus3 = await MessageBus().connect()

    async def ping():
        await bus1.call(
            Message(destination=bus1.unique_name,
                    interface='org.freedesktop.DBus.Peer',
                    path='/test/path',
                    member='Ping'))

    service_interface = ExampleInterface()
    introspection = Node.default()
    introspection.interfaces.append(service_interface.introspect())

    # get the interface before export
    obj = bus1.get_proxy_object(well_known_name, '/test/path', introspection)
    iface = obj.get_interface('test.interface')
    counter = 0

    def handler(what):
        nonlocal counter
        counter += 1

    iface.on_some_signal(handler)
    await ping()

    # now export and get the name
    bus2.export('/test/path', service_interface)
    result = await bus2.request_name(well_known_name)
    assert result is RequestNameReply.PRIMARY_OWNER

    # the signal should work
    service_interface.SomeSignal()
    await ping()
    assert counter == 1
    counter = 0

    # now queue up a transfer of the name
    service_interface2 = ExampleInterface()
    bus3.export('/test/path', service_interface2)
    result = await bus3.request_name(well_known_name)
    assert result is RequestNameReply.IN_QUEUE

    # if it doesn't own the name, the signal shouldn't work here
    service_interface2.SomeSignal()
    await ping()
    assert counter == 0

    # now transfer over the name and it should work
    bus2.disconnect()
    await ping()

    service_interface2.SomeSignal()
    await ping()
    assert counter == 1
    counter = 0

    bus1.disconnect()
    bus2.disconnect()
    bus3.disconnect()