Skip to content

Commit

Permalink
Add support for condition timeout message
Browse files Browse the repository at this point in the history
  • Loading branch information
deathaxe committed Jun 2, 2024
1 parent 190d642 commit e56d95f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,24 @@ the following
the runner continues the generator, if not, the runner will wait until the
condition is met with the default timeout of 4s. The result of the callable
can be also retrieved from the `yield` statement. The yielded object could
be also a dictionary of the form `{"condition": callable, timeout: timeout}`
to specify timeout in ms.
also be a dictionary of the form

```py
{
# required condition callable
"condition": callable,
# system timestamp when to start condition checks (default: `time.time()`)
"start_time": timestamp,
# optional the interval to invoke `condition()` (default: 17)
"period": milliseconds,
# optional timeout to wait for condition to be met (default: value from unittesting.json or 4000)
"timeout": milliseconds,
# optional message to print, if condition is not met within timeout
"timeout_message": "Condition not fulfilled"
}
```

to specify various overrides such as poll interval or timeout in ms.

- if the yielded object is an integer, say `x`, then it will [continue][4] the
generator after `x` ms.
Expand All @@ -397,7 +413,7 @@ from unittesting import DeferrableTestCase

class TestCondition(DeferrableTestCase):

def test_condition(self):
def test_condition1(self):
x = []

def append():
Expand All @@ -411,6 +427,27 @@ class TestCondition(DeferrableTestCase):
# wait until `condition()` is true
yield condition

self.assertEqual(x[0], 1)

def test_condition2(self):
x = []

def append():
x.append(1)

def condition():
return len(x) == 1

sublime.set_timeout(append, 100)

# wait until `condition()` is true
yield {
"condition": condition,
"period": 200,
"timeout": 5000,
"timeout_message": "Not enough items added to x"
}

self.assertEqual(x[0], 1)
```

Expand Down
6 changes: 5 additions & 1 deletion unittesting/core/py33/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def _wait_condition(
period=DEFAULT_CONDITION_POLL_TIME,
timeout=self.condition_timeout,
start_time=None,
timeout_message=None
):
if start_time is None:
start_time = time.time()
Expand All @@ -121,8 +122,11 @@ def _wait_condition(
if send_value:
_continue_testing(deferred, send_value=send_value)
elif (time.time() - start_time) * 1000 >= timeout:
if timeout_message is None:
timeout_message = "Condition not fulfilled"
error = TimeoutError(
"Condition not fulfilled within {:.2f} seconds".format(
"{} within {:.2f} seconds".format(
timeout_message,
timeout / 1000
)
)
Expand Down
6 changes: 5 additions & 1 deletion unittesting/core/py38/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def _wait_condition(
period=DEFAULT_CONDITION_POLL_TIME,
timeout=self.condition_timeout,
start_time=None,
timeout_message=None
):
if start_time is None:
start_time = time.time()
Expand All @@ -130,8 +131,11 @@ def _wait_condition(
if send_value:
_continue_testing(deferred, send_value=send_value)
elif (time.time() - start_time) * 1000 >= timeout:
if timeout_message is None:
timeout_message = "Condition not fulfilled"
error = TimeoutError(
"Condition not fulfilled within {:.2f} seconds".format(
"{} within {:.2f} seconds".format(
timeout_message,
timeout / 1000
)
)
Expand Down

0 comments on commit e56d95f

Please sign in to comment.