Skip to content

Commit

Permalink
Add case for calculating rate of zero time delta
Browse files Browse the repository at this point in the history
This was not seen in real life transfers, but for the functional
tests, the time deltas between reads were infitesimally small
causing the rate to be calculate with a time of 0 and throw
a ZeroDivisionError
  • Loading branch information
kyleknap committed Nov 29, 2017
1 parent 07021b4 commit cc4a5ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion s3transfer/bandwidth.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,14 @@ def record_consumption_rate(self, amt, time_at_consumption):
self._last_time = time_at_consumption

def _calculate_rate(self, amt, time_at_consumption):
return amt / (time_at_consumption - self._last_time)
time_delta = time_at_consumption - self._last_time
if time_delta <= 0:
# While it is really unlikley to see this in an actual transfer,
# we do not want to be returning back a negative rate or try to
# divide the amount by zero. So instead return back an infinite
# rate as the time delta is infinitesimally small.
return float('inf')
return amt / (time_delta)

def _calculate_exponential_moving_average_rate(self, amt,
time_at_consumption):
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_bandwidth.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,10 @@ def test_get_projected_rate(self):
self.assertEqual(projected_rate, 0.96)
self.rate_tracker.record_consumption_rate(1, 3)
self.assertEqual(self.rate_tracker.current_rate, projected_rate)

def test_get_projected_rate_for_same_timestamp(self):
self.rate_tracker.record_consumption_rate(1, 1)
self.assertEqual(
self.rate_tracker.get_projected_rate(1, 1),
float('inf')
)

0 comments on commit cc4a5ba

Please sign in to comment.