def test_call_at(self): def callback(stash, arg1, **argv): stash["arg1"] = arg1 if "try_again" in argv and "try_again" not in stash: stash["try_again"] = False return 1.0 else: stash.update(argv) r = Router() r.logger = MockLogger() stash = {} r.call_at(0.5, callback, stash, 1, arg2="a") r.call_at(datetime.datetime.now() + datetime.timedelta(seconds=0.5), callback, stash, 1, arg3="b") r.call_at(datetime.timedelta(seconds=1.0), callback, stash, 1, try_again=True) r.call_at(3, callback, stash, 2) threading.Thread(target=r.start).start() time.sleep(1.0) self.assertEquals(stash["arg1"], 1, "*args ok") self.assertEquals(stash["arg2"], "a", "**kargs ok") self.assertEquals(stash["arg3"], "b", "datetime works") self.assertEquals(stash["try_again"], False, "timedelta works") time.sleep(3.0) self.assertEquals(stash["try_again"], True, "repeated callback") self.assertEquals(stash["arg1"], 2, "int works") r.stop()
def test_start_and_stop(self): r = Router() r.logger = MockLogger() threading.Thread(target=r.start).start() self.assertTrue(r.running) r.stop() self.assertTrue(not r.running) # not waiting for the router to shutdown causes exceptions # on global destruction. (race condition) time.sleep(1.0)