Ticket #2424: 2424-02-use-monotonic-clock.patch

File 2424-02-use-monotonic-clock.patch, 3.7 KB (added by ivank, 11 years ago)
  • new file doc/core/benchmarks/test_for_monotonic_clock.py

    diff --git a/doc/core/benchmarks/test_for_monotonic_clock.py b/doc/core/benchmarks/test_for_monotonic_clock.py
    new file mode 100644
    index 0000000..4cb63e2
    - +  
     1print '''\
     2After starting this program, adjust your system clock
     3forward. If Twisted is using monoclock.nano_count(),
     4you will see no interruption in the counter below.
     5'''
     6
     7from twisted.internet import reactor
     8
     9def start(n):
     10    print n
     11    reactor.callLater(1, start, n + 1)
     12
     13reactor.callWhenRunning(start, 0)
     14reactor.run()
  • twisted/protocols/sip.py

    diff --git a/twisted/protocols/sip.py b/twisted/protocols/sip.py
    index 07d10a9..8be723c 100644
    a b class InMemoryRegistry: 
    13001300            return defer.fail(LookupError("unknown domain"))
    13011301        if self.users.has_key(userURI.username):
    13021302            dc, url = self.users[userURI.username]
    1303             return defer.succeed(Registration(int(dc.getTime() - time.time()), url))
     1303            return defer.succeed(Registration(int(dc.getTime() - dc.seconds()), url))
    13041304        else:
    13051305            return defer.fail(LookupError("no such user"))
    13061306
    class InMemoryRegistry: 
    13281328            dc = reactor.callLater(3600, self._expireRegistration, logicalURL.username)
    13291329        log.msg("Registered %s at %s" % (logicalURL.toString(), physicalURL.toString()))
    13301330        self.users[logicalURL.username] = (dc, physicalURL)
    1331         return defer.succeed(Registration(int(dc.getTime() - time.time()), physicalURL))
     1331        return defer.succeed(Registration(int(dc.getTime() - dc.seconds()), physicalURL))
    13321332
    13331333    def unregisterAddress(self, domainURL, logicalURL, physicalURL):
    13341334        return self._expireRegistration(logicalURL.username)
  • twisted/python/runtime.py

    diff --git a/twisted/python/runtime.py b/twisted/python/runtime.py
    index 94958ca..251ab14 100644
    a b  
    33# See LICENSE for details.
    44
    55
    6 # System imports
    76import os
    87import sys
    98import time
    109import imp
    1110
     11from twisted.python import log
     12
     13# If a monotonic clock is available, use it.
     14try:
     15    from monoclock import nano_count
     16    _defaultTimeFunc = lambda: nano_count() / 1e9
     17except ImportError:
     18    log.msg('Danger: monotonic clock not available; '
     19        'DelayedCalls may be affected by system time jumps.')
     20    _defaultTimeFunc = time.time
     21
    1222
    1323def shortPythonVersion():
    1424    hv = sys.hexversion
    class Platform: 
    3444    """Gives us information about the platform we're running on"""
    3545
    3646    type = knownPlatforms.get(os.name)
    37     seconds = staticmethod(_timeFunctions.get(type, time.time))
     47    seconds = staticmethod(_timeFunctions.get(type, _defaultTimeFunc))
    3848
    3949    def __init__(self, name=None):
    4050        if name is not None:
    4151            self.type = knownPlatforms.get(name)
    42             self.seconds = _timeFunctions.get(self.type, time.time)
     52            self.seconds = _timeFunctions.get(self.type, _defaultTimeFunc)
    4353
    4454    def isKnown(self):
    4555        """Do we know about this platform?"""
  • twisted/test/test_internet.py

    diff --git a/twisted/test/test_internet.py b/twisted/test/test_internet.py
    index 8b4d6f3..0702b18 100644
    a b class TimeTestCase(unittest.TestCase): 
    783783    def testCallLaterTime(self):
    784784        d = reactor.callLater(10, lambda: None)
    785785        try:
    786             self.failUnless(d.getTime() - (time.time() + 10) < 1)
     786            callTime = d.getTime()
     787            timeNow = d.seconds()
     788            self.assertTrue(callTime >= 0, callTime)
     789            self.assertTrue(callTime - (timeNow + 10) < 1,
     790                dict(callTime=callTime, timeNow=timeNow))
    787791        finally:
    788792            d.cancel()
    789793