Skip to content

Commit a2a776a

Browse files
authored
Add support for condition timeout message (#274)
This commit enables custom error messages, when deferred conditions timeout. ```py yield {"condition": lambda: False, "timeout_message": "False is not True"} ```
1 parent 190d642 commit a2a776a

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

README.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,24 @@ the following
378378
the runner continues the generator, if not, the runner will wait until the
379379
condition is met with the default timeout of 4s. The result of the callable
380380
can be also retrieved from the `yield` statement. The yielded object could
381-
be also a dictionary of the form `{"condition": callable, timeout: timeout}`
382-
to specify timeout in ms.
381+
also be a dictionary of the form
382+
383+
```py
384+
{
385+
# required condition callable
386+
"condition": callable,
387+
# system timestamp when to start condition checks (default: `time.time()`)
388+
"start_time": timestamp,
389+
# optional the interval to invoke `condition()` (default: 17)
390+
"period": milliseconds,
391+
# optional timeout to wait for condition to be met (default: value from unittesting.json or 4000)
392+
"timeout": milliseconds,
393+
# optional message to print, if condition is not met within timeout
394+
"timeout_message": "Condition not fulfilled"
395+
}
396+
```
397+
398+
to specify various overrides such as poll interval or timeout in ms.
383399

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

398414
class TestCondition(DeferrableTestCase):
399415

400-
def test_condition(self):
416+
def test_condition1(self):
401417
x = []
402418

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

430+
self.assertEqual(x[0], 1)
431+
432+
def test_condition2(self):
433+
x = []
434+
435+
def append():
436+
x.append(1)
437+
438+
def condition():
439+
return len(x) == 1
440+
441+
sublime.set_timeout(append, 100)
442+
443+
# wait until `condition()` is true
444+
yield {
445+
"condition": condition,
446+
"period": 200,
447+
"timeout": 5000,
448+
"timeout_message": "Not enough items added to x"
449+
}
450+
414451
self.assertEqual(x[0], 1)
415452
```
416453

unittesting/core/py33/runner.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def _wait_condition(
108108
period=DEFAULT_CONDITION_POLL_TIME,
109109
timeout=self.condition_timeout,
110110
start_time=None,
111+
timeout_message=None
111112
):
112113
if start_time is None:
113114
start_time = time.time()
@@ -121,8 +122,11 @@ def _wait_condition(
121122
if send_value:
122123
_continue_testing(deferred, send_value=send_value)
123124
elif (time.time() - start_time) * 1000 >= timeout:
125+
if timeout_message is None:
126+
timeout_message = "Condition not fulfilled"
124127
error = TimeoutError(
125-
"Condition not fulfilled within {:.2f} seconds".format(
128+
"{} within {:.2f} seconds".format(
129+
timeout_message,
126130
timeout / 1000
127131
)
128132
)

unittesting/core/py38/runner.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def _wait_condition(
117117
period=DEFAULT_CONDITION_POLL_TIME,
118118
timeout=self.condition_timeout,
119119
start_time=None,
120+
timeout_message=None
120121
):
121122
if start_time is None:
122123
start_time = time.time()
@@ -130,8 +131,11 @@ def _wait_condition(
130131
if send_value:
131132
_continue_testing(deferred, send_value=send_value)
132133
elif (time.time() - start_time) * 1000 >= timeout:
134+
if timeout_message is None:
135+
timeout_message = "Condition not fulfilled"
133136
error = TimeoutError(
134-
"Condition not fulfilled within {:.2f} seconds".format(
137+
"{} within {:.2f} seconds".format(
138+
timeout_message,
135139
timeout / 1000
136140
)
137141
)

0 commit comments

Comments
 (0)