1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
import unittest
from datetime import timedelta
from time import sleep
import pytest
from reactivex.internal.basic import default_now
from reactivex.scheduler.mainloop import WxScheduler
wx = pytest.importorskip("wx")
def make_app():
app = wx.App()
wx.Frame(None) # We need this for some reason, or the loop won't run
return app
class AppExit(wx.Timer):
def __init__(self, app) -> None:
super().__init__()
self.app = app
def Notify(self):
self.app.ExitMainLoop()
class TestWxScheduler(unittest.TestCase):
def test_wx_schedule_now(self):
scheduler = WxScheduler(wx)
diff = scheduler.now - default_now()
assert abs(diff) < timedelta(milliseconds=1)
def test_wx_schedule_now_units(self):
scheduler = WxScheduler(wx)
diff = scheduler.now
sleep(0.1)
diff = scheduler.now - diff
assert timedelta(milliseconds=80) < diff < timedelta(milliseconds=180)
def test_wx_schedule_action(self):
app = make_app()
exit = AppExit(app)
scheduler = WxScheduler(wx)
ran = False
def action(scheduler, state):
nonlocal ran
ran = True
scheduler.schedule(action)
exit.Start(100, wx.TIMER_ONE_SHOT)
app.MainLoop()
scheduler.cancel_all()
assert ran is True
def test_wx_schedule_action_relative(self):
app = make_app()
exit = AppExit(app)
scheduler = WxScheduler(wx)
starttime = default_now()
endtime = None
def action(scheduler, state):
nonlocal endtime
endtime = default_now()
scheduler.schedule_relative(0.1, action)
exit.Start(200, wx.TIMER_ONE_SHOT)
app.MainLoop()
scheduler.cancel_all()
assert endtime is not None
diff = endtime - starttime
assert timedelta(milliseconds=80) < diff < timedelta(milliseconds=180)
def test_wx_schedule_action_absolute(self):
app = make_app()
exit = AppExit(app)
scheduler = WxScheduler(wx)
starttime = default_now()
endtime = None
def action(scheduler, state):
nonlocal endtime
endtime = default_now()
due = scheduler.now + timedelta(milliseconds=100)
scheduler.schedule_absolute(due, action)
exit.Start(200, wx.TIMER_ONE_SHOT)
app.MainLoop()
scheduler.cancel_all()
assert endtime is not None
diff = endtime - starttime
assert timedelta(milliseconds=80) < diff < timedelta(milliseconds=180)
def test_wx_schedule_action_cancel(self):
app = make_app()
exit = AppExit(app)
scheduler = WxScheduler(wx)
ran = False
def action(scheduler, state):
nonlocal ran
ran = True
d = scheduler.schedule_relative(0.1, action)
d.dispose()
exit.Start(200, wx.TIMER_ONE_SHOT)
app.MainLoop()
scheduler.cancel_all()
assert ran is False
def test_wx_schedule_action_periodic(self):
app = make_app()
exit = AppExit(app)
scheduler = WxScheduler(wx)
period = 0.05
counter = 3
def action(state):
nonlocal counter
if state:
counter -= 1
return state - 1
scheduler.schedule_periodic(period, action, counter)
exit.Start(500, wx.TIMER_ONE_SHOT)
app.MainLoop()
scheduler.cancel_all()
assert counter == 0
|