1
|
|
|
import pytest |
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
def test_signal_registration(basicApp): |
|
|
|
|
5
|
|
|
plugin = basicApp.plugins.get("BasicPlugin") |
6
|
|
|
signals = plugin.signals.get() |
7
|
|
|
assert len(signals.keys()) == 1 |
8
|
|
|
assert "test" in signals.keys() |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def test_signal_send(basicApp): |
|
|
|
|
12
|
|
|
answers = basicApp.signals.send("test", basicApp, text="test") |
13
|
|
|
assert len(answers) == 1 |
14
|
|
|
assert isinstance(answers, list) |
15
|
|
|
assert len(answers[0]) == 2 |
16
|
|
|
assert isinstance(answers[0], tuple) |
17
|
|
|
assert answers[0][1] == {'text': 'test'} |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def test_signal_connect(basicApp): |
|
|
|
|
21
|
|
|
def _test_command(plugin, **kwargs): |
|
|
|
|
22
|
|
|
return "12345" |
23
|
|
|
|
24
|
|
|
plugin = basicApp.plugins.get("BasicPlugin") |
25
|
|
|
plugin.signals.connect("12345 receiver", "test", _test_command, "receiver 12345 for test") |
26
|
|
|
answers = plugin.signals.send("test") |
27
|
|
|
assert len(answers) == 2 |
28
|
|
|
for answer in answers: |
29
|
|
|
if answer[0] == _test_command: |
30
|
|
|
assert answer[1] == "12345" |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def test_signal_disconnect(basicApp): |
|
|
|
|
34
|
|
|
def _test_command(plugin, **kwargs): |
|
|
|
|
35
|
|
|
return "12345" |
36
|
|
|
|
37
|
|
|
plugin = basicApp.plugins.get("BasicPlugin") |
38
|
|
|
plugin.signals.connect("12345 receiver", "test", _test_command, "receiver 12345 for test") |
39
|
|
|
answers = plugin.signals.send("test") |
40
|
|
|
assert len(answers) == 2 |
41
|
|
|
for answer in answers: |
42
|
|
|
if answer[0] == _test_command: |
43
|
|
|
assert answer[1] == "12345" |
44
|
|
|
|
45
|
|
|
plugin.signals.disconnect("12345 receiver") |
46
|
|
|
answers = plugin.signals.send("test") |
47
|
|
|
assert len(answers) == 1 |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
def test_receiver_get(basicApp): |
|
|
|
|
51
|
|
|
def _test_command(plugin, **kwargs): |
|
|
|
|
52
|
|
|
return "12345" |
53
|
|
|
|
54
|
|
|
plugin = basicApp.plugins.get("BasicPlugin") |
55
|
|
|
receivers = plugin.signals.get_receiver() |
56
|
|
|
assert len(receivers) == 1 # test receiver |
57
|
|
|
plugin.signals.connect("12345 receiver", "test", _test_command, "receiver 12345 for test") |
58
|
|
|
receivers = plugin.signals.get_receiver() |
59
|
|
|
assert len(receivers) == 2 |
60
|
|
|
plugin.deactivate() |
61
|
|
|
receivers = plugin.signals.get_receiver() |
62
|
|
|
assert len(receivers) == 0 |
|
|
|
|
63
|
|
|
|
64
|
|
|
|
65
|
|
|
def test_multi_plugin_deactivations(basicApp, EmptyPlugin): |
|
|
|
|
66
|
|
|
def test(): |
|
|
|
|
67
|
|
|
pass |
68
|
|
|
|
69
|
|
|
plugin = basicApp.plugins.get("BasicPlugin") |
70
|
|
|
plugin2 = EmptyPlugin(app=basicApp, name="EmptyPlugin") |
71
|
|
|
plugin2.activate() |
72
|
|
|
assert plugin2.active is True |
73
|
|
|
|
74
|
|
|
plugin2.signals.register("test123", "test123 description") |
75
|
|
|
plugin2_signals = plugin2.signals.get() |
76
|
|
|
assert len(plugin2_signals) == 1 |
77
|
|
|
|
78
|
|
|
plugin_signals = plugin.signals.get() |
79
|
|
|
# BasicPlugin has already registered a signal during init |
80
|
|
|
assert len(plugin_signals) == 1 |
81
|
|
|
|
82
|
|
|
plugin2.deactivate() |
83
|
|
|
plugin2_signals = plugin2.signals.get() |
84
|
|
|
assert len(plugin2_signals) == 0 |
|
|
|
|
85
|
|
|
assert plugin2.active is False |
86
|
|
|
|
87
|
|
|
assert len(plugin_signals) == 1 |
88
|
|
|
assert plugin.active is True |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
def test_signal_handling(basicApp, EmptyPlugin): |
|
|
|
|
92
|
|
|
from blinker import NamedSignal |
|
|
|
|
93
|
|
|
from groundwork.signals import UnknownSignal |
94
|
|
|
|
95
|
|
|
def sig_reg_test(plugin, **kwargs): |
|
|
|
|
96
|
|
|
return ["data_1", "data_2"] |
97
|
|
|
|
98
|
|
|
plugin1 = EmptyPlugin(app=basicApp, name="EmptyPlugin") |
99
|
|
|
|
100
|
|
|
# This creates a gw receiver and a blinker internal signal called "test" |
101
|
|
|
plugin1.signals.connect("sig_reg_receiver", "sig_reg_test", sig_reg_test, "receiver sig_reg for test") |
|
|
|
|
102
|
|
|
|
103
|
|
|
# Check the blinker internal registration |
104
|
|
|
signal_namespace = basicApp.signals._namespace |
|
|
|
|
105
|
|
|
assert "sig_reg_test" in signal_namespace.keys() |
106
|
|
|
test_signal = signal_namespace["sig_reg_test"] |
107
|
|
|
assert isinstance(test_signal, NamedSignal) |
108
|
|
|
|
109
|
|
|
# Check the gw registration |
110
|
|
|
plugin_signals = plugin1.signals.get() |
111
|
|
|
# This must be 0, because gw has not registered a signal, this was done in blinker only |
112
|
|
|
assert len(plugin_signals) == 0 |
|
|
|
|
113
|
|
|
|
114
|
|
|
app_signals = basicApp.signals.get() |
115
|
|
|
assert "sig_reg_test" not in app_signals.keys() |
116
|
|
|
|
117
|
|
|
# Check if signal sending throws an error (without a registered gw signal) |
118
|
|
|
with pytest.raises(UnknownSignal): |
119
|
|
|
plugin1.signals.send("sig_reg_test") |
120
|
|
|
|
121
|
|
|
# Check if gw has a registered receiver |
122
|
|
|
plugin_receivers = plugin1.signals.get_receiver() |
123
|
|
|
assert "sig_reg_receiver" in plugin_receivers.keys() |
124
|
|
|
|
125
|
|
|
# Register the missing gw signal |
126
|
|
|
plugin_signal = plugin1.signals.register("sig_reg_test", "signal for sig_reg_test") |
127
|
|
|
assert plugin_signal is not None |
128
|
|
|
|
129
|
|
|
# Recheck the gw registration |
130
|
|
|
plugin_signals = plugin1.signals.get() |
131
|
|
|
# This must be 1, because gw has registered a signal now |
132
|
|
|
assert len(plugin_signals) == 1 |
133
|
|
|
|
134
|
|
|
# Sending should work now |
135
|
|
|
return_values = plugin1.signals.send("sig_reg_test") |
136
|
|
|
# Check if the receivers sends back needed data |
137
|
|
|
assert len(return_values) == 1 |
138
|
|
|
assert return_values[0][1][0] == "data_1" |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
def test_signal_handling_via_plugins(basicApp, EmptyPlugin): |
|
|
|
|
142
|
|
|
def sig_reg_test_1(plugin, **kwargs): |
|
|
|
|
143
|
|
|
return ["data_1", "data_2"] |
144
|
|
|
|
145
|
|
|
def sig_reg_test_2(plugin, **kwargs): |
|
|
|
|
146
|
|
|
return ["data_3", "data_4"] |
147
|
|
|
|
148
|
|
|
plugin_send = EmptyPlugin(app=basicApp, name="Plugin_send") |
149
|
|
|
|
150
|
|
|
plugin_receive_pre = EmptyPlugin(app=basicApp, name="Plugin_receive_pre") |
151
|
|
|
plugin_receive_post = EmptyPlugin(app=basicApp, name="Plugin_receive_post") |
152
|
|
|
|
153
|
|
|
plugin_receive_pre.activate() |
154
|
|
|
plugin_receive_pre.signals.connect("sig_reg_pre_receiver", "signal_test", |
155
|
|
|
sig_reg_test_1, "receiver signal_test for test") |
156
|
|
|
|
157
|
|
|
plugin_send.activate() |
158
|
|
|
plugin_send.signals.register("signal_test", "signal_test doc") |
159
|
|
|
|
160
|
|
|
return_values = plugin_send.signals.send("signal_test") |
161
|
|
|
assert len(return_values) == 1 |
162
|
|
|
|
163
|
|
|
plugin_receive_post.activate() |
164
|
|
|
plugin_receive_post.signals.connect("sig_reg_post_receiver", "signal_test", |
165
|
|
|
sig_reg_test_2, "receiver signal_test for test") |
166
|
|
|
|
167
|
|
|
return_values = plugin_send.signals.send("signal_test") |
168
|
|
|
assert len(return_values) == 2 |
169
|
|
|
|
170
|
|
|
# Send signal and check return vales |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
def test_multi_plugin_sending(basicApp, EmptyPlugin): |
|
|
|
|
174
|
|
|
""" |
175
|
|
|
Nearly some test as "test_signal_handling_via_plugins", but with an adjustable amount of plugins for sending |
|
|
|
|
176
|
|
|
and receiving. |
177
|
|
|
|
178
|
|
|
Registers receivers before the signal gets registers itself and afterwards. |
179
|
|
|
Checks if all receivers get correctly called, if signal is send. |
180
|
|
|
|
181
|
|
|
This tests will normally fail, if Weaknamespace is used in groundwork/signals.py: |
182
|
|
|
from blinker import WeakNamespace as Namespace |
183
|
|
|
|
184
|
|
|
So use always Namespace and if need do clean ups on groundwork site. |
185
|
|
|
""" |
186
|
|
|
|
187
|
|
|
amount_pre_plugins = 30 |
188
|
|
|
amount_post_plugins = 10 |
189
|
|
|
amount_send_plugins = 30 |
190
|
|
|
|
191
|
|
|
def create_receiver_function(start): |
|
|
|
|
192
|
|
|
|
193
|
|
|
def func_template(*args, **kwargs): |
|
|
|
|
194
|
|
|
return ["data_{0}_A".format(start), |
195
|
|
|
"data_{0}_B".format(start)] |
196
|
|
|
|
197
|
|
|
return func_template |
198
|
|
|
|
199
|
|
|
# PRE receivers |
200
|
|
|
plugin_receive_pre = [] |
201
|
|
|
for i in range(0, amount_pre_plugins): |
202
|
|
|
plugin_receive_pre.append(EmptyPlugin(app=basicApp, name="Plugin_receive_pre_{0}".format(i))) |
|
|
|
|
203
|
|
|
plugin_receive_pre[i].activate() |
204
|
|
|
plugin_receive_pre[i].signals.connect("sig_reg_pre_receiver_{0}".format(i), "signal_test", |
205
|
|
|
create_receiver_function(i), |
206
|
|
|
"receiver signal_test_{0} for test".format(i)) |
207
|
|
|
|
208
|
|
|
# Count gw internal registered receivers |
209
|
|
|
amount = 0 |
210
|
|
|
for receiver in basicApp.signals.receivers.keys(): |
211
|
|
|
if "sig_reg_pre_receiver" in receiver: |
212
|
|
|
amount += 1 |
213
|
|
|
assert amount == amount_pre_plugins |
214
|
|
|
|
215
|
|
|
# Senders |
216
|
|
|
plugin_send = [] |
217
|
|
|
for i in range(0, amount_send_plugins): |
218
|
|
|
plugin_send.append(EmptyPlugin(app=basicApp, name="Plugin_send_{0}".format(i))) |
219
|
|
|
plugin_send[i].activate() |
220
|
|
|
if i == 0: |
221
|
|
|
# We only need to register our signal once |
222
|
|
|
plugin_send[0].signals.register("signal_test", "signal_test") |
223
|
|
|
assert amount_pre_plugins == len(basicApp.signals.signals["signal_test"]._signal.receivers) |
|
|
|
|
224
|
|
|
|
225
|
|
|
# Check, if for our signal all receivers have been registered |
226
|
|
|
print("Registered receivers for signal_test: {0}".format( |
227
|
|
|
len(basicApp.signals.signals["signal_test"]._signal.receivers))) |
|
|
|
|
228
|
|
|
assert amount_pre_plugins == len(basicApp.signals.signals["signal_test"]._signal.receivers) |
|
|
|
|
229
|
|
|
|
230
|
|
|
# Send signal |
231
|
|
|
for index, plugin in enumerate(plugin_send): |
232
|
|
|
print(" {0} sending...".format(index)) |
233
|
|
|
return_values = plugin.signals.send("signal_test") |
234
|
|
|
|
235
|
|
|
# check return length and content |
236
|
|
|
assert len(return_values) == amount_pre_plugins |
237
|
|
|
for i in range(0, amount_pre_plugins): |
238
|
|
|
found = False |
239
|
|
|
for value in return_values: |
240
|
|
|
if value[1][0] == "data_{0}_A".format(i) and value[1][1] == "data_{0}_B".format(i): |
241
|
|
|
found = True |
242
|
|
|
break |
243
|
|
|
assert found is True |
244
|
|
|
|
245
|
|
|
# Register POST receivers |
246
|
|
|
plugin_receive_post = [] |
247
|
|
|
for i in range(0, amount_post_plugins): |
248
|
|
|
plugin_receive_post.append(EmptyPlugin(app=basicApp, name="Plugin_receive_post_{0}".format(i))) |
|
|
|
|
249
|
|
|
plugin_receive_post[i].activate() |
250
|
|
|
plugin_receive_post[i].signals.connect("sig_reg_post_receiver_{0}".format(i), "signal_test", |
251
|
|
|
create_receiver_function(amount_pre_plugins + i), |
252
|
|
|
"receiver signal_test_{0} for test".format(i)) |
253
|
|
|
|
254
|
|
|
# Send again a signal and check return values |
255
|
|
|
# Send signal |
256
|
|
|
for index, plugin in enumerate(plugin_send): |
257
|
|
|
print(" {0} sending again...".format(index)) |
258
|
|
|
return_values = plugin.signals.send("signal_test") |
259
|
|
|
|
260
|
|
|
# check return length and content |
261
|
|
|
assert len(return_values) == amount_pre_plugins + amount_post_plugins |
262
|
|
|
for i in range(0, amount_pre_plugins + amount_post_plugins): |
263
|
|
|
found = False |
264
|
|
|
for value in return_values: |
265
|
|
|
if value[1][0] == "data_{0}_A".format(i) and value[1][1] == "data_{0}_B".format(i): |
266
|
|
|
found = True |
267
|
|
|
break |
268
|
|
|
assert found is True |
269
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.