From 7d8fb10eb1381a9b3f9d7f4ba94b95ef095095ad Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Sat, 11 Aug 2018 12:14:07 -0400 Subject: [PATCH] test: Deflake autoreload_test on windows This sometimes fails with a "file in use" error while cleaning up the temp directory. Add a sleep and retry in case we've reached our teardown before the child process has released its locks. --- tornado/test/autoreload_test.py | 34 +++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/tornado/test/autoreload_test.py b/tornado/test/autoreload_test.py index 6c62b3308..1c8ffbb88 100644 --- a/tornado/test/autoreload_test.py +++ b/tornado/test/autoreload_test.py @@ -9,6 +9,20 @@ import unittest class AutoreloadTest(unittest.TestCase): + def setUp(self): + self.path = mkdtemp() + + def tearDown(self): + try: + shutil.rmtree(self.path) + except OSError: + # Windows disallows deleting files that are in use by + # another process, and even though we've waited for our + # child process below, it appears that its lock on these + # files is not guaranteed to be released by this point. + # Sleep and try again (once). + time.sleep(1) + shutil.rmtree(self.path) def test_reload_module(self): main = """\ @@ -28,11 +42,9 @@ if 'TESTAPP_STARTED' not in os.environ: """ # Create temporary test application - path = mkdtemp() - self.addCleanup(shutil.rmtree, path) - os.mkdir(os.path.join(path, 'testapp')) - open(os.path.join(path, 'testapp/__init__.py'), 'w').close() - with open(os.path.join(path, 'testapp/__main__.py'), 'w') as f: + os.mkdir(os.path.join(self.path, 'testapp')) + open(os.path.join(self.path, 'testapp/__init__.py'), 'w').close() + with open(os.path.join(self.path, 'testapp/__main__.py'), 'w') as f: f.write(main) # Make sure the tornado module under test is available to the test @@ -43,7 +55,7 @@ if 'TESTAPP_STARTED' not in os.environ: p = Popen( [sys.executable, '-m', 'testapp'], stdout=subprocess.PIPE, - cwd=path, env=dict(os.environ, PYTHONPATH=pythonpath), + cwd=self.path, env=dict(os.environ, PYTHONPATH=pythonpath), universal_newlines=True) out = p.communicate()[0] self.assertEqual(out, 'Starting\nStarting\n') @@ -77,12 +89,10 @@ else: """ # Create temporary test application - path = mkdtemp() - os.mkdir(os.path.join(path, 'testapp')) - self.addCleanup(shutil.rmtree, path) - init_file = os.path.join(path, 'testapp', '__init__.py') + os.mkdir(os.path.join(self.path, 'testapp')) + init_file = os.path.join(self.path, 'testapp', '__init__.py') open(init_file, 'w').close() - main_file = os.path.join(path, 'testapp', '__main__.py') + main_file = os.path.join(self.path, 'testapp', '__main__.py') with open(main_file, 'w') as f: f.write(main) @@ -94,7 +104,7 @@ else: autoreload_proc = Popen( [sys.executable, '-m', 'tornado.autoreload', '-m', 'testapp'], - stdout=subprocess.PIPE, cwd=path, + stdout=subprocess.PIPE, cwd=self.path, env=dict(os.environ, PYTHONPATH=pythonpath), universal_newlines=True) -- 2.47.2