]> granicus.if.org Git - python/commitdiff
Refined rollover test for slow test machines.
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Fri, 6 May 2011 18:06:00 +0000 (19:06 +0100)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>
Fri, 6 May 2011 18:06:00 +0000 (19:06 +0100)
Lib/test/test_logging.py

index 919f0b2aa3287c8599a8706353442164140c4f07..aa70295aa9f0572e1e9495f44dd6ffe48da4207c 100644 (file)
@@ -3490,23 +3490,24 @@ class TimedRotatingFileHandlerTest(BaseFileTest):
             self.assertLogFile(self.fn)
             time.sleep(1.0)
             fh.emit(r)
+            # At this point, we should have a recent rotated file which we
+            # can test for the existence of. However, in practice, on some
+            # machines which run really slowly, we don't know how far back
+            # in time to go to look for the log file. So, we go back a fair
+            # bit, and stop as soon as we see a rotated file. In theory this
+            # could of course still fail, but the chances are lower.
+            found = False
             now = datetime.datetime.now()
-            prevsec = now - datetime.timedelta(seconds=1)
-            earlier = now - datetime.timedelta(seconds=2)
-            fn1 = self.fn + prevsec.strftime(".%Y-%m-%d_%H-%M-%S")
-            fn2 = self.fn + earlier.strftime(".%Y-%m-%d_%H-%M-%S")
-            success = os.path.exists(fn1) or os.path.exists(fn2)
-            if not success:
-                # print additional diagnostic information
-                print('Neither %s nor %s exists' % (fn1, fn2), file=sys.stderr)
-                dirname = os.path.dirname(fn1)
-                files = os.listdir(dirname)
-                files = [f for f in files if f.startswith('test_logging-2-')]
-                print('matching files: %s' % files, file=sys.stderr)
-            self.assertTrue(success)
-            for fn in (fn1, fn2):
-                if os.path.exists(fn):
+            GO_BACK = 2 * 60 # seconds
+            for secs in range(1, GO_BACK):
+                prev = now - datetime.timedelta(seconds=secs)
+                fn = self.fn + prev.strftime(".%Y-%m-%d_%H-%M-%S")
+                found = os.path.exists(fn)
+                if found:
                     self.rmfiles.append(fn)
+                    break
+            msg = 'No rotated files found, went back %d seconds' % GO_BACK
+            self.assertTrue(found, msg=msg)
         finally:
             fh.close()