def testServer_thread(self): daemon=ServerCallback() port=SU.findProbablyUnusedPort() serv=SocketServer_Threadpool() serv.init(daemon,"localhost",port) self.assertEqual("localhost:"+str(port), serv.locationStr) self.assertTrue(serv.sock is not None) conn=SU.SocketConnection(serv.sock, "ID12345") self.assertEqual("ID12345",conn.objectId) self.assertTrue(conn.sock is not None) conn.close() conn.close() self.assertFalse(conn.sock is None, "connections keep their socket object even if it's closed") serv.close() serv.close() self.assertTrue(serv.sock is None)
def __init__(self, host=None, port=0, unixsocket=None, nathost=None, natport=None): _check_hmac() # check if hmac secret key is set if host is None: host = Pyro4.config.HOST if nathost is None: nathost = Pyro4.config.NATHOST if natport is None: natport = Pyro4.config.NATPORT or None if nathost and unixsocket: raise ValueError("cannot use nathost together with unixsocket") if (nathost is None) ^ (natport is None): raise ValueError("must provide natport with nathost") if Pyro4.config.SERVERTYPE == "thread": self.transportServer = SocketServer_Threadpool() elif Pyro4.config.SERVERTYPE == "multiplex": # choose the 'best' multiplexing implementation if os.name == "java": raise NotImplementedError( "select or poll-based server is not supported for jython, use thread server instead" ) self.transportServer = SocketServer_Poll( ) if socketutil.hasPoll else SocketServer_Select() else: raise errors.PyroError("invalid server type '%s'" % Pyro4.config.SERVERTYPE) self.transportServer.init(self, host, port, unixsocket) #: The location (str of the form ``host:portnumber``) on which the Daemon is listening self.locationStr = self.transportServer.locationStr log.debug("created daemon on %s", self.locationStr) natport_for_loc = natport if natport == 0: # expose internal port number as NAT port as well. (don't use port because it could be 0 and will be chosen by the OS) natport_for_loc = int(self.locationStr.split(":")[1]) #: The NAT-location (str of the form ``nathost:natportnumber``) on which the Daemon is exposed for use with NAT-routing self.natLocationStr = "%s:%d" % (nathost, natport_for_loc) if nathost else None if self.natLocationStr: log.debug("NAT address is %s", self.natLocationStr) pyroObject = DaemonObject(self) pyroObject._pyroId = constants.DAEMON_NAME #: Dictionary from Pyro object id to the actual Pyro object registered by this id self.objectsById = {pyroObject._pyroId: pyroObject} self.__mustshutdown = threadutil.Event() self.__loopstopped = threadutil.Event() self.__loopstopped.set() # assert that the configured serializers are available, and remember their ids: self.__serializer_ids = set([ util.get_serializer(ser_name).serializer_id for ser_name in Pyro4.config.SERIALIZERS_ACCEPTED ]) log.debug("accepted serializers: %s" % Pyro4.config.SERIALIZERS_ACCEPTED)
def testServerPoolFull(self): port = socketutil.findProbablyUnusedPort() serv = SocketServer_Threadpool() daemon = ServerCallback() serv.init(daemon, "localhost", port) serversock = serv.sock.getsockname() csock1 = socketutil.createSocket(connect=serversock) csock2 = socketutil.createSocket(connect=serversock) try: serv.events([serv.sock]) time.sleep(0.2) self.assertEqual([None], daemon.received_denied_reasons) serv.events([serv.sock]) time.sleep(0.2) self.assertEqual(2, len(daemon.received_denied_reasons)) self.assertIn("no free workers, increase server threadpool size", daemon.received_denied_reasons) finally: csock1.close() csock2.close() serv.shutdown()
def testServerPoolFull(self): port = socketutil.findProbablyUnusedPort() serv = SocketServer_Threadpool() daemon = ServerCallback() serv.init(daemon, "localhost", port) serversock = serv.sock.getsockname() csock1 = socketutil.createSocket(connect=serversock) csock2 = socketutil.createSocket(connect=serversock) try: serv.events([serv.sock]) time.sleep(0.2) self.assertEqual([None], daemon.received_denied_reasons) serv.events([serv.sock]) time.sleep(0.2) self.assertEqual(2, len(daemon.received_denied_reasons)) self.assertIn("no free workers, increase server threadpool size", daemon.received_denied_reasons) finally: csock1.close() csock2.close() serv.shutdown()
def testServer_thread(self): daemon = ServerCallback() port = SU.findProbablyUnusedPort() serv = SocketServer_Threadpool() serv.init(daemon, "localhost", port) self.assertEqual("localhost:" + str(port), serv.locationStr) self.assertIsNotNone(serv.sock) conn = SU.SocketConnection(serv.sock, "ID12345") self.assertEqual("ID12345", conn.objectId) self.assertIsNotNone(conn.sock) conn.close() conn.close() self.assertIsNotNone(conn.sock, "connections keep their socket object even if it's closed") serv.close() serv.close() self.assertIsNone(serv.sock)