-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use fake clock for faster unit tests (#533)
## Changes Some unit tests of retry logic are slow because they sleep for a second or two. This PR changes ApiClient to accept a Clock which supplies the time for retries and waiting. In unit tests, we use a fake clock that doesn't tick until sleep() is called, and then ticks by the amount provided. ## Tests Existing unit tests. - [ ] `make test` run locally - [ ] `make fmt` applied - [ ] relevant integration tests applied
- Loading branch information
Showing
7 changed files
with
96 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import abc | ||
import time | ||
|
||
|
||
class Clock(metaclass=abc.ABCMeta): | ||
|
||
@abc.abstractmethod | ||
def time(self) -> float: | ||
""" | ||
Return the current time in seconds since the Epoch. | ||
Fractions of a second may be present if the system clock provides them. | ||
:return: The current time in seconds since the Epoch. | ||
""" | ||
|
||
@abc.abstractmethod | ||
def sleep(self, seconds: float) -> None: | ||
""" | ||
Delay execution for a given number of seconds. The argument may be | ||
a floating point number for subsecond precision. | ||
:param seconds: The duration to sleep in seconds. | ||
:return: | ||
""" | ||
|
||
|
||
class RealClock(Clock): | ||
""" | ||
A real clock that uses the ``time`` module to get the current time and sleep. | ||
""" | ||
|
||
def time(self) -> float: | ||
""" | ||
Return the current time in seconds since the Epoch. | ||
Fractions of a second may be present if the system clock provides them. | ||
:return: The current time in seconds since the Epoch. | ||
""" | ||
return time.time() | ||
|
||
def sleep(self, seconds: float) -> None: | ||
""" | ||
Delay execution for a given number of seconds. The argument may be | ||
a floating point number for subsecond precision. | ||
:param seconds: The duration to sleep in seconds. | ||
:return: | ||
""" | ||
time.sleep(seconds) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from databricks.sdk.clock import Clock | ||
|
||
|
||
class FakeClock(Clock): | ||
""" | ||
A simple clock that can be used to mock time in tests. | ||
""" | ||
|
||
def __init__(self, start_time: float = 0.0): | ||
self._start_time = start_time | ||
|
||
def time(self) -> float: | ||
return self._start_time | ||
|
||
def sleep(self, seconds: float) -> None: | ||
self._start_time += seconds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters