Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for condition timeout message #274

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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