@@ -182,31 +182,45 @@ def setup(self):
182182 self .signal = 'test'
183183 self .callbacks = cbook .CallbackRegistry ()
184184
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
187199
188200 def is_empty (self ):
189201 assert self .callbacks ._func_cid_map == {}
190202 assert self .callbacks .callbacks == {}
203+ assert self .callbacks ._pickled_cids == set ()
191204
192205 def is_not_empty (self ):
193206 assert self .callbacks ._func_cid_map != {}
194207 assert self .callbacks .callbacks != {}
195208
196- def test_callback_complete (self ):
209+ @pytest .mark .parametrize ('pickle' , [True , False ])
210+ def test_callback_complete (self , pickle ):
197211 # ensure we start with an empty registry
198212 self .is_empty ()
199213
200214 # create a class for testing
201215 mini_me = Test_callback_registry ()
202216
203217 # 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 )
205219 assert type (cid1 ) == int
206220 self .is_not_empty ()
207221
208222 # 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 )
210224 assert cid1 == cid2
211225 self .is_not_empty ()
212226 assert len (self .callbacks ._func_cid_map ) == 1
@@ -217,6 +231,68 @@ def test_callback_complete(self):
217231 # check we now have no callbacks registered
218232 self .is_empty ()
219233
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+
220296 def dummy (self ):
221297 pass
222298
0 commit comments