Commits

Marcin Kasperski committed 1207539

Additional deferred utils.

  • Participants
  • Parent commits 9e770b3

Comments (0)

Files changed (1)

src/mekk/fics/twisted_util/defer_util.py

 # -*- coding: utf-8 -*-
 
 """
-Additional support for async communication.
+Additional support for deferred handling.
 """
 
+from twisted.internet import defer, reactor
+
+def delay_succeed(reply, delay=0.01):
+    """
+    Similar to defer.succeed(reply), but introduces some minimal
+    delay before replying.
+
+    This is most useful in tests, where it allows one to simulate
+    mocked replies as truly async - but can also be used to force
+    delays in processing.
+    """
+    d = defer.Deferred()
+    reactor.callLater(delay, d.callback, reply)
+    return d
+
+def delay_exception(exception, delay=0.01):
+    """
+    Provides deferred which will fail after given delay with given
+    exception.
+
+    This is most useful in tests, where it allows one to simulate
+    mocked replies as truly async - but can also be used to force
+    delays in processing.
+    """
+    d = defer.Deferred()
+    reactor.callLater(delay, d.errback, exception)
+    return d
+
 class CancellingDeferredList(defer.DeferredList):
     """
     Update to normal DeferredList: any failure causes all unfinished