Fix unit tests

This commit is contained in:
Gina Häußge committed 2018-09-26 15:52:10 +02:00
1 parent f32ea65b2c
commit 5ff790048d
3 files changed
+23 -2

No files matched your search

+4 -1
View File
@@ -1270,8 +1270,9 @@ class ResettableTimer(threading.Thread):
class CountedEvent(object):
def __init__(self, value=0, maximum=None, **kwargs):
def __init__(self, value=0, minimum=0, maximum=None, **kwargs):
self._counter = 0
self._min = minimum
self._max = kwargs.get("max", maximum)
self._mutex = threading.RLock()
self._event = threading.Event()
@@ -1313,6 +1314,8 @@ class CountedEvent(object):
def _internal_set(self, value):
self._counter = value
if self._counter <= 0:
if self._min is not None and self._counter < self._min:
self._counter = self._min
self._event.clear()
else:
if self._max is not None and self._counter > self._max:
+1 -1
View File
@@ -479,7 +479,7 @@ class MachineCom(object):
self._long_running_commands = settings().get(["serial", "longRunningCommands"])
self._checksum_requiring_commands = settings().get(["serial", "checksumRequiringCommands"])
self._clear_to_send = CountedEvent(name="comm.clear_to_send")
self._clear_to_send = CountedEvent(name="comm.clear_to_send", minimum=None)
self._send_queue = SendQueue()
self._temperature_timer = None
self._sd_status_timer = None
+18
View File
@@ -88,6 +88,24 @@ class CountedEventTest(unittest.TestCase):
self.assertEqual(0, event._counter)
self.assertFalse(event._event.is_set())
def test_clear_more_than_available_without_minimum(self):
"""The counter may sink below zero if initialized without a minimum."""
event = CountedEvent(1, minimum=None)
self.assertEqual(1, event._counter)
self.assertTrue(event._event.is_set())
event.clear()
self.assertEqual(0, event._counter)
self.assertFalse(event._event.is_set())
event.clear()
self.assertEqual(-1, event._counter)
self.assertFalse(event._event.is_set())
def test_blocked(self):
"""Blocked should only be true if the counter is 0."""