Commit f8326fca authored by Andrea Spadaccini's avatar Andrea Spadaccini
Browse files

Move _TimeoutExpired to utils


Signed-off-by: default avatarAndrea Spadaccini <spadaccio@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent b8a2c0ab
......@@ -130,13 +130,6 @@ class ImportExportCbBase(object):
"""
def _TimeoutExpired(epoch, timeout, _time_fn=time.time):
"""Checks whether a timeout has expired.
"""
return _time_fn() > (epoch + timeout)
class _DiskImportExportBase(object):
MODE_TEXT = None
......@@ -319,7 +312,7 @@ class _DiskImportExportBase(object):
assert self._ts_begin is not None
if not data:
if _TimeoutExpired(self._ts_begin, self._timeouts.ready):
if utils.TimeoutExpired(self._ts_begin, self._timeouts.ready):
raise _ImportExportError("Didn't become ready after %s seconds" %
self._timeouts.ready)
......@@ -342,7 +335,7 @@ class _DiskImportExportBase(object):
if self._ts_last_error is None:
self._ts_last_error = time.time()
elif _TimeoutExpired(self._ts_last_error, self._timeouts.error):
elif utils.TimeoutExpired(self._ts_last_error, self._timeouts.error):
raise _ImportExportError("Too many errors while updating data")
return False
......@@ -386,7 +379,8 @@ class _DiskImportExportBase(object):
return True
if _TimeoutExpired(self._GetConnectedCheckEpoch(), self._timeouts.connect):
if utils.TimeoutExpired(self._GetConnectedCheckEpoch(),
self._timeouts.connect):
raise _ImportExportError("Not connected after %s seconds" %
self._timeouts.connect)
......@@ -397,7 +391,8 @@ class _DiskImportExportBase(object):
"""
if ((self._ts_last_progress is None or
_TimeoutExpired(self._ts_last_progress, self._timeouts.progress)) and
utils.TimeoutExpired(self._ts_last_progress,
self._timeouts.progress)) and
self._daemon and
self._daemon.progress_mbytes is not None and
self._daemon.progress_throughput is not None):
......@@ -560,7 +555,7 @@ class DiskImport(_DiskImportExportBase):
return True
if _TimeoutExpired(self._ts_begin, self._timeouts.listen):
if utils.TimeoutExpired(self._ts_begin, self._timeouts.listen):
raise _ImportExportError("Not listening after %s seconds" %
self._timeouts.listen)
......
......@@ -32,6 +32,7 @@ import os
import re
import errno
import pwd
import time
import itertools
import select
import logging
......@@ -592,6 +593,13 @@ def SignalHandled(signums):
return wrap
def TimeoutExpired(epoch, timeout, _time_fn=time.time):
"""Checks whether a timeout has expired.
"""
return _time_fn() > (epoch + timeout)
class SignalWakeupFd(object):
try:
# This is only supported in Python 2.5 and above (some distributions
......
......@@ -31,7 +31,7 @@ from ganeti import utils
from ganeti import masterd
from ganeti.masterd.instance import \
ImportExportTimeouts, _TimeoutExpired, _DiskImportExportBase, \
ImportExportTimeouts, _DiskImportExportBase, \
ComputeRemoteExportHandshake, CheckRemoteExportHandshake, \
ComputeRemoteImportDiskInfo, CheckRemoteExportDiskInfo, \
FormatProgress
......@@ -60,10 +60,10 @@ class TestMisc(unittest.TestCase):
self.assertEqual(tmo.progress, 5)
def testTimeoutExpired(self):
self.assert_(_TimeoutExpired(100, 300, _time_fn=lambda: 500))
self.assertFalse(_TimeoutExpired(100, 300, _time_fn=lambda: 0))
self.assertFalse(_TimeoutExpired(100, 300, _time_fn=lambda: 100))
self.assertFalse(_TimeoutExpired(100, 300, _time_fn=lambda: 400))
self.assert_(utils.TimeoutExpired(100, 300, _time_fn=lambda: 500))
self.assertFalse(utils.TimeoutExpired(100, 300, _time_fn=lambda: 0))
self.assertFalse(utils.TimeoutExpired(100, 300, _time_fn=lambda: 100))
self.assertFalse(utils.TimeoutExpired(100, 300, _time_fn=lambda: 400))
def testDiskImportExportBaseDirect(self):
self.assertRaises(AssertionError, _DiskImportExportBase,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment