Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 39594cc

Browse files
committed
Fix Packet loss calculation and add new test
When failed requests were mixed with successful ones, the calculation was wrong due to missing parentheses
1 parent 482144c commit 39594cc

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pythonping/executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def append(self, value):
203203
if value.time_elapsed < self.rtt_min:
204204
self.rtt_min = value.time_elapsed
205205

206-
self.packets_lost = self.packets_lost + (0 if value.success else 1 - self.packets_lost) / len(self)
206+
self.packets_lost = self.packets_lost + ((0 if value.success else 1) - self.packets_lost) / len(self)
207207

208208
if self.verbose:
209209
print(value, file=self.output)

test/test_executor.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,20 @@ def test_some_packets_lost(self):
289289
"Unable to calculate packet loss correctly when some of the responses failed"
290290
)
291291

292+
def test_some_packets_lost_mixed(self):
293+
rs = executor.ResponseList([
294+
FailingResponseMock(None, 1),
295+
SuccessfulResponseMock(None, 1),
296+
FailingResponseMock(None, 1),
297+
SuccessfulResponseMock(None, 1),
298+
])
299+
300+
self.assertEqual(
301+
rs.packet_loss,
302+
0.5,
303+
"Unable to calculate packet loss correctly when failing responses are mixed with successful responses"
304+
)
305+
292306

293307
class CommunicatorTestCase(unittest.TestCase):
294308
"""Tests for Communicator"""

0 commit comments

Comments
 (0)