Skip to content

Commit

Permalink
test - less flaky
Browse files Browse the repository at this point in the history
  • Loading branch information
IGalat committed Sep 26, 2024
1 parent 084c376 commit a8388e1
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions tests/tapper/helper/test_repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ def raise_io_error() -> None:
raise OSError()


def wait_for_repeatable() -> None:
for _ in range(5):
if repeat_util.running_repeatable is not None:
break
time.sleep(0.001)


@pytest.fixture(autouse=True)
def assert_clean_state_after_test() -> Generator:
yield
Expand All @@ -49,42 +56,42 @@ class TestRepeatWhileFn:
def test_simplest(self) -> None:
counter = Counter()
repeat.while_fn(lambda: counter.count < 1, counter.tick, interval=0)()
time.sleep(0.001)
wait_for_repeatable()

assert counter.count == 1

def test_several_times(self) -> None:
counter = Counter()
repeat.while_fn(lambda: counter.count < 5, counter.tick, interval=0)()
time.sleep(0.001)
wait_for_repeatable()

assert counter.count == 5

def test_zero_times(self) -> None:
counter = Counter()
repeat.while_fn(lambda: False, counter.tick, interval=0)()
time.sleep(0.001)
wait_for_repeatable()

assert counter.count == 0

def test_max_repeats(self) -> None:
counter = Counter()
repeat.while_fn(lambda: True, counter.tick, interval=0, max_repeats=5)()
time.sleep(0.001)
wait_for_repeatable()

assert counter.count == 5

def test_condition_false_from_start(self) -> None:
counter = Counter()
repeat.while_fn(lambda: False, counter.tick, interval=0)()
time.sleep(0.001)
wait_for_repeatable()

assert counter.count == 0

def test_sleep_time(self, mock_sleep: Any) -> None:
counter = Counter()
repeat.while_fn(lambda: True, counter.tick, interval=0.035, max_repeats=2)()
time.sleep(0.001)
wait_for_repeatable()
assert counter.count == 2
assert mock_sleep.call_count == 2
assert mock_sleep.call_args == call(0.035)
Expand All @@ -93,7 +100,7 @@ def test_second_repeat_overrides_first(self) -> None:
counter1 = Counter()
counter2 = Counter()
repeat.while_fn(lambda: True, counter1.tick, interval=0.05)()
time.sleep(0.01)
wait_for_repeatable()
repeat.while_fn(lambda: counter2.count < 5, counter2.tick, interval=0)()
time.sleep(
0.05
Expand All @@ -104,11 +111,11 @@ def test_second_repeat_overrides_first(self) -> None:

def test_second_repeat_overrides_without_repeats(self) -> None:
counter = Counter()
repeat.while_fn(lambda: True, counter.tick, interval=0.01)()
time.sleep(0.001)
repeat.while_fn(lambda: True, counter.tick, interval=0.05)()
wait_for_repeatable()
assert counter.count == 1
repeat.while_fn(lambda: False, counter.tick, interval=0)()
time.sleep(0.02) # enough for 1st to repeat
time.sleep(0.05) # enough for 1st to repeat
assert counter.count == 1


Expand All @@ -123,21 +130,24 @@ def test_simplest(self, mock_pressed: Any, mock_sleep: Any) -> None:
counter = Counter()
counter.action_at_5 = lambda: mock_return_false(mock_pressed)
repeat.while_pressed("q", counter.tick)()
time.sleep(0.001)
for _ in range(100):
if repeat_util.running_repeatable is not None:
break
wait_for_repeatable()
mock_pressed.return_value = False
assert counter.count == 5

def test_press_and_release(self, mock_pressed: Any, mock_sleep: Any) -> None:
counter = Counter()
counter.action_at_5 = lambda: mock_return_false(mock_pressed)
repeat.while_pressed("q", counter.tick)()
time.sleep(0.001)
wait_for_repeatable()

mock_pressed.return_value = False
time.sleep(0.001)
wait_for_repeatable()

mock_pressed.return_value = True
time.sleep(0.001)
wait_for_repeatable()

assert counter.count == 5
assert mock_sleep.call_count == 4
Expand All @@ -150,7 +160,7 @@ def test_max_repeats(self, mock_pressed: Any, mock_sleep: Any) -> None:
counter = Counter()
counter.action_at_5 = lambda: mock_return_false(mock_pressed)
repeat.while_pressed("q", counter.tick, max_repeats=3)()
time.sleep(0.001)
wait_for_repeatable()
mock_pressed.return_value = False
assert counter.count == 3

Expand All @@ -161,7 +171,7 @@ def test_simplest(self, mock_sleep: Any) -> None:
toggle = repeat.toggle(counter.tick)

toggle()
time.sleep(0.001)
wait_for_repeatable()
toggle()
assert counter.count > 0

Expand All @@ -170,17 +180,17 @@ def test_toggle_twice(self, mock_sleep: Any) -> None:
toggle = repeat.toggle(counter.tick)

toggle()
time.sleep(0.001)
wait_for_repeatable()
toggle()
time.sleep(0.001)
wait_for_repeatable()
count_1 = counter.count
assert counter.count > 0

time.sleep(0.001)
wait_for_repeatable()
assert counter.count == count_1

toggle()
time.sleep(0.001)
wait_for_repeatable()
toggle()
assert counter.count > count_1

Expand All @@ -191,17 +201,17 @@ def test_two_toggles(self, mock_sleep: Any) -> None:
toggle2 = repeat.toggle(counter2.tick)

toggle1()
time.sleep(0.001)
wait_for_repeatable()
toggle2()
time.sleep(0.001)
wait_for_repeatable()
count_1 = counter1.count
time.sleep(0.001)
wait_for_repeatable()
assert counter1.count == count_1

toggle1()
time.sleep(0.001)
wait_for_repeatable()
count_2 = counter2.count
time.sleep(0.001)
wait_for_repeatable()
assert counter2.count == count_2
toggle1()

Expand All @@ -210,11 +220,11 @@ def test_max_repeats(self, mock_sleep: Any) -> None:
toggle = repeat.toggle(counter.tick, max_repeats=2)

toggle()
time.sleep(0.001)
wait_for_repeatable()
assert counter.count == 2

toggle()
time.sleep(0.001)
wait_for_repeatable()
assert counter.count == 4

def test_toggle_again_after_exception(self, mock_sleep: Any) -> None:
Expand All @@ -223,11 +233,11 @@ def test_toggle_again_after_exception(self, mock_sleep: Any) -> None:
toggle = repeat.toggle(counter.tick)

toggle()
time.sleep(0.001)
wait_for_repeatable()
assert counter.count == 5

toggle()
time.sleep(0.001)
wait_for_repeatable()
toggle()
assert counter.count > 5

Expand All @@ -236,6 +246,6 @@ def test_condition(self, mock_sleep: Any) -> None:
toggle = repeat.toggle(counter.tick, condition=lambda: counter.count < 3)

toggle()
time.sleep(0.001)
wait_for_repeatable()

assert counter.count == 3

0 comments on commit a8388e1

Please sign in to comment.