Ticket #1875: deferlater.diff

File deferlater.diff, 3.2 KB (added by Tristan Seligmann, 16 years ago)
  • twisted/test/test_task.py

     
    163163        L = []
    164164        lc = TestableLoopingCall(clock, L.append, None)
    165165        d = lc.start(0.1, now=False)
    166        
     166
    167167        theResult = []
    168168        def saveResult(result):
    169169            theResult.append(result)
     
    298298        clock.pump(timings)
    299299        self.failIf(clock.calls)
    300300        return d
     301
     302
     303class _TestableDeferredScheduler(task._DeferredScheduler):
     304    """
     305    I am a L{task._DeferredScheduler} using a dummy L{task.Clock}, for testing
     306    purposes.
     307    """
     308    def __init__(self):
     309        self.clock = task.Clock()
     310
     311    def _callLater(self, *a, **kw):
     312        return self.clock.callLater(*a, **kw)
     313
     314
     315
     316class DeferLaterTestCase(unittest.TestCase):
     317    def testCallback(self):
     318        """
     319        Test that the callback is actually invoked, and with the correct
     320        arguments.
     321        """
     322        deferLater = _TestableDeferredScheduler()
     323
     324        flag = object()
     325        def _cb(foo, bar):
     326            self.assertEquals(foo, 'foo')
     327            self.assertEquals(bar, 'bar')
     328            return flag
     329
     330        d = deferLater(1, _cb, 'foo', bar='bar')
     331        d.addCallback(self.assertEquals, flag)
     332        deferLater.clock.pump([2])
     333        return d
     334
     335    def testErrback(self):
     336        """
     337        Test that an exception raised in the callback is translated to a
     338        Failure.
     339        """
     340        deferLater = _TestableDeferredScheduler()
     341
     342        def _cb():
     343            raise TestException()
     344
     345        d = deferLater(1, _cb)
     346        deferLater.clock.pump([2])
     347        return self.assertFailure(d, TestException)
  • twisted/internet/task.py

     
    357357            self.advance(amount)
    358358
    359359
     360class _DeferredScheduler(object):
     361    """
     362    I wrap a Deferred around L{IReactorTime.callLater}. Invoke me as
     363    L{twisted.internet.task.deferLater}.
     364    """
    360365
     366    def _callLater(self, *a, **kw):
     367        from twisted.internet import reactor
     368        return reactor.callLater(*a, **kw)
     369
     370    def __call__(self, delay, callable, *args, **kw):
     371        """
     372        Schedule a function to be run after a certain period of time has
     373        passed.
     374
     375        @type delay: C{float}
     376        @param delay: the number of seconds to wait.
     377
     378        @param callable: the callable object to call later.
     379
     380        @param args: the arguments to call it with.
     381
     382        @param kw: the keyword arguments to call it with.
     383
     384        @return: A deferred that fires when the specified time has elapsed, and
     385                 has had the provided callback added to its chain.
     386        """
     387        d = defer.Deferred()
     388        d.addCallback(lambda dummy: callable(*args, **kw))
     389        self._callLater(delay, lambda: d.callback(None))
     390        return d
     391
     392
     393
     394deferLater = _DeferredScheduler()
     395
     396
     397
    361398__all__ = [
    362399    'LoopingCall',
    363400
    364401    'Clock',
    365402
    366403    'SchedulerStopped', 'Cooperator', 'coiterate',
     404
     405    'deferLater',
    367406    ]