diff --git a/tests/units/tokens/test_timeout_token.py b/tests/units/tokens/test_timeout_token.py index 15549c5..a5dfe75 100644 --- a/tests/units/tokens/test_timeout_token.py +++ b/tests/units/tokens/test_timeout_token.py @@ -329,3 +329,377 @@ def test_timeout_is_more_important_than_cache(): def test_zero_timeout_token_report_is_about_superpower(): for report in TimeoutToken(0).get_report(True), TimeoutToken(0).get_report(False): assert report.cause == CancelCause.SUPERPOWER + + +@pytest.mark.parametrize( + ['addictional_kwargs'], + [ + ({'monotonic': False},), + ({},), + ({'monotonic': True},), + ], +) +def test_bigger_temp_timeout_token_plus_less_temp_timeout_token_with_same_monotonic_flag(addictional_kwargs): + token = TimeoutToken(2, **addictional_kwargs) + TimeoutToken(1, **addictional_kwargs) + + assert isinstance(token, TimeoutToken) + assert token.timeout == 2 + assert len(token.tokens) == 1 + assert len(token.tokens[0].tokens) == 0 + assert isinstance(token.tokens[0], TimeoutToken) + assert token.tokens[0].timeout == 1 + + +@pytest.mark.parametrize( + ['left_addictional_kwargs', 'right_addictional_kwargs'], + [ + ({'monotonic': False}, {'monotonic': True}), + ({}, {'monotonic': True}), + ({'monotonic': True}, {'monotonic': False}), + ({'monotonic': True}, {}), + ], +) +def test_bigger_temp_timeout_token_plus_less_temp_timeout_token_with_not_same_monotonic_flag(left_addictional_kwargs, right_addictional_kwargs): + token = TimeoutToken(2, **left_addictional_kwargs) + TimeoutToken(1, **right_addictional_kwargs) + + assert isinstance(token, TimeoutToken) + assert token.timeout == 2 + assert len(token.tokens) == 1 + assert len(token.tokens[0].tokens) == 0 + assert isinstance(token.tokens[0], TimeoutToken) + assert token.tokens[0].timeout == 1 + + +@pytest.mark.parametrize( + ['timeout_for_equal_or_bigger_token'], + [ + (1,), + (2,), + ], +) +@pytest.mark.parametrize( + ['addictional_kwargs'], + [ + ({'monotonic': False},), + ({},), + ({'monotonic': True},), + ], +) +def test_less_or_equal_temp_not_monotonic_timeout_token_plus_bigger_or_equal_temp_not_monotonic_timeout_token_with_same_monotonic_flag(timeout_for_equal_or_bigger_token, addictional_kwargs): + token = TimeoutToken(1, **addictional_kwargs) + TimeoutToken(timeout_for_equal_or_bigger_token, **addictional_kwargs) + + assert isinstance(token, TimeoutToken) + assert token.timeout == 1 + assert len(token.tokens) == 0 + + +@pytest.mark.parametrize( + ['timeout_for_equal_or_bigger_token'], + [ + (1,), + (2,), + ], +) +@pytest.mark.parametrize( + ['left_addictional_kwargs', 'right_addictional_kwargs'], + [ + ({'monotonic': False}, {'monotonic': True}), + ({}, {'monotonic': True}), + ({'monotonic': True}, {'monotonic': False}), + ({'monotonic': True}, {}), + ], +) +def test_less_or_equal_temp_not_monotonic_timeout_token_plus_bigger_or_equal_temp_not_monotonic_timeout_token_with_not_same_monotonic_flag(timeout_for_equal_or_bigger_token, left_addictional_kwargs, right_addictional_kwargs): + token = TimeoutToken(1, **left_addictional_kwargs) + TimeoutToken(timeout_for_equal_or_bigger_token, **right_addictional_kwargs) + + assert isinstance(token, TimeoutToken) + assert token.timeout == 1 + assert len(token.tokens) == 1 + assert len(token.tokens[0].tokens) == 0 + assert isinstance(token.tokens[0], TimeoutToken) + assert token.tokens[0].timeout == timeout_for_equal_or_bigger_token + + +@pytest.mark.parametrize( + ['addictional_kwargs'], + [ + ({'monotonic': False},), + ({},), + ({'monotonic': True},), + ], +) +def test_bigger_timeout_token_plus_less_temp_timeout_token_with_same_monotonic_flag(addictional_kwargs): + left_timeout_token = TimeoutToken(2, **addictional_kwargs) + token = left_timeout_token + TimeoutToken(1, **addictional_kwargs) + + assert isinstance(token, TimeoutToken) + assert token is not left_timeout_token + assert token.timeout == 1 + assert len(token.tokens) == 0 + + +@pytest.mark.parametrize( + ['left_addictional_kwargs', 'right_addictional_kwargs'], + [ + ({'monotonic': False}, {'monotonic': True}), + ({}, {'monotonic': True}), + ({'monotonic': True}, {'monotonic': False}), + ({'monotonic': True}, {}), + ], +) +def test_bigger_timeout_token_plus_less_temp_timeout_token_with_not_same_monotonic_flag(left_addictional_kwargs, right_addictional_kwargs): + left_timeout_token = TimeoutToken(2, **left_addictional_kwargs) + token = left_timeout_token + TimeoutToken(1, **right_addictional_kwargs) + + assert isinstance(token, TimeoutToken) + assert token.timeout == 1 + assert len(token.tokens) == 1 + assert len(token.tokens[0].tokens) == 0 + assert isinstance(token.tokens[0], TimeoutToken) + assert token.tokens[0].timeout == 2 + assert token.tokens[0] is left_timeout_token + + +@pytest.mark.parametrize( + ['addictional_kwargs'], + [ + ({'monotonic': False},), + ({},), + ({'monotonic': True},), + ], +) +def test_less_not_monotonic_timeout_token_plus_bigger_temp_not_monotonic_timeout_token_with_same_monotonic_flag(addictional_kwargs): + left_timeout_token = TimeoutToken(1, **addictional_kwargs) + token = left_timeout_token + TimeoutToken(2, **addictional_kwargs) + + assert isinstance(token, TimeoutToken) + assert token.timeout == 2 + assert len(token.tokens) == 1 + assert len(token.tokens[0].tokens) == 0 + assert isinstance(token.tokens[0], TimeoutToken) + assert token.tokens[0].timeout == 1 + assert token.tokens[0] is left_timeout_token + + +@pytest.mark.parametrize( + ['left_addictional_kwargs', 'right_addictional_kwargs'], + [ + ({'monotonic': False}, {'monotonic': True}), + ({}, {'monotonic': True}), + ({'monotonic': True}, {'monotonic': False}), + ({'monotonic': True}, {}), + ], +) +def test_less_not_monotonic_timeout_token_plus_bigger_temp_not_monotonic_timeout_token_with_not_same_monotonic_flag(left_addictional_kwargs, right_addictional_kwargs): + left_timeout_token = TimeoutToken(1, **left_addictional_kwargs) + token = left_timeout_token + TimeoutToken(2, **right_addictional_kwargs) + + assert isinstance(token, TimeoutToken) + assert token.timeout == 2 + assert len(token.tokens) == 1 + assert len(token.tokens[0].tokens) == 0 + assert isinstance(token.tokens[0], TimeoutToken) + assert token.tokens[0].timeout == 1 + assert token.tokens[0] is left_timeout_token + + +@pytest.mark.parametrize( + ['addictional_kwargs'], + [ + ({'monotonic': False},), + ({},), + ({'monotonic': True},), + ], +) +def test_bigger_temp_timeout_token_plus_less_timeout_token_with_same_monotonic_flag(addictional_kwargs): + right_timeout_token = TimeoutToken(1, **addictional_kwargs) + token = TimeoutToken(2, **addictional_kwargs) + right_timeout_token + + assert isinstance(token, TimeoutToken) + assert token.timeout == 2 + assert len(token.tokens) == 1 + assert len(token.tokens[0].tokens) == 0 + assert isinstance(token.tokens[0], TimeoutToken) + assert token.tokens[0].timeout == 1 + assert token.tokens[0] is right_timeout_token + + +@pytest.mark.parametrize( + ['left_addictional_kwargs', 'right_addictional_kwargs'], + [ + ({'monotonic': False}, {'monotonic': True}), + ({}, {'monotonic': True}), + ({'monotonic': True}, {'monotonic': False}), + ({'monotonic': True}, {}), + ], +) +def test_bigger_temp_timeout_token_plus_less_timeout_token_with_not_same_monotonic_flag(left_addictional_kwargs, right_addictional_kwargs): + right_timeout_token = TimeoutToken(1, **right_addictional_kwargs) + token = TimeoutToken(2, **left_addictional_kwargs) + right_timeout_token + + assert isinstance(token, TimeoutToken) + assert token.timeout == 2 + assert len(token.tokens) == 1 + assert len(token.tokens[0].tokens) == 0 + assert isinstance(token.tokens[0], TimeoutToken) + assert token.tokens[0].timeout == 1 + assert token.tokens[0] is right_timeout_token + + +@pytest.mark.parametrize( + ['addictional_kwargs'], + [ + ({'monotonic': False},), + ({},), + ({'monotonic': True},), + ], +) +def test_less_temp_not_monotonic_timeout_token_plus_bigger_not_monotonic_timeout_token_with_same_monotonic_flag(addictional_kwargs): + right_timeout_token = TimeoutToken(2, **addictional_kwargs) + token = TimeoutToken(1, **addictional_kwargs) + right_timeout_token + + assert isinstance(token, TimeoutToken) + assert token.timeout == 1 + assert len(token.tokens) == 0 + + +@pytest.mark.parametrize( + ['left_addictional_kwargs', 'right_addictional_kwargs'], + [ + ({'monotonic': False}, {'monotonic': True}), + ({}, {'monotonic': True}), + ({'monotonic': True}, {'monotonic': False}), + ({'monotonic': True}, {}), + ], +) +def test_less_temp_not_monotonic_timeout_token_plus_bigger_not_monotonic_timeout_token_with_not_same_monotonic_flag(left_addictional_kwargs, right_addictional_kwargs): + right_timeout_token = TimeoutToken(2, **right_addictional_kwargs) + token = TimeoutToken(1, **left_addictional_kwargs) + right_timeout_token + + assert isinstance(token, TimeoutToken) + assert token.timeout == 1 + assert len(token.tokens) == 1 + assert len(token.tokens[0].tokens) == 0 + assert isinstance(token.tokens[0], TimeoutToken) + assert token.tokens[0].timeout == 2 + assert token.tokens[0] is right_timeout_token + + +@pytest.mark.parametrize( + ['addictional_kwargs'], + [ + ({'monotonic': False},), + ({},), + ({'monotonic': True},), + ], +) +def test_bigger_timeout_token_plus_less_timeout_token_with_same_monotonic_flag(addictional_kwargs): + left = TimeoutToken(2, **addictional_kwargs) + right = TimeoutToken(1, **addictional_kwargs) + token = left + right + + assert isinstance(token, SimpleToken) + assert token + assert len(token.tokens) == 2 + assert len(token.tokens[0].tokens) == 0 + assert len(token.tokens[1].tokens) == 0 + assert isinstance(token.tokens[0], TimeoutToken) + assert isinstance(token.tokens[1], TimeoutToken) + assert token.tokens[0].timeout == 2 + assert token.tokens[1].timeout == 1 + assert token.tokens[0] is left + assert token.tokens[1] is right + + +@pytest.mark.parametrize( + ['left_addictional_kwargs', 'right_addictional_kwargs'], + [ + ({'monotonic': False}, {'monotonic': True}), + ({}, {'monotonic': True}), + ({'monotonic': True}, {'monotonic': False}), + ({'monotonic': True}, {}), + ], +) +def test_bigger_timeout_token_plus_less_timeout_token_with_not_same_monotonic_flag(left_addictional_kwargs, right_addictional_kwargs): + left = TimeoutToken(2, **left_addictional_kwargs) + right = TimeoutToken(1, **right_addictional_kwargs) + token = left + right + + assert isinstance(token, SimpleToken) + assert token + assert len(token.tokens) == 2 + assert len(token.tokens[0].tokens) == 0 + assert len(token.tokens[1].tokens) == 0 + assert isinstance(token.tokens[0], TimeoutToken) + assert isinstance(token.tokens[1], TimeoutToken) + assert token.tokens[0].timeout == 2 + assert token.tokens[1].timeout == 1 + assert token.tokens[0] is left + assert token.tokens[1] is right + + +@pytest.mark.parametrize( + ['timeout_for_equal_or_bigger_token'], + [ + (1,), + (2,), + ], +) +@pytest.mark.parametrize( + ['addictional_kwargs'], + [ + ({'monotonic': False},), + ({},), + ({'monotonic': True},), + ], +) +def test_less_or_equal_not_monotonic_timeout_token_plus_bigger_or_equal_not_monotonic_timeout_token_with_same_monotonic_flag(timeout_for_equal_or_bigger_token, addictional_kwargs): + left = TimeoutToken(1, **addictional_kwargs) + right = TimeoutToken(timeout_for_equal_or_bigger_token, **addictional_kwargs) + token = left + right + + assert isinstance(token, SimpleToken) + assert token + assert len(token.tokens) == 2 + assert len(token.tokens[0].tokens) == 0 + assert len(token.tokens[1].tokens) == 0 + assert isinstance(token.tokens[0], TimeoutToken) + assert isinstance(token.tokens[1], TimeoutToken) + assert token.tokens[0].timeout == 1 + assert token.tokens[1].timeout == timeout_for_equal_or_bigger_token + assert token.tokens[0] is left + assert token.tokens[1] is right + + +@pytest.mark.parametrize( + ['timeout_for_equal_or_bigger_token'], + [ + (1,), + (2,), + ], +) +@pytest.mark.parametrize( + ['left_addictional_kwargs', 'right_addictional_kwargs'], + [ + ({'monotonic': False}, {'monotonic': True}), + ({}, {'monotonic': True}), + ({'monotonic': True}, {'monotonic': False}), + ({'monotonic': True}, {}), + ], +) +def test_less_or_equal_not_monotonic_timeout_token_plus_bigger_or_equal_not_monotonic_timeout_token_with_not_same_monotonic_flag(timeout_for_equal_or_bigger_token, left_addictional_kwargs, right_addictional_kwargs): + left = TimeoutToken(1, **left_addictional_kwargs) + right = TimeoutToken(timeout_for_equal_or_bigger_token, **right_addictional_kwargs) + token = left + right + + assert isinstance(token, SimpleToken) + assert token + assert len(token.tokens) == 2 + assert len(token.tokens[0].tokens) == 0 + assert len(token.tokens[1].tokens) == 0 + assert isinstance(token.tokens[0], TimeoutToken) + assert isinstance(token.tokens[1], TimeoutToken) + assert token.tokens[0].timeout == 1 + assert token.tokens[1].timeout == timeout_for_equal_or_bigger_token + assert token.tokens[0] is left + assert token.tokens[1] is right