diff options
Diffstat (limited to 'lava_common/test/test_timeout.py')
-rw-r--r-- | lava_common/test/test_timeout.py | 156 |
1 files changed, 77 insertions, 79 deletions
diff --git a/lava_common/test/test_timeout.py b/lava_common/test/test_timeout.py index 1e7675ef9..c2827860b 100644 --- a/lava_common/test/test_timeout.py +++ b/lava_common/test/test_timeout.py @@ -5,8 +5,6 @@ import time from lava_common.exceptions import ConfigurationError, InfrastructureError, JobError from lava_common.timeout import Timeout -# nosec - assert is part of the test process. - class DummyAlarm: def __init__(self, data): @@ -14,7 +12,7 @@ class DummyAlarm: self.previous = 0 def __call__(self, duration): - assert self.data.pop(0) == duration + assert self.data.pop(0) == duration # nosec - assert is part of the test process. previous = self.previous self.previous = duration return previous @@ -26,8 +24,8 @@ class DummySignal: self.previous = 0 def __call__(self, sig, func): - assert sig == signal.SIGALRM - assert func == self.data.pop(0) + assert sig == signal.SIGALRM # nosec - assert is part of the test process. + assert func == self.data.pop(0) # nosec - assert is part of the test process. previous = self.previous self.previous = func return previous @@ -40,19 +38,19 @@ class ParentAction: def test_parsing(): # 1/ simple durations - assert Timeout.parse({"days": 1}) == 86400 - assert Timeout.parse({"hours": 3}) == 3 * 3600 - assert Timeout.parse({"minutes": 1}) == 1 * 60 - assert Timeout.parse({"seconds": 345}) == 345 + assert Timeout.parse({"days": 1}) == 86400 # nosec - assert is part of the test process. + assert Timeout.parse({"hours": 3}) == 3 * 3600 # nosec - assert is part of the test process. + assert Timeout.parse({"minutes": 1}) == 1 * 60 # nosec - assert is part of the test process. + assert Timeout.parse({"seconds": 345}) == 345 # nosec - assert is part of the test process. # 2/ complexe durations - assert Timeout.parse({"minutes": 22, "seconds": 17}) == 22 * 60 + 17 - assert Timeout.parse({"hours": 2, "minutes": 22, "seconds": 17}) == 2 * 3600 + 22 * 60 + 17 - assert Timeout.parse({"days": 1, "minutes": 22, "seconds": 17}) == 86400 + 22 * 60 + 17 + assert Timeout.parse({"minutes": 22, "seconds": 17}) == 22 * 60 + 17 # nosec - assert is part of the test process. + assert Timeout.parse({"hours": 2, "minutes": 22, "seconds": 17}) == 2 * 3600 + 22 * 60 + 17 # nosec - assert is part of the test process. + assert Timeout.parse({"days": 1, "minutes": 22, "seconds": 17}) == 86400 + 22 * 60 + 17 # nosec - assert is part of the test process. # 3/ invalid durations - assert Timeout.parse({"day": 1}) == Timeout.default_duration() - assert Timeout.parse({}) == Timeout.default_duration() + assert Timeout.parse({"day": 1}) == Timeout.default_duration() # nosec - assert is part of the test process. + assert Timeout.parse({}) == Timeout.default_duration() # nosec - assert is part of the test process. with pytest.raises(ConfigurationError): Timeout.parse("") @@ -84,13 +82,13 @@ def test_without_raising(monkeypatch): monkeypatch.setattr(signal, "alarm", DummyAlarm([200, 0])) monkeypatch.setattr(time, "time", lambda: 0) with t(None, None) as max_end_time: - assert max_end_time == 200 + assert max_end_time == 200 # nosec - assert is part of the test process. # signal.alarm and signal.signal were called once each - assert signal.alarm.data == [0] - assert signal.signal.data == [] + assert signal.alarm.data == [0] # nosec - assert is part of the test process. + assert signal.signal.data == [] # nosec - assert is part of the test process. monkeypatch.setattr(time, "time", lambda: 23) - assert signal.alarm.data == [] - assert t.elapsed_time == 23 + assert signal.alarm.data == [] # nosec - assert is part of the test process. + assert t.elapsed_time == 23 # nosec - assert is part of the test process. # 1.1/ with a smaller max_end_time t = Timeout("timeout-name", 200) @@ -98,12 +96,12 @@ def test_without_raising(monkeypatch): monkeypatch.setattr(signal, "alarm", DummyAlarm([125, 0])) monkeypatch.setattr(time, "time", lambda: 0) with t(None, 125) as max_end_time: - assert max_end_time == 125 - assert signal.alarm.data == [0] - assert signal.signal.data == [] + assert max_end_time == 125 # nosec - assert is part of the test process. + assert signal.alarm.data == [0] # nosec - assert is part of the test process. + assert signal.signal.data == [] # nosec - assert is part of the test process. monkeypatch.setattr(time, "time", lambda: 109) - assert signal.alarm.data == [] - assert t.elapsed_time == 109 + assert signal.alarm.data == [] # nosec - assert is part of the test process. + assert t.elapsed_time == 109 # nosec - assert is part of the test process. # 1.2/ with a larger max_end_time t = Timeout("timeout-name", 200) @@ -111,12 +109,12 @@ def test_without_raising(monkeypatch): monkeypatch.setattr(signal, "alarm", DummyAlarm([200, 0])) monkeypatch.setattr(time, "time", lambda: 0) with t(None, 201) as max_end_time: - assert max_end_time == 200 - assert signal.alarm.data == [0] - assert signal.signal.data == [] + assert max_end_time == 200 # nosec - assert is part of the test process. + assert signal.alarm.data == [0] # nosec - assert is part of the test process. + assert signal.signal.data == [] # nosec - assert is part of the test process. monkeypatch.setattr(time, "time", lambda: 45) - assert signal.alarm.data == [] - assert t.elapsed_time == 45 + assert signal.alarm.data == [] # nosec - assert is part of the test process. + assert t.elapsed_time == 45 # nosec - assert is part of the test process. # 2/ with a parent # 2.1/ with a larger max_end_time @@ -127,12 +125,12 @@ def test_without_raising(monkeypatch): monkeypatch.setattr(signal, "alarm", DummyAlarm([100, 177])) monkeypatch.setattr(time, "time", lambda: 0) with t1(parent, 200) as max_end_time: - assert max_end_time == 100 - assert signal.alarm.data == [177] - assert signal.signal.data == [t0._timed_out] + assert max_end_time == 100 # nosec - assert is part of the test process. + assert signal.alarm.data == [177] # nosec - assert is part of the test process. + assert signal.signal.data == [t0._timed_out] # nosec - assert is part of the test process. monkeypatch.setattr(time, "time", lambda: 23) - assert signal.alarm.data == [] - assert t1.elapsed_time == 23 + assert signal.alarm.data == [] # nosec - assert is part of the test process. + assert t1.elapsed_time == 23 # nosec - assert is part of the test process. # 2.2/ with a smaller max_end_time t0 = Timeout("timeout-parent", 50) @@ -142,12 +140,12 @@ def test_without_raising(monkeypatch): monkeypatch.setattr(signal, "alarm", DummyAlarm([50, 27])) monkeypatch.setattr(time, "time", lambda: 0) with t1(parent, 50) as max_end_time: - assert max_end_time == 50 - assert signal.alarm.data == [27] - assert signal.signal.data == [t0._timed_out] + assert max_end_time == 50 # nosec - assert is part of the test process. + assert signal.alarm.data == [27] # nosec - assert is part of the test process. + assert signal.signal.data == [t0._timed_out] # nosec - assert is part of the test process. monkeypatch.setattr(time, "time", lambda: 23) - assert signal.alarm.data == [] - assert t1.elapsed_time == 23 + assert signal.alarm.data == [] # nosec - assert is part of the test process. + assert t1.elapsed_time == 23 # nosec - assert is part of the test process. def test_with_raising(monkeypatch): @@ -159,13 +157,13 @@ def test_with_raising(monkeypatch): monkeypatch.setattr(time, "time", lambda: 0) with pytest.raises(JobError): with t(None, None) as max_end_time: - assert max_end_time == 200 - assert signal.alarm.data == [0] - assert signal.signal.data == [] + assert max_end_time == 200 # nosec - assert is part of the test process. + assert signal.alarm.data == [0] # nosec - assert is part of the test process. + assert signal.signal.data == [] # nosec - assert is part of the test process. monkeypatch.setattr(time, "time", lambda: 200) t._timed_out(None, None) - assert signal.alarm.data == [] - assert t.elapsed_time == 200 + assert signal.alarm.data == [] # nosec - assert is part of the test process. + assert t.elapsed_time == 200 # nosec - assert is part of the test process. # 1.1/ with a smaller max_end_time t = Timeout("timeout-name", 200) @@ -174,13 +172,13 @@ def test_with_raising(monkeypatch): monkeypatch.setattr(time, "time", lambda: 0) with pytest.raises(JobError): with t(None, 125) as max_end_time: - assert max_end_time == 125 - assert signal.alarm.data == [0] - assert signal.signal.data == [] + assert max_end_time == 125 # nosec - assert is part of the test process. + assert signal.alarm.data == [0] # nosec - assert is part of the test process. + assert signal.signal.data == [] # nosec - assert is part of the test process. monkeypatch.setattr(time, "time", lambda: 126) t._timed_out(None, None) - assert signal.alarm.data == [] - assert t.elapsed_time == 126 + assert signal.alarm.data == [] # nosec - assert is part of the test process. + assert t.elapsed_time == 126 # nosec - assert is part of the test process. # 1.2/ with a larger max_end_time t = Timeout("timeout-name", 200) @@ -189,13 +187,13 @@ def test_with_raising(monkeypatch): monkeypatch.setattr(time, "time", lambda: 0) with pytest.raises(JobError): with t(None, 201) as max_end_time: - assert max_end_time == 200 - assert signal.alarm.data == [0] - assert signal.signal.data == [] + assert max_end_time == 200 # nosec - assert is part of the test process. + assert signal.alarm.data == [0] # nosec - assert is part of the test process. + assert signal.signal.data == [] # nosec - assert is part of the test process. monkeypatch.setattr(time, "time", lambda: 200) t._timed_out(None, None) - assert signal.alarm.data == [] - assert t.elapsed_time == 200 + assert signal.alarm.data == [] # nosec - assert is part of the test process. + assert t.elapsed_time == 200 # nosec - assert is part of the test process. # 1.3/ with max_end_time <= 0 t = Timeout("timeout-name", 200) @@ -205,9 +203,9 @@ def test_with_raising(monkeypatch): with pytest.raises(JobError): with t(None, 0) as max_end_time: # Check that the exception is raised before this line - assert 0 - assert signal.alarm.data == [] - assert t.elapsed_time == 0 + assert 0 # nosec - assert is part of the test process. + assert signal.alarm.data == [] # nosec - assert is part of the test process. + assert t.elapsed_time == 0 # nosec - assert is part of the test process. # 2/ with a parent # 2.1/ with a larger max_end_time @@ -219,14 +217,14 @@ def test_with_raising(monkeypatch): monkeypatch.setattr(time, "time", lambda: 0) with pytest.raises(JobError): with t1(parent, 200) as max_end_time: - assert max_end_time == 100 - assert signal.alarm.data == [0] - assert signal.signal.data == [t0._timed_out] + assert max_end_time == 100 # nosec - assert is part of the test process. + assert signal.alarm.data == [0] # nosec - assert is part of the test process. + assert signal.signal.data == [t0._timed_out] # nosec - assert is part of the test process. monkeypatch.setattr(time, "time", lambda: 100) t1._timed_out(None, None) - assert signal.alarm.data == [] - assert signal.signal.data == [t0._timed_out] - assert t1.elapsed_time == 100 + assert signal.alarm.data == [] # nosec - assert is part of the test process. + assert signal.signal.data == [t0._timed_out] # nosec - assert is part of the test process. + assert t1.elapsed_time == 100 # nosec - assert is part of the test process. # 2.2/ with a smaller max_end_time t0 = Timeout("timeout-parent", 50) @@ -237,14 +235,14 @@ def test_with_raising(monkeypatch): monkeypatch.setattr(time, "time", lambda: 0) with pytest.raises(JobError): with t1(parent, 50) as max_end_time: - assert max_end_time == 50 - assert signal.alarm.data == [0] - assert signal.signal.data == [t0._timed_out] + assert max_end_time == 50 # nosec - assert is part of the test process. + assert signal.alarm.data == [0] # nosec - assert is part of the test process. + assert signal.signal.data == [t0._timed_out] # nosec - assert is part of the test process. monkeypatch.setattr(time, "time", lambda: 23) t1._timed_out(None, None) - assert signal.alarm.data == [] - assert signal.signal.data == [t0._timed_out] - assert t1.elapsed_time == 23 + assert signal.alarm.data == [] # nosec - assert is part of the test process. + assert signal.signal.data == [t0._timed_out] # nosec - assert is part of the test process. + assert t1.elapsed_time == 23 # nosec - assert is part of the test process. # 2.3/ with max_end_time <= 0 t0 = Timeout("timeout-parent", 1) @@ -255,10 +253,10 @@ def test_with_raising(monkeypatch): monkeypatch.setattr(time, "time", lambda: 0) with pytest.raises(JobError): with t1(parent, -1) as max_end_time: - assert 0 - assert signal.alarm.data == [] - assert signal.signal.data == [t1._timed_out, t0._timed_out] - assert t1.elapsed_time == 0 + assert 0 # nosec - assert is part of the test process. + assert signal.alarm.data == [] # nosec - assert is part of the test process. + assert signal.signal.data == [t1._timed_out, t0._timed_out] # nosec - assert is part of the test process. + assert t1.elapsed_time == 0 # nosec - assert is part of the test process. # 2.4/ raising parent timeout t0 = Timeout("timeout-parent", 50, InfrastructureError) @@ -269,10 +267,10 @@ def test_with_raising(monkeypatch): monkeypatch.setattr(time, "time", lambda: 0) with pytest.raises(InfrastructureError): with t1(parent, 50) as max_end_time: - assert max_end_time == 50 - assert signal.alarm.data == [0, 0] - assert signal.signal.data == [t0._timed_out] + assert max_end_time == 50 # nosec - assert is part of the test process. + assert signal.alarm.data == [0, 0] # nosec - assert is part of the test process. + assert signal.signal.data == [t0._timed_out] # nosec - assert is part of the test process. monkeypatch.setattr(time, "time", lambda: 50) - assert signal.alarm.data == [] - assert signal.signal.data == [] - assert t1.elapsed_time == 50 + assert signal.alarm.data == [] # nosec - assert is part of the test process. + assert signal.signal.data == [] # nosec - assert is part of the test process. + assert t1.elapsed_time == 50 # nosec - assert is part of the test process. |