Skip to content

Commit

Permalink
Update stopwatch to include error handling with unit tests (#510)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel McKnight <daniel@neon.ai>
  • Loading branch information
NeonDaniel and NeonDaniel authored Apr 9, 2024
1 parent 6076440 commit b4723b8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion neon_utils/metrics_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ def start(self):
self.start_time = time()

def stop(self):
self.time = time() - self.start_time
try:
self.time = time() - self.start_time
except TypeError:
LOG.error("stop called before start!")
self.time = None
return self.time

def report(self):
Expand Down
4 changes: 4 additions & 0 deletions tests/metric_util_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def test_stopwatch_simple(self):
sleep(sleep_time)
self.assertEqual(round(stopwatch.time, 2), sleep_time)

stopwatch = Stopwatch()
stopwatch.stop()
self.assertIsNone(stopwatch.time)

def test_stopwatch_reuse(self):
sleep_time = 0.5
stopwatch = Stopwatch()
Expand Down

0 comments on commit b4723b8

Please sign in to comment.