def tst_disable_default_socket_options(self): """Test that passing None disables all socket options.""" # This test needs to be here in order to be run. socket.create_connection actually tries to # connect to the host provided so we need a dummyserver to be running. pool = HTTPConnectionPool(self.host, self.port, socket_options=None) s = pool._new_conn()._new_conn() using_nagle = s.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY) == 0 self.assertTrue(using_nagle) s.close()
def tst_socket_options(self): """Test that connections accept socket options.""" # This test needs to be here in order to be run. socket.create_connection actually tries to # connect to the host provided so we need a dummyserver to be running. pool = HTTPConnectionPool(self.host, self.port, socket_options=[(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)]) s = pool._new_conn()._new_conn() # Get the socket using_keepalive = s.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE) > 0 self.assertTrue(using_keepalive) s.close()
def tst_socket_options(self): """Test that connections accept socket options.""" # This test needs to be here in order to be run. socket.create_connection actually tries to # connect to the host provided so we need a dummyserver to be running. pool = HTTPConnectionPool(self.host, self.port, socket_options=[ (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) ]) s = pool._new_conn()._new_conn() # Get the socket using_keepalive = s.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE) > 0 self.assertTrue(using_keepalive) s.close()
def tst_defaults_are_applied(self): """Test that modifying the default socket options works.""" # This test needs to be here in order to be run. socket.create_connection actually tries to # connect to the host provided so we need a dummyserver to be running. pool = HTTPConnectionPool(self.host, self.port) # Get the HTTPConnection instance conn = pool._new_conn() # Update the default socket options conn.default_socket_options += [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)] s = conn._new_conn() nagle_disabled = s.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY) > 0 using_keepalive = s.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE) > 0 self.assertTrue(nagle_disabled) self.assertTrue(using_keepalive)
def test_timeout(self): """ Requests should time out when expected """ url = '/sleep?seconds=0.002' timeout = Timeout(read=0.001) # Pool-global timeout pool = HTTPConnectionPool(self.host, self.port, timeout=timeout, retries=False) conn = pool._get_conn() yield From(self.aioAssertRaises(ReadTimeoutError, pool._make_request, conn, 'GET', url)) pool._put_conn(conn) time.sleep(0.02) # Wait for server to start receiving again. :( yield From(self.aioAssertRaises(ReadTimeoutError, pool.request, 'GET', url)) # Request-specific timeouts should raise errors pool = HTTPConnectionPool(self.host, self.port, timeout=0.1, retries=False) conn = pool._get_conn() yield From(self.aioAssertRaises(ReadTimeoutError, pool._make_request, conn, 'GET', url, timeout=timeout)) pool._put_conn(conn) time.sleep(0.02) # Wait for server to start receiving again. :( yield From(self.aioAssertRaises(ReadTimeoutError, pool.request, 'GET', url, timeout=timeout)) # Timeout int/float passed directly to request and _make_request should # raise a request timeout yield From(self.aioAssertRaises(ReadTimeoutError, pool.request, 'GET', url, timeout=0.001)) conn = pool._new_conn() yield From(self.aioAssertRaises(ReadTimeoutError, pool._make_request, conn, 'GET', url, timeout=0.001)) pool._put_conn(conn) # Timeout int/float passed directly to _make_request should not raise a # request timeout if it's a high value yield From(pool.request('GET', url, timeout=1))
def test_timeout(self): """ Requests should time out when expected """ url = "/sleep?seconds=0.002" timeout = Timeout(read=0.001) # Pool-global timeout pool = HTTPConnectionPool(self.host, self.port, timeout=timeout, retries=False) conn = pool._get_conn() yield From(self.aioAssertRaises(ReadTimeoutError, pool._make_request, conn, "GET", url)) pool._put_conn(conn) time.sleep(0.02) # Wait for server to start receiving again. :( yield From(self.aioAssertRaises(ReadTimeoutError, pool.request, "GET", url)) # Request-specific timeouts should raise errors pool = HTTPConnectionPool(self.host, self.port, timeout=0.1, retries=False) conn = pool._get_conn() yield From(self.aioAssertRaises(ReadTimeoutError, pool._make_request, conn, "GET", url, timeout=timeout)) pool._put_conn(conn) time.sleep(0.02) # Wait for server to start receiving again. :( yield From(self.aioAssertRaises(ReadTimeoutError, pool.request, "GET", url, timeout=timeout)) # Timeout int/float passed directly to request and _make_request should # raise a request timeout yield From(self.aioAssertRaises(ReadTimeoutError, pool.request, "GET", url, timeout=0.001)) conn = pool._new_conn() yield From(self.aioAssertRaises(ReadTimeoutError, pool._make_request, conn, "GET", url, timeout=0.001)) pool._put_conn(conn) # Timeout int/float passed directly to _make_request should not raise a # request timeout if it's a high value yield From(pool.request("GET", url, timeout=1))