|
15 | 15 | LogMessageWaitStrategy, |
16 | 16 | PortWaitStrategy, |
17 | 17 | WaitStrategy, |
| 18 | + RunFunctionWaitStrategy, |
18 | 19 | ) |
19 | 20 |
|
20 | 21 |
|
@@ -550,6 +551,60 @@ def test_wait_until_ready(self, mock_sleep, mock_time, mock_is_file, file_exists |
550 | 551 | strategy.wait_until_ready(mock_container) |
551 | 552 |
|
552 | 553 |
|
| 554 | +class TestRunFunctionWaitStrategy: |
| 555 | + """Test the RunFunctionWaitStrategy class.""" |
| 556 | + |
| 557 | + def test_run_function_wait_strategy_initialization(self): |
| 558 | + func = lambda x: True |
| 559 | + strategy = RunFunctionWaitStrategy(func) |
| 560 | + assert strategy.func == func |
| 561 | + |
| 562 | + def test_run_function_wait_strategy_wait_until_ready(self): |
| 563 | + returns = [False, False, True] |
| 564 | + mock_container = object() |
| 565 | + |
| 566 | + def func(target) -> bool: |
| 567 | + assert target is mock_container |
| 568 | + return returns.pop(0) |
| 569 | + |
| 570 | + strategy = RunFunctionWaitStrategy(func).with_poll_interval(0) |
| 571 | + strategy.wait_until_ready(mock_container) # type: ignore[arg-type] |
| 572 | + |
| 573 | + def test_run_function_wait_strategy_wait_until_ready_with_unknown_exception(self): |
| 574 | + mock_container = object() |
| 575 | + |
| 576 | + def func(target) -> bool: |
| 577 | + assert target is mock_container |
| 578 | + raise RuntimeError("Unknown error, abort!") |
| 579 | + |
| 580 | + strategy = RunFunctionWaitStrategy(func).with_poll_interval(0) |
| 581 | + with pytest.raises(RuntimeError, match="Unknown error, abort!"): |
| 582 | + strategy.wait_until_ready(mock_container) # type: ignore[arg-type] |
| 583 | + |
| 584 | + @pytest.mark.parametrize("transient_exception", [ConnectionError, NotImplementedError]) |
| 585 | + def test_run_function_wait_strategy_wait_until_ready_with_transient_exception(self, transient_exception): |
| 586 | + mock_container = object() |
| 587 | + returns = [False, False, True] |
| 588 | + |
| 589 | + def func(target) -> bool: |
| 590 | + assert target is mock_container |
| 591 | + if returns.pop(0): |
| 592 | + return True |
| 593 | + raise transient_exception("Go on") |
| 594 | + |
| 595 | + # ConnectionError should be in the default transient exceptions, but NotImplementedError ist not |
| 596 | + strategy = ( |
| 597 | + RunFunctionWaitStrategy(func).with_poll_interval(0.001).with_transient_exceptions(NotImplementedError) |
| 598 | + ) |
| 599 | + strategy.wait_until_ready(mock_container) # type: ignore[arg-type] |
| 600 | + |
| 601 | + def test_run_function_wait_strategy_wait_until_ready_with_timeout(self): |
| 602 | + mock_container = object() |
| 603 | + strategy = RunFunctionWaitStrategy(lambda x: False).with_poll_interval(0).with_startup_timeout(0) |
| 604 | + with pytest.raises(TimeoutError, match=r"Wait time (.*) exceeded for"): |
| 605 | + strategy.wait_until_ready(mock_container) # type: ignore[arg-type] |
| 606 | + |
| 607 | + |
553 | 608 | class TestCompositeWaitStrategy: |
554 | 609 | """Test the CompositeWaitStrategy class.""" |
555 | 610 |
|
|
0 commit comments