def testReaderOrWriterDoesNotMaskKeyboardInterrupt(self): fd, path = mkstemp() reactor = Reactor() def raiser(): raise KeyboardInterrupt('Ctrl-C') reactor.addReader(sok=fd, sink=raiser) self.assertEquals([raiser], [c.callback for c in reactor._readers.values()]) try: reactor.step() self.fail('step() must raise KeyboardInterrupt') except KeyboardInterrupt: self.assertEquals([], [c.callback for c in reactor._readers.values()]) fd, path = mkstemp() reactor = Reactor() reactor.addWriter(sok=fd, source=raiser) try: reactor.step() self.fail('step() must raise KeyboardInterrupt') except KeyboardInterrupt: self.assertEquals([], [c.callback for c in reactor._readers.values()])
def testReaderOrWriterDoesNotMaskSystemExit(self): fd, path = mkstemp() reactor = Reactor() def raiser(): raise SystemExit('shutdown...') reactor.addReader(sok=fd, sink=raiser) self.assertEquals([raiser], [c.callback for c in reactor._readers.values()]) try: reactor.step() self.fail('step() must raise SystemExit') except SystemExit: self.assertEquals([], [c.callback for c in reactor._readers.values()]) fd, path = mkstemp() reactor = Reactor() reactor.addWriter(sok=fd, source=raiser) try: reactor.step() self.fail('step() must raise SystemExit') except SystemExit: self.assertEquals([], [c.callback for c in reactor._readers.values()])
def testGetRidOfBadFileDescriptors(self): reactor = Reactor() class BadSocket(object): def fileno(self): return 88 def close(self): raise Exception('hell breaks loose') self.timeout = False def timeout(): self.timeout = True reactor.addReader(99, lambda: None) # broken reactor.addWriter(99, lambda: None) # broken reactor.addReader(BadSocket(), lambda: None) # even more broken reactor.addTimer(0.01, timeout) with self.stderr_replaced() as s: for i in range(10): if self.timeout: break reactor.step() self.assertTrue("Bad file descriptor" in s.getvalue(), s.getvalue()) self.assertTrue(self.timeout) self.assertEquals({}, reactor._readers) self.assertEquals({}, reactor._writers) self.assertEquals([], reactor._timers)
def testAddSocketWriting(self): class Sok: def __hash__(self): return 1 reactor = Reactor() mockSok = Sok() self.assertTrue(mockSok not in reactor._writers) reactor.addWriter(mockSok, None) self.assertTrue(mockSok in reactor._writers) reactor.removeWriter(mockSok) self.assertFalse(mockSok in reactor._writers)
def testWriteFollowsRead(self): reactor = Reactor(lambda r,w,o,t: (r,w,o)) t = [] def read(): t.append('t1') def write(): t.append('t2') reactor.addWriter('sok1', write) reactor.addReader('sok1', read) reactor.step() self.assertEquals(['t1', 't2'], t)
def testAssertionErrorInWRITECallback(self): sys.stderr = StringIO() try: def callback(): raise AssertionError('here is the assertion') reactor = Reactor(lambda r, w, o, t: (r,w,o)) reactor.addWriter(9, callback) try: reactor.step() self.fail('must raise exception') except AssertionError, e: self.assertEquals('here is the assertion', str(e)) finally: sys.stderr = sys.__stderr__
def testReadDeletesWrite(self): reactor = Reactor(lambda r,w,o,t: (r,w,o)) self.read = self.write = False def read(): self.read = True reactor.removeWriter('sok1') def write(): self.write = True reactor.addWriter('sok1', write) reactor.addReader('sok1', read) reactor.step() self.assertTrue(self.read) self.assertFalse(self.write)
def testWrongUseAfterSuspending(self): reactor = Reactor(select_func=mockselect) handle = ['initial value'] def callback(): handle[0] = reactor.suspend() sok = MockSocket() reactor.addWriter(sok, callback) reactor.step() self.assertEquals(sok, handle[0]) try: reactor.addWriter(sok, callback) self.fail("Exception not raised") except ValueError, e: self.assertEquals('Socket is suspended', str(e))
def testWriteFollowsRead(self): reactor = Reactor(lambda r, w, o, t: (r, w, o)) t = [] def read(): t.append('t1') def write(): t.append('t2') reactor.addWriter('sok1', write) reactor.addReader('sok1', read) reactor.step() self.assertEquals(['t1', 't2'], t)
def testReactorResumeWriter(self): handle = ['initial value'] reactor = Reactor(select_func=mockselect) def callback(): handle[0] = reactor.suspend() yield yield sok = MockSocket() reactor.addWriter(sok, callback().next) reactor.step() reactor.resumeWriter(handle[0]) reactor.step() self.assertTrue(sok in reactor._writers) self.assertTrue(sok not in reactor._readers) self.assertRaises(KeyError, reactor.resumeWriter, handle[0])
def testGetRidOfClosedSocket(self): reactor = Reactor() sok = socket() sok.close() callbacks = [] def callback(): callbacks.append(True) reactor.addReader(sok, callback) reactor.addWriter(sok, callback) with self.stderr_replaced() as s: reactor.step() reactor.step() self.assertTrue("Bad file descriptor" in s.getvalue(), s.getvalue()) self.assertEquals({}, reactor._readers) self.assertEquals([True, True], callbacks)
def testAssertionErrorInWRITECallback(self): sys.stderr = StringIO() try: def callback(): raise AssertionError('here is the assertion') reactor = Reactor(lambda r, w, o, t: (r, w, o)) reactor.addWriter(9, callback) try: reactor.step() self.fail('must raise exception') except AssertionError, e: self.assertEquals('here is the assertion', str(e)) finally: sys.stderr = sys.__stderr__
def testReadDeletesWrite(self): reactor = Reactor(lambda r, w, o, t: (r, w, o)) self.read = self.write = False def read(): self.read = True reactor.removeWriter('sok1') def write(): self.write = True reactor.addWriter('sok1', write) reactor.addReader('sok1', read) reactor.step() self.assertTrue(self.read) self.assertFalse(self.write)
def testCleanUp(self): reactor = Reactor(select_func=mockselect) def handler(): reactor.suspend() yield reactor.addWriter(1, lambda: None) reactor.addReader(2, lambda: None) reactor.addReader(3, handler().next) reactor.step() self.assertTrue(1 in reactor._writers) reactor.cleanup(1) self.assertFalse(1 in reactor._writers) self.assertTrue(2 in reactor._readers) reactor.cleanup(2) self.assertFalse(2 in reactor._readers) self.assertTrue(3 in reactor._suspended) reactor.cleanup(3) self.assertFalse(3 in reactor._suspended)
def testReactorSuspend(self): handle = ['initial value'] reactor = Reactor(select_func=mockselect) def callback(): handle[0] = reactor.suspend() sok = MockSocket() reactor.addReader(sok, callback) self.assertTrue(sok in reactor._readers) reactor.step() self.assertTrue(sok not in reactor._readers) sok = MockSocket() reactor.addWriter(sok, callback) self.assertTrue(sok in reactor._writers) reactor.step() self.assertTrue(sok not in reactor._writers) self.assertTrue(handle[0] != None) self.assertTrue(handle[0] != 'initial value')
def testShutdownReactor(self): reactor = Reactor() sok1 = MockSocket() sok2 = MockSocket() sok3 = MockSocket() def callback(): reactor.suspend() reactor.addReader(sok1, lambda: None) reactor.addWriter(sok2, lambda: None) reactor.addWriter(sok3, callback) reactor.step() self.assertFalse(sok3.fileno() in reactor._fds) self.assertFalse(sok3.fileno() in reactor._fds) with self.stdout_replaced() as s: reactor.shutdown() self.assertTrue(str(sok1) in s.getvalue(), s.getvalue()) self.assertTrue(str(sok2) in s.getvalue(), s.getvalue()) self.assertTrue(str(sok3) in s.getvalue()) self.assertTrue(sok1.closed) self.assertTrue(sok2.closed) self.assertTrue(sok3.closed)