Skip to content

Commit

Permalink
SNOW-1540289: add test for max policy backoff time (#2072)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-aling authored Oct 18, 2024
1 parent a959fc6 commit b55531b
Showing 1 changed file with 53 additions and 3 deletions.
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,
]

0 comments on commit b55531b

Please sign in to comment.