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

fix: remove default argument now from _seconds_until_refresh #356

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 2 additions & 5 deletions google/cloud/alloydb/connector/refresh_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,19 @@
_refresh_buffer: int = 4 * 60 # 4 minutes


def _seconds_until_refresh(
expiration: datetime, now: datetime = datetime.now(timezone.utc)
) -> int:
def _seconds_until_refresh(expiration: datetime) -> int:
"""
Calculates the duration to wait before starting the next refresh.
Usually the duration will be half of the time until certificate
expiration.

Args:
expiration (datetime.datetime): Time of certificate expiration.
now (datetime.datetime): Current time (UTC)
Returns:
int: Time in seconds to wait before performing next refresh.
"""

duration = int((expiration - now).total_seconds())
duration = int((expiration - datetime.now(timezone.utc)).total_seconds())

# if certificate duration is less than 1 hour
if duration < 3600:
Expand Down
22 changes: 18 additions & 4 deletions tests/unit/test_refresh_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from datetime import timedelta
from datetime import timezone

import pytest

from google.cloud.alloydb.connector.refresh_utils import _seconds_until_refresh


Expand All @@ -24,8 +26,14 @@ def test_seconds_until_refresh_over_1_hour() -> None:
Test _seconds_until_refresh returns proper time in seconds.
If expiration is over 1 hour, should return duration/2.
"""
now = datetime.now()
assert _seconds_until_refresh(now + timedelta(minutes=62), now) == 31 * 60
# using pytest.approx since sometimes can be off by a second
assert (
pytest.approx(
_seconds_until_refresh(datetime.now(timezone.utc) + timedelta(minutes=62)),
1,
)
== 31 * 60
)


def test_seconds_until_refresh_under_1_hour_over_4_mins() -> None:
Expand All @@ -34,8 +42,14 @@ def test_seconds_until_refresh_under_1_hour_over_4_mins() -> None:
If expiration is under 1 hour and over 4 minutes,
should return duration-refresh_buffer (refresh_buffer = 4 minutes).
"""
now = datetime.now(timezone.utc)
assert _seconds_until_refresh(now + timedelta(minutes=5), now) == 60
# using pytest.approx since sometimes can be off by a second
assert (
pytest.approx(
_seconds_until_refresh(datetime.now(timezone.utc) + timedelta(minutes=5)),
1,
)
== 60
)


def test_seconds_until_refresh_under_4_mins() -> None:
Expand Down
Loading