def _set_debug_level_and_connect(): set_debug_level(10) handler = ProcessConnectionHandler() with ServerWorker(run_tcp, localhost, 0, handler=handler) as worker: with Connection.from_tcp(*worker.address) as conn: if conn.call(get_debug_level) != 10: raise AssertionError
def boot_connection(export=top_name, cwd=None): if cwd is None: cwd = mktemp() os.mkdir(cwd) chan = ProcessChannel([ sys.executable, '-c', get_bios(), ], cwd=cwd) conn = Connection(chan) if export is None: export = [] elif isinstance(export, str): export = [export] conn.export(*export) return conn
def test_invalid_timeout_type(self): for timeout in [ [], '12.0', ]: with self.assertRaises(TypeError): Connection(EOFChannel(), options=Options(timeout=timeout))
def test_connect_exception(self): chan = FailingChannel() chan.connect_future.set_exception(FirstError()) chan.close_future.set_result(None) with self.assertRaises(FirstError): with Connection(chan): pass
def test_no_idle_messages(self): timeout = None TimeoutableQueue.last = None chan = ByteCountingConnectionChannel() remote_sender_queue = TimeoutableQueue.last with Connection(chan, options=Options(timeout=timeout)) as conn: conn.call(nop) # ensure handshake is done self.assertIsNone(remote_sender_queue.get_last_get_timeout())
def __run_stopping_test(self, handler): with ServerWorker(run_tcp, localhost, 0, handler=handler) as worker: conn = Connection.from_tcp(*worker.address) self.assertEqual(conn.call(add, 1, 2), 3) self.assertRaises(DisconnectError, conn.call, add, 1, 2) try: conn.close() except OSError: # Connection reset by peer on Windows pass
def test_channel_close_exception_is_ignored_after_previous_exception(self): chan = FailingChannel() chan.connect_future.set_result(None) chan.send_future.set_result(None) chan.recv_future.set_exception(FirstError()) chan.close_future.set_exception(SecondError()) with self.assertRaises(FirstError): with Connection(chan) as conn: conn.wait()
def test_invalid_timeout_value(self): for timeout in [ -1, -1.0, float('nan'), float('-inf'), ]: with self.assertRaises(ValueError): Connection(EOFChannel(), options=Options(timeout=timeout))
def test_remote_close_during_call(self): chan, rchan = make_channel_pair() conn = Connection(chan) Connection(rchan).close() with self.assertRaisesRegex(DisconnectError, 'Connection was remotely closed'): conn.call(nop) conn.close()
def test_bios_handler(self): handler = BIOSConnectionHandler() # Child process may be interrupted during interpreter shutdown, causing various errors to be # printed on stderr. Let's suppress them. with InheritableStderrCollector(): with ServerWorker(run_tcp, localhost, 0, handler=handler) as worker: with Connection.from_tcp(*worker.address) as conn: self.assertEqual(conn.call(add, 1, 2), 3) self.assertNotEqual(conn.call(os.getpid), os.getpid())
def test_enter_after_cancel(self): # There are no writes to chan2, so handshake should never complete chan1, chan2 = make_channel_pair() conn = Connection(chan1) conn.cancel() with self.assertRaises(ValueError): conn.__enter__()
def test_multiple_connections(self): with ServerWorker(run_tcp, localhost, 0) as worker: num_conns = 5 conns = [ Connection.from_tcp(*worker.address) for _ in range(num_conns) ] for i, conn in enumerate(conns): self.assertEqual(conn.call(add, i, 1), i + 1) for conn in conns: conn.close()
def test_communication_error_during_call(self): chan = ConnectionChannel() conn = Connection(chan) try: with self.assertRaisesRegex(DisconnectError, 'Connection was aborted due to .*'): conn.call(Box(chan.cancel) ) # simulate communication error with channel.cancel with self.assertRaisesRegex(DisconnectError, 'Connection was aborted due to .*'): conn.call(nop) finally: try: conn.close() except Exception: pass
def test_idle_messages(self): timeout = 10 TimeoutableQueue.last = None chan = ByteCountingConnectionChannel() remote_sender_queue = TimeoutableQueue.last with Connection(chan, options=Options(timeout=timeout)) as conn: conn.call(nop) # ensure handshake is done for _ in range(10): recv_bytes = chan.get_recv_bytes() remote_sender_queue.simulate_timeout() chan.wait_recv_bytes_changed(recv_bytes) last_get_timeout = remote_sender_queue.get_last_get_timeout() self.assertIsNotNone(last_get_timeout) self.assertLess(last_get_timeout, timeout * 0.75) self.assertGreater(last_get_timeout, timeout * 0.1)
def _run_export_over_multiple_connections_test(): options = Options(accept_exported_modules=True) conn1 = Connection(make_channel_pair()[0], options=options) try: Connection(make_channel_pair()[0], options=options) except ValueError: pass else: raise AssertionError('ValueError not raised') conn1.cancel() try: conn1.close() except Exception: # generated by cancel() pass
def test_from_command(self): with Connection.from_command(base_args) as conn: self.assertEqual(conn.call(operator.add, 2, 2), 4)
def test_from_shell(self): with Connection.from_shell(' '.join(map(shell_quote, base_args))) as conn: self.assertEqual(conn.call(operator.add, 2, 2), 4)
def test_from_process(self): proc = subprocess.Popen(base_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE) with Connection.from_process(proc) as conn: self.assertEqual(conn.call(operator.add, 2, 2), 4)
def test_large_timeout(self): with Connection(ConnectionChannel(), options=Options(timeout=1.0e100)) as conn: conn.call(nop)
def test_valid_timeouts(self): for timeout in [10, 10.0]: with Connection(ConnectionChannel(), options=Options(timeout=timeout)) as conn: conn.call(nop)
def _run_shutdown_with_unresponsive_connections_test(): Connection(make_channel_pair()[0], options=Options(timeout=1e100))
def test_enter_when_connect_fails(self): conn = Connection(UnconnectableChannel()) with self.assertRaises(ConnectError): conn.__enter__() with self.assertRaisesRegex(ValueError, 'Connection is closed'): conn.__enter__()
def test_unix(self): path = mktemp() with ServerWorker(run_unix, path): with Connection.from_unix(path) as conn: self.assertEqual(conn.call(add, 1, 2), 3)
def test_exit_after_disconnect(self): chan = ConnectionChannel() with self.assertRaises(ValueError): with Connection(chan) as conn: conn.call(Box(chan.cancel))
def test_tcp_dual_stack(self): with ServerWorker(run_tcp, anyaddr_ipv6, 0) as worker: with Connection.from_tcp(localhost, worker.address[1]) as conn: self.assertEqual(conn.call(add, 1, 2), 3)
def test_tcp_ipv6(self): with ServerWorker(run_tcp, localhost_ipv6, 0) as worker: with Connection.from_tcp(*worker.address[:2]) as conn: self.assertEqual(conn.call(add, 1, 2), 3)
def test_enter_after_remote_close(self): chan1, chan2 = make_channel_pair() conn1, conn2 = Connection(chan1), Connection(chan2) conn2.close() with conn1: pass
def make_connection(self, handler): sock1, sock2 = make_tcp_socket_pair() conn = Connection.from_socket(sock1) handler.handle_socket(sock2) return conn
def test_from_command_no_action(self): with Connection.from_command(base_args): pass