Skip to content

Commit 469ed04

Browse files
authored
fix: Use utcnow() in refresh calculation (#890)
1 parent 9c566ea commit 469ed04

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

google/cloud/sql/connector/refresh_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def _seconds_until_refresh(
220220
:returns: Time in seconds to wait before performing next refresh.
221221
"""
222222

223-
duration = int((expiration - datetime.datetime.now()).total_seconds())
223+
duration = int((expiration - datetime.datetime.utcnow()).total_seconds())
224224

225225
# if certificate duration is less than 1 hour
226226
if duration < 3600:
@@ -237,7 +237,7 @@ async def _is_valid(task: asyncio.Task) -> bool:
237237
try:
238238
metadata = await task
239239
# only valid if now is before the cert expires
240-
if datetime.datetime.now() < metadata.expiration:
240+
if datetime.datetime.utcnow() < metadata.expiration:
241241
return True
242242
except Exception:
243243
# supress any errors from task

tests/unit/mocks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ def __init__(
6666

6767

6868
async def instance_metadata_success(*args: Any, **kwargs: Any) -> MockMetadata:
69-
return MockMetadata(datetime.datetime.now() + datetime.timedelta(minutes=10))
69+
return MockMetadata(datetime.datetime.utcnow() + datetime.timedelta(minutes=10))
7070

7171

7272
async def instance_metadata_expired(*args: Any, **kwargs: Any) -> MockMetadata:
73-
return MockMetadata(datetime.datetime.now() - datetime.timedelta(minutes=10))
73+
return MockMetadata(datetime.datetime.utcnow() - datetime.timedelta(minutes=10))
7474

7575

7676
async def instance_metadata_error(*args: Any, **kwargs: Any) -> None:

tests/unit/test_refresh_utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ def test_seconds_until_refresh_over_1_hour() -> None:
326326
"""
327327
# using pytest.approx since sometimes can be off by a second
328328
assert (
329-
pytest.approx(_seconds_until_refresh(datetime.now() + timedelta(minutes=62)), 1)
329+
pytest.approx(
330+
_seconds_until_refresh(datetime.utcnow() + timedelta(minutes=62)), 1
331+
)
330332
== 31 * 60
331333
)
332334

@@ -340,7 +342,9 @@ def test_seconds_until_refresh_under_1_hour_over_4_mins() -> None:
340342
"""
341343
# using pytest.approx since sometimes can be off by a second
342344
assert (
343-
pytest.approx(_seconds_until_refresh(datetime.now() + timedelta(minutes=5)), 1)
345+
pytest.approx(
346+
_seconds_until_refresh(datetime.utcnow() + timedelta(minutes=5)), 1
347+
)
344348
== 60
345349
)
346350

@@ -351,4 +355,4 @@ def test_seconds_until_refresh_under_4_mins() -> None:
351355
352356
If expiration is under 4 minutes, should return 0.
353357
"""
354-
assert _seconds_until_refresh(datetime.now() + timedelta(minutes=3)) == 0
358+
assert _seconds_until_refresh(datetime.utcnow() + timedelta(minutes=3)) == 0

0 commit comments

Comments
 (0)