def test_start_stop(self, monkeypatch): _mmanager = multiprocessing.Manager() shqs = [_mmanager.Queue(), _mmanager.Queue()] sh = TransportTestServerHandler("calvinip://localhost", shqs[0], shqs[1], timeout=2) ttf_uuid = str(uuid.uuid4()) ttf = calvinip_transport.CalvinTransportFactory( ttf_uuid, ttf_uuid, sh.get_callbacks()) sh.set_ttf(ttf) sh.start() error = None try: while sh.is_alive(): try: mess = shqs[0].get(timeout=.3) # print(mess) except: continue if mess[0] == 'timeout': print(mess[1]) raise Exception("Timeout: %s" % "\n".join(mess[1][11:])) elif mess[0] == 'server_started': pass shqs[1].put(['stop', [], {}]) elif mess[0] == 'server_stopped': break else: # print mess if not mess[0]: for a in mess[1]: print a, for k, v in mess[2].items(): print "%s = %s" % (k, repr(v)) raise Exception("\n".join(mess[1][11:])) except Exception as e: import traceback traceback.print_exc() error = e shqs[1].put(['stop', [], {}]) sh.join(timeout=.2) slay([sh]) if error: pytest.fail(error)
def test_start_stop(self, monkeypatch): shqs = [self._mmanager.Queue(), self._mmanager.Queue()] sh = ServerHandler("calvinip://localhost", shqs[0], shqs[1]) ttf = calvinip_transport.CalvinTransportFactory(str(uuid.uuid4()), sh.get_callbacks()) sh.set_ttf(ttf) sh.start() error = None try: while sh.is_alive(): try: mess = shqs[0].get(timeout=.3) #print(mess) except: continue if mess[0] == 'timeout': print(mess[1]) raise Exception("Timeout: %s" % "\n".join(mess[1][11:])) elif mess[0] == 'server_started': shqs[1].put(['stop', [], {}]) elif mess[0] == 'server_stopped': break else: #print mess if not mess[0]: for a in mess[1]: print a, for k,v in mess[2].items(): print "%s = %s" % (k, repr(v)) raise Exception("\n".join(mess[1][11:])) except Exception as e: error = e shqs[1].put(['stop', [], {}]) sh.join(timeout=.2) if sh.is_alive(): sh.terminate() if error: pytest.fail(error)
def test_connect(self, monkeypatch): queues = [] shqs = [self._mmanager.Queue(), self._mmanager.Queue()] chqs = [self._mmanager.Queue(), self._mmanager.Queue()] sh = ServerHandler("calvinip://127.0.0.1", shqs[0], shqs[1]) ch = ClientHandler("calvinip://127.0.0.1", chqs[0], chqs[1]) ttfs = calvinip_transport.CalvinTransportFactory(str(uuid.uuid4()), sh.get_callbacks()) ttfc = calvinip_transport.CalvinTransportFactory(str(uuid.uuid4()), ch.get_callbacks()) sh.set_ttf(ttfs) ch.set_ttf(ttfc) sh.start() #ch.start() queues = [shqs, chqs] cstop = sstop = False stop = False error = None try: while not stop: for q in queues: try: mess = q[0].get(timeout=.1) #print(mess[0]) except: continue if mess[0] == 'timeout': print(mess[1]) # TODO: terminate raise Exception("Timeout: %s" % "\n".join(mess[1][11:])) elif mess[0] == 'server_stopped': print "Hej hej" sstop = True stop = (sstop and cstop) elif mess[0] == 'server_started': ch.set_port(mess[2]) ch.start() elif mess[0] == 'client_disconnected': cstop = True stop = (sstop and cstop) elif mess[0] == 'client_join_finished': stop = True else: #print mess if not mess[0]: for a in mess[1][11:-1]: print a, for k,v in mess[2].items(): print "%s = %s" % (k, repr(v)) raise Exception("\n".join(mess[1][11:])) except Exception as e: error = e for tq in queues: print(repr(tq)) tq[1].put(['stop', [], {}]) print sh.join(timeout=.5) print ch.join(timeout=.5) if sh.is_alive(): sh.terminate() if ch.is_alive(): ch.terminate() if error: pytest.fail(error)
def test_connect_fail(self, monkeypatch): _mmanager = multiprocessing.Manager() queues = [] shqs = [_mmanager.Queue(), _mmanager.Queue()] chqs = [_mmanager.Queue(), _mmanager.Queue()] sh = TransportTestServerHandler("calvinip://127.0.0.1", shqs[0], shqs[1]) ch = TransportTestClientHandler("calvinip://127.0.0.1", chqs[0], chqs[1]) ttfs_uuid = str(uuid.uuid4()) ttfs = calvinip_transport.CalvinTransportFactory( ttfs_uuid, ttfs_uuid, sh.get_callbacks()) ttfc_uuid = str(uuid.uuid4()) ttfc = calvinip_transport.CalvinTransportFactory( ttfc_uuid, ttfc_uuid, ch.get_callbacks()) sh.set_ttf(ttfs) ch.set_ttf(ttfc) sh.start() #ch.start() queues = [shqs, chqs] cstop = sstop = False stop = False error = None try: while not stop: for q in queues: try: mess = q[0].get(timeout=.1) #print(mess[0]) except: continue if mess[0] == 'timeout': print(mess[1]) # TODO: terminate raise Exception("Timeout: %s" % "\n".join(mess[1][11:])) elif mess[0] == 'server_stopped': print "Hej hej" sstop = True stop = (sstop and cstop) elif mess[0] == 'server_started': ch.set_port(str(int(mess[2]) + 1)) ch.start() elif mess[0] == 'client_disconnected': cstop = True stop = (sstop and cstop) elif mess[0] == 'client_join_finished': stop = True elif mess[0] == 'client_join_failed': raise ClientJoinFailed(str(mess[2])) elif mess[0] == 'server_join_failed': raise ServerJoinFailed(str(mess[2])) elif mess[0] == 'client_connection_failed': raise ConnectionFailed(str(mess[2])) else: # print mess if not mess[0]: for a in mess[1][11:-1]: print a, for k, v in mess[2].items(): print "%s = %s" % (k, repr(v)) raise Exception("\n".join(mess[1][11:])) except Exception as e: error = e for tq in queues: print "hej", repr(tq) tq[1].put(['stop', [], {}]) print sh, ch slay([sh, ch]) if error: with pytest.raises(ConnectionFailed): import traceback traceback.print_exc(error) raise error else: pytest.fail("No exception")