Skip to content

Commit 27f31cd

Browse files
fix: remove default argument now from _seconds_until_refresh (#356)
Having now as a default argument means it is set once and then never adjusted. So in subsequent calls to _seconds_until_refresh the duration will just continue to grow larger and larger as it evaluates agains the same now The result of this means that duration // 2 block is always being hit and will continue to grow. It could grow to be greater than 3600 and lead to errors.
1 parent 14d6b1c commit 27f31cd

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

google/cloud/alloydb/connector/refresh_utils.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,19 @@
2626
_refresh_buffer: int = 4 * 60 # 4 minutes
2727

2828

29-
def _seconds_until_refresh(
30-
expiration: datetime, now: datetime = datetime.now(timezone.utc)
31-
) -> int:
29+
def _seconds_until_refresh(expiration: datetime) -> int:
3230
"""
3331
Calculates the duration to wait before starting the next refresh.
3432
Usually the duration will be half of the time until certificate
3533
expiration.
3634
3735
Args:
3836
expiration (datetime.datetime): Time of certificate expiration.
39-
now (datetime.datetime): Current time (UTC)
4037
Returns:
4138
int: Time in seconds to wait before performing next refresh.
4239
"""
4340

44-
duration = int((expiration - now).total_seconds())
41+
duration = int((expiration - datetime.now(timezone.utc)).total_seconds())
4542

4643
# if certificate duration is less than 1 hour
4744
if duration < 3600:

tests/unit/test_refresh_utils.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from datetime import timedelta
1717
from datetime import timezone
1818

19+
import pytest
20+
1921
from google.cloud.alloydb.connector.refresh_utils import _seconds_until_refresh
2022

2123

@@ -24,8 +26,14 @@ def test_seconds_until_refresh_over_1_hour() -> None:
2426
Test _seconds_until_refresh returns proper time in seconds.
2527
If expiration is over 1 hour, should return duration/2.
2628
"""
27-
now = datetime.now()
28-
assert _seconds_until_refresh(now + timedelta(minutes=62), now) == 31 * 60
29+
# using pytest.approx since sometimes can be off by a second
30+
assert (
31+
pytest.approx(
32+
_seconds_until_refresh(datetime.now(timezone.utc) + timedelta(minutes=62)),
33+
1,
34+
)
35+
== 31 * 60
36+
)
2937

3038

3139
def test_seconds_until_refresh_under_1_hour_over_4_mins() -> None:
@@ -34,8 +42,14 @@ def test_seconds_until_refresh_under_1_hour_over_4_mins() -> None:
3442
If expiration is under 1 hour and over 4 minutes,
3543
should return duration-refresh_buffer (refresh_buffer = 4 minutes).
3644
"""
37-
now = datetime.now(timezone.utc)
38-
assert _seconds_until_refresh(now + timedelta(minutes=5), now) == 60
45+
# using pytest.approx since sometimes can be off by a second
46+
assert (
47+
pytest.approx(
48+
_seconds_until_refresh(datetime.now(timezone.utc) + timedelta(minutes=5)),
49+
1,
50+
)
51+
== 60
52+
)
3953

4054

4155
def test_seconds_until_refresh_under_4_mins() -> None:

0 commit comments

Comments
 (0)