]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
test: Deflake autoreload_test on windows 2467/head
authorBen Darnell <ben@bendarnell.com>
Sat, 11 Aug 2018 16:14:07 +0000 (12:14 -0400)
committerBen Darnell <ben@bendarnell.com>
Sat, 11 Aug 2018 16:14:07 +0000 (12:14 -0400)
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

index 6c62b3308af486d821394996f50241560b611c28..1c8ffbb88fdd56f6fdf556064eb7133df896f799 100644 (file)
@@ -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)