def __server_setup_and_call(self, method, http_enc=True, use_proc=True, **kwargs): """Setup an rpc server and make a call to it. All calls are made asynchronously.""" server_proc, client_rpc = self.__server_setup(http_enc=http_enc, use_proc=use_proc) method_cb = getattr(client_rpc, method) ac = AsyncCall() ac.start(method_cb, **kwargs) # Destroying all references to the client object should close # the client end of our pipe to the server, which in turn # should cause the server to cleanly exit. If we hang waiting # for the server to exist then that's a bug. del method_cb, client_rpc try: rv = ac.result() except AsyncCallException as ex: # we explicity delete the client rpc object to try and # ensure that any connection to the server process # gets closed (so that the server process exits). server_proc.join() raise server_proc.join() return rv
def __test_rpc_interruptions(self, http_enc): # sanity check rpc sleep call rv = self.__server_setup_and_call("sleep", n=0, http_enc=http_enc) # test interrupted rpc calls by killing the server for i in range(10): server_proc, client_rpc = self.__server_setup(http_enc=http_enc) ac = AsyncCall() method = getattr(client_rpc, "sleep") ac.start(method, n=10000) del method, client_rpc # add optional one second delay so that we can try # kill before and after the call has been started. time.sleep(random.randint(0, 1)) # vary how we kill the target if random.randint(0, 1) == 1: server_proc.terminate() else: os.kill(server_proc.pid, signal.SIGKILL) self.assertRaises(AsyncCallException, ac.result) server_proc.join()
def test_async_thread_errors(self): # test exceptions raised in the AsyncCall class DebugValues["async_thread_error"] = 1 ac = AsyncCall() ac.start(self.__nop) self.assertRaisesRegexp(AsyncCallException, "async_thread_error", ac.result)
def __test_rpc_interruptions(self, http_enc): # sanity check rpc sleep call rv = self.__server_setup_and_call("sleep", n=0, http_enc=http_enc) # test interrupted rpc calls by killing the server for i in range(10): server_proc, client_rpc = self.__server_setup( http_enc=http_enc) ac = AsyncCall() method = getattr(client_rpc, "sleep") ac.start(method, n=10000) del method, client_rpc # add optional one second delay so that we can try # kill before and after the call has been started. time.sleep(random.randint(0, 1)) # vary how we kill the target if random.randint(0, 1) == 1: server_proc.terminate() else: os.kill(server_proc.pid, signal.SIGKILL) self.assertRaises(AsyncCallException, ac.result) server_proc.join()
def __server_setup_and_call(self, method, http_enc=True, use_proc=True, **kwargs): """Setup an rpc server and make a call to it. All calls are made asynchronously.""" server_proc, client_rpc = self.__server_setup( http_enc=http_enc, use_proc=use_proc) method_cb = getattr(client_rpc, method) ac = AsyncCall() ac.start(method_cb, **kwargs) # Destroying all references to the client object should close # the client end of our pipe to the server, which in turn # should cause the server to cleanly exit. If we hang waiting # for the server to exist then that's a bug. del method_cb, client_rpc try: rv = ac.result() except AsyncCallException, ex: # we explicity delete the client rpc object to try and # ensure that any connection to the server process # gets closed (so that the server process exits). server_proc.join() raise
def test_async_basics(self): # test a simple async call with no parameters ac = AsyncCall() ac.start(self.__nop) ac.result() # test a simple async call with positional parameters ac = AsyncCall() ac.start(self.__add, 1, 2) rv = ac.result() self.assertEqual(rv, 3) # test a simple async call with keyword parameters ac = AsyncCall() ac.start(self.__add, x=1, y=2) rv = ac.result() self.assertEqual(rv, 3) # test async call with invalid arguments ac = AsyncCall() ac.start(self.__add, 1, 2, 3) if six.PY2: self.assertRaisesRegexp(AsyncCallException, "takes exactly 2 arguments", ac.result) else: self.assertRaisesRegexp(AsyncCallException, "takes 2 positional arguments", ac.result) ac = AsyncCall() ac.start(self.__add, x=1, y=2, z=3) self.assertRaisesRegexp(AsyncCallException, "got an unexpected keyword argument", ac.result) ac = AsyncCall() ac.start(self.__add, y=2, z=3) self.assertRaisesRegexp(AsyncCallException, "got an unexpected keyword argument", ac.result)
def test_async_basics(self): # test a simple async call with no parameters ac = AsyncCall() ac.start(self.__nop) ac.result() # test a simple async call with positional parameters ac = AsyncCall() ac.start(self.__add, 1, 2) rv = ac.result() self.assertEqual(rv, 3) # test a simple async call with keyword parameters ac = AsyncCall() ac.start(self.__add, x=1, y=2) rv = ac.result() self.assertEqual(rv, 3) # test async call with invalid arguments ac = AsyncCall() ac.start(self.__add, 1, 2, 3) self.assertRaisesRegexp(AsyncCallException, "takes exactly 2 arguments", ac.result) ac = AsyncCall() ac.start(self.__add, x=1, y=2, z=3) self.assertRaisesRegexp(AsyncCallException, "got an unexpected keyword argument", ac.result) ac = AsyncCall() ac.start(self.__add, y=2, z=3) self.assertRaisesRegexp(AsyncCallException, "got an unexpected keyword argument", ac.result)