Skip to content

Commit

Permalink
Move ntp.query retry logic to conftest
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Dec 9, 2022
1 parent 28ef983 commit 7715ca1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
20 changes: 20 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import sys
import importlib
import socket

import pytest
import jaraco.functools
from jaraco.context import ExceptionTrap


Expand Down Expand Up @@ -30,3 +33,20 @@ def pywin32_missing():
]
* (sys.version_info > (3, 11))
)


@pytest.fixture(autouse=True)
def retry_ntp_query(request, monkeypatch):
"""
ntp.query is flaky (by design), so be resilient during tests.
"""
if not request.node.name.endswith('net.ntp.query'):
return

from jaraco.net import ntp

retry = jaraco.functools.retry(
retries=2,
trap=socket.timeout,
)
monkeypatch.setattr(ntp, 'query', retry(ntp.query))
11 changes: 1 addition & 10 deletions jaraco/net/ntp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,12 @@
TIME1970 = 0x83AA7E80


_flaky = jaraco.functools.retry(
retries=2,
trap=socket.timeout,
)
"""
ntp.query is flaky (by design), so be resilient to it during tests.
"""


def query(server, force_ipv6=False):
"""
Return current time from NTP server as a Unix timestamp
>>> import time
>>> res = _flaky(query)('us.pool.ntp.org')
>>> res = query('pool.ntp.org')
>>> type(res)
<class 'int'>
>>> res > time.time() - 5
Expand Down

0 comments on commit 7715ca1

Please sign in to comment.