def test_default_pool(self, pool=None): """ Tests the default pool """ # Setup server server = PooledJSONRPCServer(("localhost", 0), thread_pool=pool) server.register_function(add) # Serve in a thread thread = threading.Thread(target=server.serve_forever) thread.daemon = True thread.start() # Find its port port = server.socket.getsockname()[1] # Make the client client = ServerProxy("http://localhost:{0}".format(port)) # Check calls for _ in range(5): a, b = random.random(), random.random() result = client.add(a, b) self.assertEqual(result, a + b) # Close server server.shutdown() server.server_close() thread.join()
def test_default_pool(self, pool=None): """ Tests the default pool """ # Setup server server = PooledJSONRPCServer(("localhost", 0), thread_pool=pool) server.register_function(add) # Serve in a thread thread = threading.Thread(target=server.serve_forever) thread.daemon = True thread.start() # Find its port port = server.socket.getsockname()[1] # Make the client client = ServerProxy("http://localhost:{0}".format(port)) # Check calls for _ in range(10): a, b = random.random(), random.random() result = client.add(a, b) self.assertEqual(result, a + b) # Close server server.server_close() thread.join()
def test_server(self): """ Tests the CGI request handler """ # Move the parent directory of "cgi-bin" old_dir = os.getcwd() try: # Setup server os.chdir(os.path.dirname(__file__)) server = HTTPServer(("localhost", 0), CGIHTTPRequestHandler) # Serve in a thread thread = threading.Thread(target=server.serve_forever) thread.daemon = True thread.start() # Find its port port = server.socket.getsockname()[1] # Make the client client = ServerProxy( "http://localhost:{0}/cgi-bin/cgi_server.py".format(port)) # Check call for _ in range(2): a, b = random.random(), random.random() result = client.add(a, b) self.assertEqual(result, a + b) # Close server server.shutdown() server.server_close() thread.join() finally: os.chdir(old_dir)
def test_default_pool(self, pool=None, max_time=3): """ Tests the given or the default pool :param pool: Thread pool to use :param max_time: Max time the sleep test should take """ # Setup server server = PooledJSONRPCServer(("localhost", 0), thread_pool=pool) server.register_function(add) server.register_function(sleep) # Serve in a thread thread = threading.Thread(target=server.serve_forever) thread.daemon = True thread.start() try: # Find its port port = server.socket.getsockname()[1] # Make the client target_url = "http://localhost:{0}".format(port) client = ServerProxy(target_url) # Check calls for _ in range(5): rand1, rand2 = random.random(), random.random() result = client.add(rand1, rand2) self.assertEqual(result, rand1 + rand2) # Check pauses (using different clients) threads = [threading.Thread(target=ServerProxy( target_url).sleep, args=(1,)) for _ in range(5)] start_time = time.time() for thread in threads: thread.start() for thread in threads: thread.join() end_time = time.time() self.assertLessEqual(end_time - start_time, max_time) finally: # Close server server.shutdown() server.server_close() thread.join()
def test_host_only(self): """ Starts a Unix socket server, giving with a relative path to the socket """ # Ensure we have a new socket socket_name = "test_local.socket" if os.path.exists(socket_name): os.remove(socket_name) # Use a random int as result awaited_result = random.randint(1, 100) try: # Prepare the server srv = SimpleJSONRPCServer(socket_name, address_family=socket.AF_UNIX) srv.register_function(lambda: awaited_result, "test") # Run the server in a thread thread = threading.Thread(target=srv.serve_forever) thread.start() try: # Run the request client = ServerProxy("unix+http://{}".format(socket_name)) result = client.test() self.assertEqual(result, awaited_result) finally: # Stop the server srv.shutdown() srv.server_close() thread.join(5) finally: # Clean up try: os.remove(socket_name) except: pass
def __init__(self, url, tryton_admin_password): self._tryton_admin_password = tryton_admin_password self._server = ServerProxy(url, verbose=0)