PageRenderTime 46ms CodeModel.GetById 36ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/core/test_timers.py

https://bitbucket.org/prologic/circuits/
Python | 87 lines | 56 code | 25 blank | 6 comment | 2 complexity | 9ee52cb11f3d28c80c91d4bceb5cd920 MD5 | raw file
 1# Module:   test_timers
 2# Date:     10th February 2010
 3# Author:   James Mills, prologic at shortcircuit dot net dot au
 4import time
 5
 6"""Timers Tests"""
 7
 8import pytest
 9
10from datetime import datetime, timedelta
11
12from circuits import Event, Component, Timer
13
14
15def pytest_funcarg__app(request):
16    return request.cached_setup(
17        setup=lambda: setupapp(request),
18        teardown=lambda app: teardownapp(app),
19        scope="module"
20    )
21
22
23def setupapp(request):
24    app = App()
25    app.start()
26    return app
27
28
29def teardownapp(app):
30    app.stop()
31
32
33class test(Event):
34    """test Event"""
35
36
37class App(Component):
38
39    def __init__(self):
40        super(App, self).__init__()
41        self.flag = False
42        self.count = 0
43        self.timestamps = []
44
45    def reset(self):
46        self.timestamps = []
47        self.flag = False
48        self.count = 0
49
50    def test(self):
51        self.timestamps.append(time.time())
52        self.count += 1
53        self.flag = True
54
55
56def test_timer(app):
57    timer = Timer(0.1, test(), "timer")
58    timer.register(app)
59    assert pytest.wait_for(app, "flag")
60    app.reset()
61
62
63def test_persistentTimer(app):
64    app.timestamps.append(time.time())
65    timer = Timer(0.2, test(), "timer", persist=True)
66    timer.register(app)
67
68    wait_res = pytest.wait_for(app, "count", 2)
69    assert app.count >= 2
70    assert wait_res
71    delta = app.timestamps[1] - app.timestamps[0]
72    # Should be 0.1, but varies depending on timer precision and load
73    assert delta >= 0.08 and delta < 0.5
74    delta = app.timestamps[2] - app.timestamps[1]
75    assert delta >= 0.08 and delta < 0.5
76    app.reset()
77
78    timer.unregister()
79
80
81def test_datetime(app):
82    now = datetime.now()
83    d = now + timedelta(seconds=0.1)
84    timer = Timer(d, test(), "timer")
85    timer.register(app)
86    assert pytest.wait_for(app, "flag")
87    app.reset()