def test_eventloop_schedule_action_periodic(self): scheduler = EventLoopScheduler(exit_if_empty=False) gate = threading.Semaphore(0) period = 0.05 counter = 3 def action(state): nonlocal counter if state: counter -= 1 return state - 1 if counter == 0: gate.release() disp = scheduler.schedule_periodic(period, action, counter) def dispose(scheduler, state): disp.dispose() gate.release() gate.acquire() assert counter == 0 assert scheduler._has_thread() is True scheduler.schedule(dispose) gate.acquire() assert scheduler._has_thread() is True sleep(period) scheduler.dispose() sleep(period) assert scheduler._has_thread() is False
def test_event_loop_schedule_action(self): scheduler = EventLoopScheduler(exit_if_empty=True) ran = [False] gate = threading.Semaphore(0) def action(scheduler, state): gate.release() ran[0] = True scheduler.schedule(action) gate.acquire() assert (ran[0] is True)
def test_event_loop_different_thread(self): thread_id = [None] scheduler = EventLoopScheduler(exit_if_empty=True) gate = threading.Semaphore(0) def action(scheduler, state): gate.release() thread_id[0] = threading.current_thread().ident scheduler.schedule(action) gate.acquire() assert (thread_id[0] != threading.current_thread().ident)
def test_event_loop_schedule_action(self): scheduler = EventLoopScheduler(exit_if_empty=True) ran = [False] gate = threading.Semaphore(0) def action(scheduler, state): gate.release() ran[0] = True scheduler.schedule(action) gate.acquire() assert (ran[0] == True)
def test_event_loop_schedule_action_cancel(self): scheduler = EventLoopScheduler(exit_if_empty=True) gate = threading.Semaphore(0) ran = [False] def action(scheduler, state): ran[0] = True d = scheduler.schedule_relative(timedelta(milliseconds=1), action) d.dispose() sleep(0.1) assert (not ran[0])
def test_event_loop_schedule_action(self): scheduler = EventLoopScheduler(exit_if_empty=True) ran = False gate = threading.Semaphore(0) def action(scheduler, state): nonlocal ran ran = True gate.release() scheduler.schedule(action) gate.acquire() assert ran is True assert scheduler._has_thread() is False
def test_event_loop_different_thread(self): thread_id = None scheduler = EventLoopScheduler(exit_if_empty=True) gate = threading.Semaphore(0) def action(scheduler, state): nonlocal thread_id thread_id = threading.current_thread().ident gate.release() scheduler.schedule(action) gate.acquire() assert thread_id != threading.current_thread().ident assert scheduler._has_thread() is False
def test_event_loop_schedule_ordered_actions(self): scheduler = EventLoopScheduler(exit_if_empty=True) gate = threading.Semaphore(0) result = [] scheduler.schedule(lambda s, t: result.append(1)) def action(scheduler, state): gate.release() result.append(2) scheduler.schedule(action) gate.acquire() assert (result == [1,2])
def test_event_loop_schedule_action_due(self): scheduler = EventLoopScheduler(exit_if_empty=True) gate = threading.Semaphore(0) starttime = datetime.utcnow() endtime = [None] def action(scheduler, state): endtime[0] = datetime.utcnow() gate.release() scheduler.schedule_relative(timedelta(milliseconds=200), action) gate.acquire() diff = endtime[0] - starttime assert (diff > timedelta(milliseconds=180))
def test_event_loop_schedule_action_due(self): scheduler = EventLoopScheduler(exit_if_empty=True) gate = threading.Semaphore(0) starttime = datetime.utcnow() endtime = [None] def action(scheduler, state): endtime[0] = datetime.utcnow() gate.release() scheduler.schedule_relative(timedelta(milliseconds=200), action) gate.acquire() diff = endtime[0]-starttime assert(diff > timedelta(milliseconds=180))
def test_event_loop_schedule_action_absolute_due(self): scheduler = EventLoopScheduler(exit_if_empty=True) gate = threading.Semaphore(0) starttime = default_now() endtime = None def action(scheduler, state): nonlocal endtime endtime = default_now() gate.release() scheduler.schedule_absolute(scheduler.now, action) gate.acquire() diff = endtime - starttime assert diff < timedelta(milliseconds=180) assert scheduler._has_thread() is False
def test_event_loop_schedule_action_absolute_due(self): scheduler = EventLoopScheduler(exit_if_empty=True) gate = threading.Semaphore(0) starttime = datetime.utcnow() endtime = None def action(scheduler, state): nonlocal endtime endtime = datetime.utcnow() gate.release() scheduler.schedule_absolute(scheduler.now, action) gate.acquire() diff = endtime - starttime assert diff < timedelta(milliseconds=180) assert scheduler._has_thread() is False
def test_eventloop_schedule_action_periodic(self): scheduler = EventLoopScheduler() gate = threading.Semaphore(0) period = 0.050 counter = [3] def action(state): if state: counter[0] -= 1 return state - 1 if counter[0] == 0: gate.release() scheduler.schedule_periodic(period, action, counter[0]) def done(): assert counter[0] == 0 gate.acquire()
def test_event_loop_schedule_ordered_actions_due(self): scheduler = EventLoopScheduler(exit_if_empty=True) gate = threading.Semaphore(0) result = [] def action(scheduler, state): result.append(3) gate.release() scheduler.schedule_relative(0.10, action) scheduler.schedule_relative(0.05, lambda s, t: result.append(2)) scheduler.schedule(lambda s, t: result.append(1)) gate.acquire() assert result == [1, 2, 3] assert scheduler._has_thread() is False
def test_event_loop_schedule_ordered_actions_due_mixed(self): scheduler = EventLoopScheduler(exit_if_empty=True) gate = threading.Semaphore(0) result = [] def action(scheduler, state): result.append(1) scheduler.schedule_relative(0.2, action3) scheduler.schedule(action2) def action2(scheduler, state): result.append(2) def action3(scheduler, state): result.append(3) gate.release() scheduler.schedule(action) gate.acquire() assert result == [1, 2, 3] assert scheduler._has_thread() is False
def bus_context(conf): blinker_ticks = Observable.interval(conf.blink.interval * 1000) montage_ticks = Observable.interval(conf.montage.interval * 1000) event_loop = EventLoopScheduler() buttons = [ Button(Shoot, conf.event.shoot, conf.bounce_time, event_loop), Button(Quit, conf.event.quit, conf.bounce_time, event_loop), Button(Quit, conf.event.reboot, conf.bounce_time, event_loop), Button(Quit, conf.event.shutdown, conf.bounce_time, event_loop) ] commands = (Observable.merge([button.events for button in buttons]).merge( ThreadPoolScheduler(max_workers=conf.workers), blinker_ticks.map(const(Blink())), montage_ticks.map(const(ShowRandomMontage())))) with commands.subscribe(on_next=inject(handle_command, conf)): yield
def test_eventloop_schedule_periodic_dispose_error(self): scheduler = EventLoopScheduler(exit_if_empty=False) scheduler.dispose() ran = False def action(scheduler, state): nonlocal ran ran = True with pytest.raises(DisposedException): scheduler.schedule_periodic(0.1, action) assert ran is False assert scheduler._has_thread() is False
def test_event_loop_schedule_ordered_actions(self): scheduler = EventLoopScheduler(exit_if_empty=True) gate = threading.Semaphore(0) result = [] scheduler.schedule(lambda s, t: result.append(1)) def action(scheduler, state): gate.release() result.append(2) scheduler.schedule(action) gate.acquire() assert (result == [1, 2])
def test_eventloop_schedule_periodic_dispose(self): scheduler = EventLoopScheduler(exit_if_empty=False) scheduler.dispose() ran = False def action(scheduler, state): nonlocal ran ran = True exc = None try: scheduler.schedule_periodic(0.1, scheduler.now, action) except Exception as e: exc = e finally: assert isinstance(exc, DisposedException) assert ran is False assert scheduler._has_thread() is False
def test_event_loop_now(self): scheduler = EventLoopScheduler() res = scheduler.now() - datetime.utcnow() assert res < timedelta(microseconds=1000)
def test_event_loop_now_units(self): scheduler = EventLoopScheduler() diff = scheduler.now sleep(0.1) diff = scheduler.now - diff assert timedelta(milliseconds=80) < diff < timedelta(milliseconds=180)
def test_event_loop_now(self): scheduler = EventLoopScheduler() diff = scheduler.now - default_now() assert abs(diff) < timedelta(milliseconds=1)
def test_event_loop_now(self): scheduler = EventLoopScheduler() res = scheduler.now - datetime.utcnow() assert res < timedelta(microseconds=1000)