Debian Codegraph @

tests/unit/test_timer.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import logging
18 from time import sleep
19
20 import pytest
21
22 from cylc.flow.timer import Timer
23
24
25 def test_Timer(caplog: pytest.LogCaptureFixture):
26 """Test the Timer class."""
27 caplog.set_level(logging.INFO)
28 timer = Timer("bob timeout", 1.0)
29
30 # timer attributes
31 assert timer.name == "bob timer"
32 assert timer.interval == "PT1S"
33
34 # start timer
35 timer.reset()
36 assert caplog.records[-1].message == "PT1S bob timer starts NOW"
37
38 # check timeout
39 sleep(2)
40 assert timer.timed_out()
41 assert caplog.records[-1].message == "bob timer timed out after PT1S"
42
43 # stop should do nothing after timeout
44 caplog.clear()
45 timer.stop()
46 assert not caplog.records
47
48 # start timer again, then stop it
49 timer.reset()
50 assert caplog.records[-1].message == "PT1S bob timer starts NOW"
51 timer.stop()
52 assert caplog.records[-1].message == "bob timer stopped"
53
54 # another stop should do nothing
55 caplog.clear()
56 timer.stop()
57 assert not caplog.records