|
| 1 | +from functools import partial |
| 2 | +import time |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +import sublime |
| 6 | + |
| 7 | +from unittesting import DeferrableTestCase |
| 8 | + |
| 9 | + |
| 10 | +def run_in_worker(fn, *args, **kwargs): |
| 11 | + sublime.set_timeout_async(partial(fn, *args, **kwargs)) |
| 12 | + |
| 13 | + |
| 14 | +# When we swap `set_timeout_async` with `set_timeout` we basically run |
| 15 | +# our program single-threaded. |
| 16 | +# This has some benefits: |
| 17 | +# - We avoid async/timing issues |
| 18 | +# - We can use plain `yield` to run Sublime's task queue empty, see below |
| 19 | +# - Every code we run will get correct coverage |
| 20 | +# |
| 21 | +# However note, that Sublime will just put all async events on the queue, |
| 22 | +# avoiding the API. We cannot patch that. That means, the event handlers |
| 23 | +# will *not* run using plain `yield` like below, you still have to await |
| 24 | +# them using `yield AWAIT_WORKER`. |
| 25 | +# |
| 26 | + |
| 27 | +class TestTimingInDeferredTestCase(DeferrableTestCase): |
| 28 | + |
| 29 | + def test_a(self): |
| 30 | + # `patch` doesn't work as a decorator with generator functions so we |
| 31 | + # use `with` |
| 32 | + with patch.object(sublime, 'set_timeout_async', sublime.set_timeout): |
| 33 | + messages = [] |
| 34 | + |
| 35 | + def work(message, worktime=None): |
| 36 | + # simulate that a task might take some time |
| 37 | + # this will not yield back but block |
| 38 | + if worktime: |
| 39 | + time.sleep(worktime) |
| 40 | + |
| 41 | + messages.append(message) |
| 42 | + |
| 43 | + def uut(): |
| 44 | + run_in_worker(work, 1, 0.5) # add task (A) |
| 45 | + run_in_worker(work, 2) # add task (B) |
| 46 | + |
| 47 | + uut() # after that task queue has: (A)..(B) |
| 48 | + yield # add task (C) and wait for (C) |
| 49 | + expected = [1, 2] |
| 50 | + self.assertEqual(messages, expected) |
| 51 | + |
| 52 | + def test_b(self): |
| 53 | + # `patch` doesn't work as a decorator with generator functions so we |
| 54 | + # use `with` |
| 55 | + with patch.object(sublime, 'set_timeout_async', sublime.set_timeout): |
| 56 | + messages = [] |
| 57 | + |
| 58 | + def work(message, worktime=None): |
| 59 | + if worktime: |
| 60 | + time.sleep(worktime) |
| 61 | + messages.append(message) |
| 62 | + |
| 63 | + def sub_task(): |
| 64 | + run_in_worker(work, 2, 0.5) # add task (D) |
| 65 | + |
| 66 | + def uut(): |
| 67 | + run_in_worker(work, 1, 0.3) # add task (A) |
| 68 | + run_in_worker(sub_task) # add task (B) |
| 69 | + |
| 70 | + uut() |
| 71 | + # task queue now: (A)..(B) |
| 72 | + |
| 73 | + yield # add task (C) and wait for (C) |
| 74 | + # (A) runs, (B) runs and adds task (D), (C) resolves |
| 75 | + expected = [1] |
| 76 | + self.assertEqual(messages, expected) |
| 77 | + # task queue now: (D) |
| 78 | + yield # add task (E) and wait for it |
| 79 | + # (D) runs and (E) resolves |
| 80 | + expected = [1, 2] |
| 81 | + self.assertEqual(messages, expected) |
0 commit comments