Skip to content
Closed
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
13 changes: 0 additions & 13 deletions tests/_Deferred/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,3 @@ def condition():
yield condition

self.assertEqual(x[0], 1)

def test_condition_timeout(self):
x = []

def append():
x.append(1)

sublime.set_timeout(append, 100)

# wait until condition timeout
yield {"condition": lambda: False, "timeout": 500}

self.assertEqual(x[0], 1)
30 changes: 30 additions & 0 deletions tests/test_yield_condition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from unittesting import DeferrableTestCase


class TestYieldConditionsHandlingInDeferredTestCase(DeferrableTestCase):
def test_reraises_errors_raised_in_conditions(self):
try:
yield lambda: 1 / 0
self.fail('Did not reraise the exception from the condition')
except ZeroDivisionError:
pass
except Exception:
self.fail('Did not throw the original exception')

def test_returns_condition_value(self):
rv = yield lambda: 'Hans Peter'

self.assertEqual(rv, 'Hans Peter')

def test_handle_condition_timeout_as_failure(self):
try:
yield {
'condition': lambda: True is False,
'timeout': 100
}
self.fail('Unmet condition should have thrown')
except TimeoutError as e:
self.assertEqual(
str(e),
'Condition not fulfilled within 0.10 seconds'
)
25 changes: 19 additions & 6 deletions unittesting/core/st3/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ def _start_testing():
except Exception as e:
_handle_error(e)

def _continue_testing(deferred):
def _continue_testing(deferred, send_value=None, throw_value=None):
try:
condition = next(deferred)
if throw_value:
condition = deferred.throw(throw_value)
else:
condition = deferred.send(send_value)

if callable(condition):
defer(100, _wait_condition, deferred, condition)
elif isinstance(condition, dict) and "condition" in condition and \
Expand All @@ -70,11 +74,20 @@ def _wait_condition(deferred, condition, period=100, timeout=10000, start_time=N
if start_time is None:
start_time = time.time()

if condition():
defer(10, _continue_testing, deferred)
try:
send_value = condition()
except Exception as e:
defer(10, _continue_testing, deferred, throw_value=e)
return

if send_value:
defer(10, _continue_testing, deferred, send_value=send_value)
elif (time.time() - start_time) * 1000 >= timeout:
self.stream.writeln("Condition timeout, continue anyway.")
defer(10, _continue_testing, deferred)
error = TimeoutError(
'Condition not fulfilled within {:.2f} seconds'
.format(timeout / 1000)
)
defer(10, _continue_testing, deferred, throw_value=error)
else:
defer(period, _wait_condition, deferred, condition, period, timeout, start_time)

Expand Down