def testAcceptorWithPrio(self): reactor = CallTrace() acceptor = Acceptor(reactor, self.port, lambda sok: None, prio=5) client = socket() client.connect(('127.0.0.1', self.port)) acceptor._accept() self.assertEquals(5, reactor.calledMethods[0].kwargs['prio'])
def testReuseAddress(self): reactor = CallTrace() acceptor = Acceptor(reactor, self.port, lambda sok: None) client = socket() client.connect(('127.0.0.1', self.port)) acceptor._accept() acceptor.close() acceptor = Acceptor(reactor, self.port, lambda sok: None)
class DebugPrompt(object): def __init__(self, reactor, port, globals): self._reactor = reactor self._port = port self._globals = globals def observer_init(self): self._acceptor = Acceptor(reactor=self._reactor, port=self._port, sinkFactory=self._connect, bindAddress='127.0.0.1') def _connect(self, sok): next(self.handle_conversation(sok)) def close(self): self._acceptor.close() @identify def handle_conversation(self, sok): response = PROMPT this = yield while True: self._reactor.addWriter(sok, this.__next__) yield self._reactor.removeWriter(sok) sok.sendall(response) self._reactor.addReader(sok, this.__next__) yield self._reactor.removeReader(sok) command = sok.recv(1024).strip() if not command: sok.close() yield buff = StringIO() try: stdout_prev = sys.stdout stderr_prev = sys.stderr sys.stdout = buff sys.stderr = buff exec(command, self._globals) except: print_exc(limit=0) finally: sys.stdout = stdout_prev sys.stderr = stderr_prev response = buff.getvalue().encode() + PROMPT
def testBindAddressCustom(self): reactor = CallTrace() acceptor = Acceptor(reactor, self.port, lambda sok: None, bindAddress='127.0.0.1', prio=5) self.assertEquals(('127.0.0.1', self.port), acceptor._sok.getsockname())
def testConnect(self): reactor = CallTrace() acceptor = Acceptor(reactor, self.port, lambda sok: None) self.assertEquals('addReader', reactor.calledMethods[0].name) acceptCallback = reactor.calledMethods[0].args[1] sok = socket() sok.connect(('localhost', self.port)) acceptCallback() self.assertEquals(['addReader'], reactor.calledMethodNames()) reactor.calledMethods[0].args[0].close() sok.close()
def testStartListening(self): reactor = CallTrace() port = randint(2**10, 2**16) acceptor = Acceptor(reactor, port, None) self.assertEquals('addReader', reactor.calledMethods[0].name) sok = reactor.calledMethods[0].args[0] out = Popen(['netstat', '--numeric', '--listening', '--tcp'], stdout=PIPE, stderr=PIPE).communicate()[0] self.assertTrue(str(port) in out, out) sok.close() callback = reactor.calledMethods[0].args[1] self.assertTrue(callable(callback))
def testCreateSink(self): reactor = CallTrace('reactor') port = randint(2**10, 2**16) class sinkFactory(object): def __init__(inner, *args, **kwargs): self.args, self.kwargs = args, kwargs acceptor = Acceptor(reactor, port, sinkFactory) acceptCallback = reactor.calledMethods[0].args[1] sok = socket() sok.connect(('localhost', port)) acceptCallback() self.assertEquals('addReader', reactor.calledMethods[1].name) sink = reactor.calledMethods[1].args[1] self.assertEquals(socket, type(self.args[0])) reactor.calledMethods[0].args[0].close()
def testReadData(self): reactor = CallTrace('reactor') port = randint(2**10, 2**16) class sinkFactory(object): def __init__(inner, *args, **kwargs): self.args, self.kwargs = args, kwargs def next(inner): self.next = True acceptor = Acceptor(reactor, port, sinkFactory) acceptCallback = reactor.calledMethods[0].args[1] sok = socket() sok.connect(('localhost', port)) acceptCallback() sink = reactor.calledMethods[1].args[1] self.next = False sink.next() self.assertTrue(self.next) reactor.calledMethods[0].args[0].close()
def testBindAddress_DefaultsTo_0_0_0_0(self): reactor = CallTrace() acceptor = Acceptor(reactor, self.port, lambda sok: None, prio=5) self.assertEquals(('0.0.0.0', self.port), acceptor._sok.getsockname())
def observer_init(self): self._acceptor = Acceptor(reactor=self._reactor, port=self._port, sinkFactory=self._connect, bindAddress='127.0.0.1')