def test_constructor_with_unreachable_addresses(self): addr = Address("192.168.0.1", 5701) config = _Config() start = time.time() conn = AsyncoreConnection(MagicMock(map=dict()), MagicMock(), None, addr, config, None) try: # Server is unreachable, but this call should return # before connection timeout self.assertLess(time.time() - start, config.connection_timeout) finally: conn.close(None, None)
def test_socket_options(self): config = _Config() config.socket_options = [ (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ] conn = AsyncoreConnection(MagicMock(map=dict()), None, None, self.member.address, config, None) try: # By default this is set to 0 self.assertEqual(1, conn.socket.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)) finally: conn._inner_close()
def test_send_buffer_size(self): # When the SO_SNDBUF option is set, we should try # to use that value while trying to write something. config = _Config() size = 64 * 1024 config.socket_options = [(socket.SOL_SOCKET, socket.SO_SNDBUF, size)] conn = AsyncoreConnection(MagicMock(map=dict()), None, None, self.member.address, config, None) try: # By default this is set to 128000 self.assertEqual(size, conn.send_buffer_size) finally: conn._inner_close()
def test_resources_cleaned_up_after_immediate_failure(self): addr = Address("invalid-address", 5701) config = _Config() mock_reactor = MagicMock(map={}) try: conn = AsyncoreConnection(mock_reactor, MagicMock(), None, addr, config, None) conn.close_connection(None, None) self.fail( "Connection attempt to an invalid address should fail immediately" ) except socket.error: # Constructor of the connection should remove itself from the # dispatchers map of the reactor. self.assertEqual(0, len(mock_reactor.map))
def test_receive_buffer_size(self): # When the SO_RCVBUF option is set, we should try # to use that value while trying to read something. self.server = MockServer() config = _Config() size = 64 * 1024 config.socket_options = [(socket.SOL_SOCKET, socket.SO_RCVBUF, size)] conn = AsyncoreConnection(MagicMock(map=dict()), None, None, self.server.get_address(), config, None) try: # By default this is set to 128000 self.assertEqual(size, conn.receive_buffer_size) finally: conn._inner_close()