Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Errors #2 #7

Merged
merged 2 commits into from
Sep 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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