class TestIoServiceAssertions(unittest.TestCase): def setUp(self): self.ioservice = IoService("service", 9000) def test_funcStop(self): with self.assertRaises(RuntimeError): self.ioservice.stop() def test_funcStartTimer(self): with self.assertRaises(RuntimeError): foo = self.ioservice.createTimer(1.0, lambda: None) foo.start() def test_funcSendMessage(self): with self.assertRaises(RuntimeError): self.ioservice.sendMessage((localhost(), 9000), "interface", "channelInfo", {"key": "value"})
class TestIoServiceTimers(unittest.TestCase): def setUp(self): self.ioservice = IoService("timer", 9000) self.ioservice.start() def test_startTimer(self): def onSuccess(): self.successful = True self.successful = False foo = self.ioservice.createTimer(0.1, onSuccess) foo.start() time.sleep(0.2) self.assertTrue(self.successful) def test_startTimerWithArguments(self): def onSuccess(*args, **kwargs): self.assertEqual(args, (1, 2, 3)) self.assertEqual(kwargs, {"kwargOne": 4, "kwargTwo": 5, "kwargThree": 6}) self.successful = True self.successful = False foo = self.ioservice.createTimer(0.1, onSuccess, 1, 2, 3, kwargOne=4, kwargTwo=5, kwargThree=6) foo.start() time.sleep(0.2) self.assertTrue(self.successful) def test_cancelTimer(self): def onExpiration(): self.expired = True self.expired = False foo = self.ioservice.createTimer(0.2, onExpiration) foo.start() time.sleep(0.1) foo.cancel() time.sleep(0.2) self.assertFalse(self.expired) def test_restartTimer(self): def onExpiration(): self.count += 1 if self.count < 2: foo = self.ioservice.createTimer(0.05, onExpiration) foo.start() self.count = 0 foo = self.ioservice.createTimer(0.05, onExpiration) foo.start() time.sleep(0.2) self.assertEqual(self.count, 2) def test_restartCanceledTimer(self): def onExpiration(): self.successful = True foo = self.ioservice.createTimer(0.1, onExpiration) foo.start() time.sleep(0.05) foo.cancel() foo = self.ioservice.createTimer(0.1, onExpiration) foo.start() time.sleep(0.2) self.assertTrue(self.successful) def tearDown(self): self.ioservice.stop()