def test_executor_does_not_execute_if_immediately_closed(self): def target(): raise Exception executor = pymongo_greenlet.GreenletPeriodicExecutor( 5, 'dummy', target, self.ioloop ) def run(): self.states.append(2) executor.open() self.states.append(3) executor.close() self.ioloop.add_callback(self.ioloop.stop) green = greenlet.greenlet(run) with freezegun.freeze_time('12:00'): self.states.append(1) self.ioloop.add_callback(run) self.ioloop.start() self.assertState(3)
def test_executor_executes_multiple_times(self): class State(object): executed = 0 def target(): if State.executed == 0: self.states.append(4) if State.executed == 1: self.states.append(7) if State.executed == 2: self.states.append(10) self.ioloop.stop() State.executed += 1 return True executor = pymongo_greenlet.GreenletPeriodicExecutor( 5, 'dummy', target, self.ioloop ) def run(frozen): current = greenlet.getcurrent() parent = current.parent self.states.append(2) executor.open() self.states.append(3) self.ioloop.add_callback(current.switch) parent.switch() self.states.append(5) frozen.tick(delta=timedelta(seconds=3)) # three seconds have passed - do not execute self.ioloop.add_callback(current.switch) parent.switch() self.states.append(6) frozen.tick(delta=timedelta(seconds=3)) # 6s, past the 5 second mark - execute self.ioloop.add_callback(current.switch) parent.switch() self.states.append(8) frozen.tick(delta=timedelta(seconds=2)) # 8s, not past a mark, do not execute self.ioloop.add_callback(current.switch) parent.switch() self.states.append(9) frozen.tick(delta=timedelta(seconds=3)) parent.switch() raise Exception green = greenlet.greenlet(run) with freezegun.freeze_time('12:00') as frozen: self.states.append(1) self.ioloop.add_callback(partial(green.switch, frozen)) self.ioloop.start() self.assertState(10)
def test_executor_executes_immediately(self): def target(): self.states.append(2) self.ioloop.stop() return True executor = pymongo_greenlet.GreenletPeriodicExecutor( 5, 'dummy', target, self.ioloop ) with freezegun.freeze_time('12:00'): executor.open() self.states.append(1) self.ioloop.start() self.assertState(2)
def test_executor_wake_then_close_does_not_execute(self): class State: executed = 0 def target(): if State.executed == 0: self.states.append(2) else: raise Exception State.executed += 1 return True executor = pymongo_greenlet.GreenletPeriodicExecutor( 5, 'dummy', target, self.ioloop ) def run(): current = greenlet.getcurrent() parent = current.parent self.states.append(1) executor.open() self.ioloop.add_callback(current.switch) parent.switch() self.states.append(3) executor.wake() executor.close() self.ioloop.add_callback(current.switch) parent.switch() self.states.append(4) self.ioloop.stop() green = greenlet.greenlet(run) with freezegun.freeze_time('12:00'): self.ioloop.add_callback(green.switch) self.ioloop.start() self.assertState(4)
def test_executor_can_start_twice_safely(self): def target(): self.states.append(2) self.ioloop.add_callback(self.ioloop.stop) return True executor = pymongo_greenlet.GreenletPeriodicExecutor( 5, 'dummy', target, self.ioloop ) with freezegun.freeze_time('12:00'): executor.open() executor.open() self.states.append(1) self.ioloop.start() self.assertState(2)
def test_executor_stops_on_exception(self): class ExpectedException(Exception): pass def target(): self.states.append(2) raise ExpectedException def custom_handler(dummy): exc_type, _, _ = sys.exc_info() self.states.append(3) self.assertEqual(exc_type, ExpectedException) self.ioloop.handle_callback_exception = custom_handler executor = pymongo_greenlet.GreenletPeriodicExecutor( 5, 'dummy', target, self.ioloop ) def run(): current = greenlet.getcurrent() parent = current.parent self.states.append(1) executor.open() self.ioloop.add_callback(current.switch) parent.switch() self.states.append(4) self.ioloop.stop() green = greenlet.greenlet(run) with freezegun.freeze_time('12:00'): self.ioloop.add_callback(green.switch) self.ioloop.start() self.assertState(4)
def test_executor_stops_on_false(self): def target(): self.states.append(4) return False executor = pymongo_greenlet.GreenletPeriodicExecutor( 5, 'dummy', target, self.ioloop ) def run(frozen): current = greenlet.getcurrent() parent = current.parent self.states.append(2) executor.open() self.states.append(3) self.ioloop.add_callback(current.switch) parent.switch() self.states.append(5) frozen.tick(delta=timedelta(seconds=3)) self.ioloop.add_callback(current.switch) parent.switch() self.states.append(6) frozen.tick(delta=timedelta(seconds=3)) self.ioloop.add_callback(current.switch) parent.switch() self.states.append(7) self.ioloop.stop() green = greenlet.greenlet(run) with freezegun.freeze_time('12:00') as frozen: self.states.append(1) self.ioloop.add_callback(partial(green.switch, frozen)) self.ioloop.start() self.assertState(7)