Skip to content

Commit

Permalink
Allow default and minimum timeouts to be specified via environment va…
Browse files Browse the repository at this point in the history
…riables.
  • Loading branch information
cjw296 committed Jun 29, 2021
1 parent 8dd74d1 commit 1bb8de7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
11 changes: 8 additions & 3 deletions carly/clock.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from __future__ import print_function

from os import environ

from twisted.internet import reactor
from twisted.internet.defer import Deferred, inlineCallbacks

DEFAULT_TIMEOUT = 0.2
DEFAULT_TIMEOUT = environ.get('CARLY_DEFAULT_TIMEOUT', 0.2)


def withTimeout(deferred, timeout=None):
if timeout is None:
timeout = DEFAULT_TIMEOUT
minimum = environ.get('CARLY_MINIMUM_TIMEOUT')
minimum = float(minimum) if minimum else None
timeout = timeout or minimum or DEFAULT_TIMEOUT
if minimum is not None:
timeout = max(timeout, minimum)
return deferred.addTimeout(timeout, reactor)


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "carly"
version = "0.10.2"
version = "0.11.0"
description = "A tool for putting messages into and collecting responses from Twisted servers using real networking"
authors = ["Chris Withers <chris@withers.org>"]
license = "MIT"
Expand Down
38 changes: 37 additions & 1 deletion tests/test_clock.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from testfixtures import ShouldRaise
from testfixtures import ShouldRaise, compare, Replace, not_there
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from twisted.trial.unittest import TestCase

from carly import cancelDelayedCalls, advanceTime
from carly.clock import withTimeout


def call1(x): pass
Expand Down Expand Up @@ -40,3 +41,38 @@ def testLetDeferredsScheduleTheirCalls(self):
reactor.callLater(0, fireLater, called)
yield advanceTime(seconds=6)
assert called


class MockDeferred(object):

def addTimeout(self, timeout, _):
self.timeout = timeout
return self


class TestWithTimeout(TestCase):

def testNormal(self):
with Replace('os.environ.CARLY_MINIMUM_TIMEOUT', not_there, strict=False):
actual = withTimeout(MockDeferred())
compare(actual.timeout, expected=0.2)

def testExplicit(self):
with Replace('os.environ.CARLY_MINIMUM_TIMEOUT', not_there, strict=False):
actual = withTimeout(MockDeferred(), timeout=42)
compare(actual.timeout, expected=42)

def testFromEnv(self):
with Replace('os.environ.CARLY_MINIMUM_TIMEOUT', '42', strict=False):
actual = withTimeout(MockDeferred())
compare(actual.timeout, expected=42)

def testExplicitLongerThanEnv(self):
with Replace('os.environ.CARLY_MINIMUM_TIMEOUT', '1', strict=False):
actual = withTimeout(MockDeferred(), timeout=2)
compare(actual.timeout, expected=2)

def testEnvLongerThanExplicit(self):
with Replace('os.environ.CARLY_MINIMUM_TIMEOUT', '2', strict=False):
actual = withTimeout(MockDeferred(), timeout=1)
compare(actual.timeout, expected=2)
2 changes: 1 addition & 1 deletion tests/test_threads.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from time import sleep

from testfixtures import compare, ShouldRaise
from testfixtures import compare, ShouldRaise, Replace
from twisted.internet.defer import inlineCallbacks, TimeoutError
from twisted.internet.task import LoopingCall
from twisted.internet.threads import deferToThread
Expand Down

0 comments on commit 1bb8de7

Please sign in to comment.