ENH: deprecate sophisticated MTimeSleep in favor of no sleeping at all

all invocations of mtimesleep() are left in the tests for now
pull/280/head
Yaroslav Halchenko 2013-07-02 20:44:28 -04:00
parent f340e5c0f5
commit 052e7ff9da
1 changed files with 4 additions and 53 deletions

View File

@ -101,56 +101,7 @@ class FormatterWithTraceBack(logging.Formatter):
record.tbc = record.tb = self._tb()
return logging.Formatter.format(self, record)
class MTimeSleep(object):
"""Sleep minimal duration needed to resolve changes in mtime of files in TMPDIR
mtime resolution depends on Python version AND underlying filesystem
"""
def __init__(self):
self._sleep = None
@staticmethod
def _get_good_sleep():
logSys = logging.getLogger("fail2ban.tests")
times = [1.5, 2., 5., 10.]
# we know that older Pythons simply have no ability to resolve
# at < sec level.
if sys.version_info[:2] > (2, 4):
times = [0.1] + times
ffid, name = tempfile.mkstemp()
tfile = os.fdopen(ffid, 'w')
for stime in times:
prev_stat, dt = "", 0.
# needs to be done 3 times (not clear why)
for i in xrange(3):
stat2 = os.stat(name)
if prev_stat:
dt = (stat2.st_mtime - prev_stat.st_mtime)
prev_stat = stat2
tfile.write("LOAD\n")
tfile.flush()
time.sleep(stime)
# check dt but also verify that we are not getting 'quick'
# stime simply by chance of catching second increment
if dt and \
not (stime < 1 and int(stat2.st_mtime) == stat2.st_mtime):
break
if not dt:
#from warnings import warn
logSys.warn("Could not deduce appropriate sleep time for tests. "
"Maximal tested one of %f sec will be used." % stime)
else:
logSys.debug("It was needed a sleep of %f to detect dt=%f mtime change"
% (stime, dt))
os.unlink(name)
return stime
def __call__(self):
if self._sleep is None:
self._sleep = self._get_good_sleep()
time.sleep(self._sleep)
mtimesleep = MTimeSleep()
def mtimesleep():
# no sleep now should be necessary since polling tracks now not only
# mtime but also ino and size
pass