@@ -359,48 +359,52 @@ def test_it_should_dashify(self):
359359 self .assertEqual ("foo" , dashify ("foo" ))
360360
361361 def test_lock_file (self ):
362- my_file = tempfile .mktemp ()
363- lock_file = LockFile (my_file )
364- assert not lock_file ._has_lock ()
365- # Release lock we don't have - fine.
366- lock_file ._release_lock ()
362+ with tempfile .TemporaryDirectory () as tdir :
363+ my_file = os .path .join (tdir , "my-lock-file" )
364+ lock_file = LockFile (my_file )
365+ assert not lock_file ._has_lock ()
366+ # Release lock we don't have - fine.
367+ lock_file ._release_lock ()
367368
368- # Get lock.
369- lock_file ._obtain_lock_or_raise ()
370- assert lock_file ._has_lock ()
369+ # Get lock.
370+ lock_file ._obtain_lock_or_raise ()
371+ assert lock_file ._has_lock ()
371372
372- # Concurrent access.
373- other_lock_file = LockFile (my_file )
374- assert not other_lock_file ._has_lock ()
375- self .assertRaises (IOError , other_lock_file ._obtain_lock_or_raise )
373+ # Concurrent access.
374+ other_lock_file = LockFile (my_file )
375+ assert not other_lock_file ._has_lock ()
376+ self .assertRaises (IOError , other_lock_file ._obtain_lock_or_raise )
376377
377- lock_file ._release_lock ()
378- assert not lock_file ._has_lock ()
378+ lock_file ._release_lock ()
379+ assert not lock_file ._has_lock ()
379380
380- other_lock_file ._obtain_lock_or_raise ()
381- self .assertRaises (IOError , lock_file ._obtain_lock_or_raise )
381+ other_lock_file ._obtain_lock_or_raise ()
382+ self .assertRaises (IOError , lock_file ._obtain_lock_or_raise )
382383
383- # Auto-release on destruction.
384- del other_lock_file
385- lock_file ._obtain_lock_or_raise ()
386- lock_file ._release_lock ()
384+ # Auto-release on destruction.
385+ del other_lock_file
386+ lock_file ._obtain_lock_or_raise ()
387+ lock_file ._release_lock ()
387388
388389 def test_blocking_lock_file (self ):
389- my_file = tempfile .mktemp ()
390- lock_file = BlockingLockFile (my_file )
391- lock_file ._obtain_lock ()
392-
393- # Next one waits for the lock.
394- start = time .time ()
395- wait_time = 0.1
396- wait_lock = BlockingLockFile (my_file , 0.05 , wait_time )
397- self .assertRaises (IOError , wait_lock ._obtain_lock )
398- elapsed = time .time () - start
390+ with tempfile .TemporaryDirectory () as tdir :
391+ my_file = os .path .join (tdir , "my-lock-file" )
392+ lock_file = BlockingLockFile (my_file )
393+ lock_file ._obtain_lock ()
394+
395+ # Next one waits for the lock.
396+ start = time .time ()
397+ wait_time = 0.1
398+ wait_lock = BlockingLockFile (my_file , 0.05 , wait_time )
399+ self .assertRaises (IOError , wait_lock ._obtain_lock )
400+ elapsed = time .time () - start
401+
399402 extra_time = 0.02
400403 if os .name == "nt" or sys .platform == "cygwin" :
401404 extra_time *= 6 # Without this, we get indeterministic failures on Windows.
402405 elif sys .platform == "darwin" :
403406 extra_time *= 18 # The situation on macOS is similar, but with more delay.
407+
404408 self .assertLess (elapsed , wait_time + extra_time )
405409
406410 def test_user_id (self ):
0 commit comments