@@ -182,31 +182,45 @@ def setup(self):
182
182
self .signal = 'test'
183
183
self .callbacks = cbook .CallbackRegistry ()
184
184
185
- def connect (self , s , func ):
186
- return self .callbacks .connect (s , func )
185
+ def connect (self , s , func , pickle ):
186
+ cid = self .callbacks .connect (s , func )
187
+ if pickle :
188
+ self .callbacks ._pickled_cids .add (cid )
189
+ return cid
190
+
191
+ def disconnect (self , cid ):
192
+ return self .callbacks .disconnect (cid )
193
+
194
+ def count (self ):
195
+ count1 = len (self .callbacks ._func_cid_map .get (self .signal , []))
196
+ count2 = len (self .callbacks .callbacks .get (self .signal ))
197
+ assert count1 == count2
198
+ return count1
187
199
188
200
def is_empty (self ):
189
201
assert self .callbacks ._func_cid_map == {}
190
202
assert self .callbacks .callbacks == {}
203
+ assert self .callbacks ._pickled_cids == set ()
191
204
192
205
def is_not_empty (self ):
193
206
assert self .callbacks ._func_cid_map != {}
194
207
assert self .callbacks .callbacks != {}
195
208
196
- def test_callback_complete (self ):
209
+ @pytest .mark .parametrize ('pickle' , [True , False ])
210
+ def test_callback_complete (self , pickle ):
197
211
# ensure we start with an empty registry
198
212
self .is_empty ()
199
213
200
214
# create a class for testing
201
215
mini_me = Test_callback_registry ()
202
216
203
217
# test that we can add a callback
204
- cid1 = self .connect (self .signal , mini_me .dummy )
218
+ cid1 = self .connect (self .signal , mini_me .dummy , pickle )
205
219
assert type (cid1 ) == int
206
220
self .is_not_empty ()
207
221
208
222
# test that we don't add a second callback
209
- cid2 = self .connect (self .signal , mini_me .dummy )
223
+ cid2 = self .connect (self .signal , mini_me .dummy , pickle )
210
224
assert cid1 == cid2
211
225
self .is_not_empty ()
212
226
assert len (self .callbacks ._func_cid_map ) == 1
@@ -217,6 +231,68 @@ def test_callback_complete(self):
217
231
# check we now have no callbacks registered
218
232
self .is_empty ()
219
233
234
+ @pytest .mark .parametrize ('pickle' , [True , False ])
235
+ def test_callback_disconnect (self , pickle ):
236
+ # ensure we start with an empty registry
237
+ self .is_empty ()
238
+
239
+ # create a class for testing
240
+ mini_me = Test_callback_registry ()
241
+
242
+ # test that we can add a callback
243
+ cid1 = self .connect (self .signal , mini_me .dummy , pickle )
244
+ assert type (cid1 ) == int
245
+ self .is_not_empty ()
246
+
247
+ self .disconnect (cid1 )
248
+
249
+ # check we now have no callbacks registered
250
+ self .is_empty ()
251
+
252
+ @pytest .mark .parametrize ('pickle' , [True , False ])
253
+ def test_callback_wrong_disconnect (self , pickle ):
254
+ # ensure we start with an empty registry
255
+ self .is_empty ()
256
+
257
+ # create a class for testing
258
+ mini_me = Test_callback_registry ()
259
+
260
+ # test that we can add a callback
261
+ cid1 = self .connect (self .signal , mini_me .dummy , pickle )
262
+ assert type (cid1 ) == int
263
+ self .is_not_empty ()
264
+
265
+ self .disconnect ("foo" )
266
+
267
+ # check we still have callbacks registered
268
+ self .is_not_empty ()
269
+
270
+ @pytest .mark .parametrize ('pickle' , [True , False ])
271
+ def test_registration_on_non_empty_registry (self , pickle ):
272
+ # ensure we start with an empty registry
273
+ self .is_empty ()
274
+
275
+ # setup the registry with a callback
276
+ mini_me = Test_callback_registry ()
277
+ self .connect (self .signal , mini_me .dummy , pickle )
278
+
279
+ # Add another callback
280
+ mini_me2 = Test_callback_registry ()
281
+ self .connect (self .signal , mini_me2 .dummy , pickle )
282
+
283
+ # Remove and add the second callback
284
+ mini_me2 = Test_callback_registry ()
285
+ self .connect (self .signal , mini_me2 .dummy , pickle )
286
+
287
+ # We still have 2 references
288
+ self .is_not_empty ()
289
+ assert self .count () == 2
290
+
291
+ # Removing the last 2 references
292
+ mini_me = None
293
+ mini_me2 = None
294
+ self .is_empty ()
295
+
220
296
def dummy (self ):
221
297
pass
222
298
0 commit comments