From: Christian Herdtweck Date: Mon, 8 Nov 2021 14:53:58 +0000 (+0100) Subject: log_read: Deal with rare case of wrong fstat at test start X-Git-Tag: v1.6.7~2^2~6 X-Git-Url: http://developer.intra2net.com/git/?p=pyi2ncommon;a=commitdiff_plain;h=ea8b01a3089fee07b412b0a0dce34ab76d8ac48c log_read: Deal with rare case of wrong fstat at test start --- diff --git a/src/log_read.py b/src/log_read.py index c6b646e..52b8cec 100644 --- a/src/log_read.py +++ b/src/log_read.py @@ -225,9 +225,11 @@ class IterativeReader(object): category=LogReadWarning) self.ignore[idx] = True else: - if new_size < last_size: - warn('{0} / {1} has become smaller ({2} --> {3})!' - .format(obj, description, last_size, new_size), + if new_size < last_size: # happened at start of some tests + warn('{0} / {1} has become smaller ({2} --> {3})! ' + .format(obj, description, last_size, new_size) + + 'Maybe you are reading from a half-initialized ' + + 'file?', category=LogReadWarning) try: new_data = obj.read() diff --git a/test/test_log_read.py b/test/test_log_read.py index daf8ea6..7fd7fac 100644 --- a/test/test_log_read.py +++ b/test/test_log_read.py @@ -29,8 +29,9 @@ from tempfile import mkstemp import os import time import logging +from warnings import warn -from src.log_read import IterativeReader, LineReader +from src.log_read import IterativeReader, LineReader, LogReadWarning # get best clock from sys import version_info @@ -198,12 +199,22 @@ class LogReadTester(unittest.TestCase): with open(self.temp_file, 'rt') as file_handle: reader = IterativeReader(file_handle) self.helper_test_len(reader, 1) - for counter, (desc, text) in enumerate(reader): + counter = -1 # we may have to adapt this manually + for desc, text in reader: receive_time = perf_counter() + counter += 1 text = text.strip() if DEBUG: print('{1}: received text "{0}" at {2}' .format(text, counter, receive_time)) + if counter == 0 and not text: + # if reader runs stat() before we write, we might get + # a warning and one empty read here + counter -= 1 + warn('Got an empty read, you should have seen another ' + 'warning about file shrinking', + category=LogReadWarning) + continue index = text.index(':') count_text = int(text[:index].strip()) self.assertEqual(count_text, counter)