Skip to content

Commit

Permalink
Merge pull request #7 from erunks/reduce-errors
Browse files Browse the repository at this point in the history
Reduce Errors #2
  • Loading branch information
erunks authored Sep 5, 2020
2 parents 35715d9 + 9fe20f7 commit 12da379
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/InternetStatusReporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@ def measure_latency(self):
from json import dumps
from sys import exc_info
from tcp_latency import measure_latency
from src.utils import calculate_standard_deviation
from src.utils import calculate_standard_deviation, remove_none

results = {}
for latency_address in self.latency_addresses:
try:
runs = int(getenv('LATENCY_RUNS'))
data = measure_latency(host=latency_address, port=80, runs=runs, timeout=2.5)
data = remove_none(data)
mean, deviation = calculate_standard_deviation(data, self.logger)
except:
self.logger.exception(f'Unexpected error: {exc_info()[0]}')
Expand Down
10 changes: 5 additions & 5 deletions src/tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ def test_calculate_standard_deviation_when_an_array_of_numbers_is_passed(self, m
)

@patch('logging.Logger')
def test_calculate_standard_deviation_when_an_array_of_numbers_and_none_is_passed(self, mock_logger):
def test_calculate_standard_deviation_when_an_array_of_one_number_is_passed(self, mock_logger):
self.assertEqual(
calculate_standard_deviation([None,1.0,2,3,None,4.0,5], mock_logger),
(3.0, 1.5811388300841898)
calculate_standard_deviation([1.0], mock_logger),
(1.0, 0)
)

@patch('logging.Logger')
def test_calculate_standard_deviation_when_an_array_of_none_is_passed(self, mock_logger):
def test_calculate_standard_deviation_when_an_empty_array_is_passed(self, mock_logger):
self.assertEqual(
calculate_standard_deviation([None, None, None, None], mock_logger),
calculate_standard_deviation([], mock_logger),
None
)

Expand Down
3 changes: 2 additions & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ def calculate_standard_deviation(data, logger):
from math import sqrt
from sys import exc_info

data = remove_none(data)
length = len(data)

if length == 0:
return
elif length == 1:
return data[0], 0

try:
mean = reduce(lambda a,b: a+b, data)/length
Expand Down

0 comments on commit 12da379

Please sign in to comment.