Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1540289: add test for max policy backoff time #2072

Merged
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
56 changes: 53 additions & 3 deletions test/unit/test_backoff_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,70 @@
import pytest

try:
from snowflake.connector.backoff_policies import exponential_backoff, linear_backoff
from snowflake.connector.backoff_policies import (
DEFAULT_BACKOFF_CAP,
exponential_backoff,
linear_backoff,
)
except ImportError:
pass


@pytest.mark.skipolddriver
def test_linear_backoff():
# test default w/ config
backoff_generator = linear_backoff()()
# after 12 calls, we will reach the default max 16 and won't grow
# 1, 3, 5, 7, 9, 11, 13, 15, 16, 16...
assert max([next(backoff_generator) for _ in range(200)]) <= DEFAULT_BACKOFF_CAP

# test default w/o enable_jitter config
backoff_generator = linear_backoff(enable_jitter=False)()
# after 12 calls, we will reach the default max 16 and won't grow
# 1, 3, 5, 7, 9, 11, 13, 15, 16, 16...
assert [next(backoff_generator) for _ in range(20)] == [
i for i in range(1, DEFAULT_BACKOFF_CAP, 2)
] + [DEFAULT_BACKOFF_CAP] * 12

# # test custom config
backoff_generator = linear_backoff(factor=2, base=1, cap=100, enable_jitter=False)()
assert [next(backoff_generator) for _ in range(5)] == [1, 3, 5, 7, 9]
# after 50 calls, we will reach the max 100 and won't grow
# 1, 3, 5, 7, 9, ... 100, 100, 100, 100, 100, max is the cap 100
assert [next(backoff_generator) for _ in range(60)] == [
i for i in range(1, 100, 2)
] + [100] * 10


@pytest.mark.skipolddriver
def test_exponential_backoff():
# test default w/ enable_jitter config
backoff_generator = exponential_backoff()()
# after 12 calls, we will reach the default max 16 and won't grow
# 1, 3, 5, 7, 9, 11, 13, 15, 16, 16...
assert max([next(backoff_generator) for _ in range(200)]) <= DEFAULT_BACKOFF_CAP

# test default w/o enable_jitter config
backoff_generator = exponential_backoff(enable_jitter=False)()
# after 12 calls, we will reach the default max 16 and won't grow
# 1, 3, 5, 7, 9, 11, 13, 15, 16, 16...
assert [next(backoff_generator) for _ in range(20)] == [1, 2, 4, 8] + [
DEFAULT_BACKOFF_CAP
] * 16

# test custom config
backoff_generator = exponential_backoff(
factor=2, base=1, cap=100, enable_jitter=False
)()
assert [next(backoff_generator) for _ in range(5)] == [1, 2, 4, 8, 16]
# after 7 calls, we will reach max which is the cap 100 and won't grow
assert [next(backoff_generator) for _ in range(10)] == [
1,
2,
4,
8,
16,
32,
64,
100,
100,
100,
]
Loading