@@ -37,6 +37,16 @@ def wait(self):
3737class KazooLockTests (KazooTestCase ):
3838 thread_count = 20
3939
40+ def __init__ (self , * args , ** kw ):
41+ super (KazooLockTests , self ).__init__ (* args , ** kw )
42+ self .threads_made = []
43+
44+ def tearDown (self ):
45+ super (KazooLockTests , self ).tearDown ()
46+ while self .threads_made :
47+ t = self .threads_made .pop ()
48+ t .join ()
49+
4050 @staticmethod
4151 def make_condition ():
4252 return threading .Condition ()
@@ -45,9 +55,11 @@ def make_condition():
4555 def make_event ():
4656 return threading .Event ()
4757
48- @staticmethod
49- def make_thread (* args , ** kwargs ):
50- return threading .Thread (* args , ** kwargs )
58+ def make_thread (self , * args , ** kwargs ):
59+ t = threading .Thread (* args , ** kwargs )
60+ t .daemon = True
61+ self .threads_made .append (t )
62+ return t
5163
5264 @staticmethod
5365 def make_wait ():
@@ -389,6 +401,16 @@ def _thread(lock, event, timeout):
389401
390402class TestSemaphore (KazooTestCase ):
391403
404+ def __init__ (self , * args , ** kw ):
405+ super (TestSemaphore , self ).__init__ (* args , ** kw )
406+ self .threads_made = []
407+
408+ def tearDown (self ):
409+ super (TestSemaphore , self ).tearDown ()
410+ while self .threads_made :
411+ t = self .threads_made .pop ()
412+ t .join ()
413+
392414 @staticmethod
393415 def make_condition ():
394416 return threading .Condition ()
@@ -397,9 +419,11 @@ def make_condition():
397419 def make_event ():
398420 return threading .Event ()
399421
400- @staticmethod
401- def make_thread (* args , ** kwargs ):
402- return threading .Thread (* args , ** kwargs )
422+ def make_thread (self , * args , ** kwargs ):
423+ t = threading .Thread (* args , ** kwargs )
424+ t .daemon = True
425+ self .threads_made .append (t )
426+ return t
403427
404428 def setUp (self ):
405429 super (TestSemaphore , self ).setUp ()
@@ -539,8 +563,8 @@ def sema_one():
539563 event .set ()
540564 event2 .wait ()
541565
542- thread = self .make_thread (target = sema_one , args = ())
543- thread .start ()
566+ thread1 = self .make_thread (target = sema_one , args = ())
567+ thread1 .start ()
544568
545569 started .wait ()
546570 eq_ (lh_semaphore .lease_holders (), ['george' ])
@@ -552,8 +576,8 @@ def expire():
552576 self .expire_session (self .make_event )
553577 expired .set ()
554578
555- thread = self .make_thread (target = expire , args = ())
556- thread .start ()
579+ thread2 = self .make_thread (target = expire , args = ())
580+ thread2 .start ()
557581 expire_semaphore .wake_event .wait ()
558582 expired .wait ()
559583
@@ -563,7 +587,9 @@ def expire():
563587 event .wait (15 )
564588 eq_ (expire_semaphore .lease_holders (), ['fred' ])
565589 event2 .set ()
566- thread .join ()
590+
591+ for t in (thread1 , thread2 ):
592+ t .join ()
567593
568594 def test_inconsistent_max_leases (self ):
569595 sem1 = self .client .Semaphore (self .lockpath , max_leases = 1 )
0 commit comments