def complete_io(self, iocb, msg): """Called by a handler to return data to the client.""" if _debug: IOQController._debug("complete_io %r %r", iocb, msg) # check to see if it is completing the active one if iocb is not self.active_iocb: raise RuntimeError, "not the current iocb" # normal completion IOController.complete_io(self, iocb, msg) # no longer an active iocb self.active_iocb = None # check to see if we should wait a bit if self.wait_time: # change our state self.state = CTRL_WAITING # schedule a call in the future task = FunctionTask(IOQController._wait_trigger, self) task.install_task(_time() + self.wait_time) else: # change our state self.state = CTRL_IDLE # look for more to do deferred(IOQController._trigger, self)
def process_io(self, iocb): if _debug: SomethingController._debug("process_io %r", iocb) # simulate taking some time to complete this request task_delta = random.random() * 3.0 print("{}, {}, {:4.2f}s".format(iocb.args, iocb.kwargs, task_delta)) task = FunctionTask(self.complete_io, iocb, True) task.install_task(delta=task_delta) if _debug: SomethingController._debug(" - task: %r", task)
def set_timeout(self, delay, err=TimeoutError): """Called to set a transaction timer.""" if _debug: IOCB._debug("set_timeout(%d) %r err=%r", self.ioID, delay, err) # if one has already been created, cancel it if self.ioTimeout: self.ioTimeout.suspend_task() else: self.ioTimeout = FunctionTask(self.abort, err) # (re)schedule it self.ioTimeout.install_task(_time() + delay)
def test_function_task_immediate(self): if _debug: TestTimeMachine._debug("test_function_task_immediate") global sample_task_function_called # create a function task ft = FunctionTask(sample_task_function) sample_task_function_called = [] # reset the time machine, install the task, let it run reset_time_machine() ft.install_task(0.0) run_time_machine(60.0) # function called, 60 seconds have passed assert almost_equal(sample_task_function_called, [0.0]) assert time_machine.current_time == 60.0
def test_function_task_immediate(self): if _debug: TestTimeMachine._debug("test_function_task_immediate") global sample_task_function_called # create a function task ft = FunctionTask(sample_task_function) sample_task_function_called = 0 # reset the time machine, install the task, let it run reset_time_machine() ft.install_task(0.0) run_time_machine(60.0) # function called, no time has passed assert sample_task_function_called == 1 assert time_machine.current_time == 0.0
def test_function_task_delay(self): if _debug: TestTimeMachine._debug("test_function_task_delay") global sample_task_function_called sample_delay = 10.0 # create a function task ft = FunctionTask(sample_task_function) sample_task_function_called = [] # reset the time machine, install the task, let it run reset_time_machine() ft.install_task(sample_delay) run_time_machine(60.0) # function called, no time has passed assert almost_equal(sample_task_function_called, [sample_delay]) assert time_machine.current_time == sample_delay