Esempio n. 1
0
    def test_multiple_hosts(self):
        pool = ConnectionPool(max_host_count=5, max_count=20)

        for port in range(10):
            session = yield From(pool.session('localhost', port))
            with session as connection:
                self.assertTrue(connection)
Esempio n. 2
0
    def test_multiple_hosts(self):
        pool = ConnectionPool(max_host_count=5, max_count=20)

        for port in range(10):
            session = yield From(
                pool.session('localhost', port)
            )
            with session as connection:
                self.assertTrue(connection)
Esempio n. 3
0
    def test_host_max_limit(self):
        pool = ConnectionPool(max_host_count=2)

        yield From(pool.acquire('localhost', self.get_http_port()))
        yield From(pool.acquire('localhost', self.get_http_port()))

        with self.assertRaises(trollius.TimeoutError):
            yield From(
                trollius.wait_for(
                    pool.acquire('localhost', self.get_http_port()), 0.1))
Esempio n. 4
0
    def test_host_max_limit(self):
        pool = ConnectionPool(max_host_count=2)

        yield From(pool.acquire('localhost', self.get_http_port()))
        yield From(pool.acquire('localhost', self.get_http_port()))

        with self.assertRaises(trollius.TimeoutError):
            yield From(trollius.wait_for(
                pool.acquire('localhost', self.get_http_port()),
                0.1
            ))
Esempio n. 5
0
    def test_clean(self):
        pool = ConnectionPool(max_host_count=2)

        conn1 = yield From(pool.acquire('localhost', self.get_http_port()))
        conn2 = yield From(pool.acquire('localhost', self.get_http_port()))

        yield From(pool.release(conn1))
        yield From(pool.release(conn2))
        yield From(pool.clean())

        self.assertEqual(0, len(pool.host_pools))
Esempio n. 6
0
    def test_clean(self):
        pool = ConnectionPool(max_host_count=2)

        conn1 = yield From(pool.acquire('localhost', self.get_http_port()))
        conn2 = yield From(pool.acquire('localhost', self.get_http_port()))

        yield From(pool.release(conn1))
        yield From(pool.release(conn2))
        yield From(pool.clean())

        self.assertEqual(0, len(pool.host_pools))
Esempio n. 7
0
    def test_basic_acquire(self):
        pool = ConnectionPool(max_host_count=2)

        conn1 = yield From(pool.acquire('localhost', self.get_http_port()))
        conn2 = yield From(pool.acquire('localhost', self.get_http_port()))

        yield From(pool.release(conn1))
        yield From(pool.release(conn2))

        conn3 = yield From(pool.acquire('localhost', self.get_http_port()))
        conn4 = yield From(pool.acquire('localhost', self.get_http_port()))

        yield From(pool.release(conn3))
        yield From(pool.release(conn4))
Esempio n. 8
0
    def test_session(self):
        pool = ConnectionPool()

        for dummy in range(10):
            session = yield From(
                pool.session('localhost', self.get_http_port()))
            with session as connection:
                if connection.closed():
                    yield From(connection.connect())

        self.assertEqual(1, len(pool.host_pools))
        host_pool = list(pool.host_pools.values())[0]
        self.assertIsInstance(host_pool, HostPool)
        self.assertEqual(1, host_pool.count())
Esempio n. 9
0
    def test_session(self):
        pool = ConnectionPool()

        for dummy in range(10):
            session = yield From(
                pool.session('localhost', self.get_http_port())
            )
            with session as connection:
                if connection.closed():
                    yield From(connection.connect())

        self.assertEqual(1, len(pool.host_pools))
        host_pool = list(pool.host_pools.values())[0]
        self.assertIsInstance(host_pool, HostPool)
        self.assertEqual(1, host_pool.count())
Esempio n. 10
0
    def __init__(self, connection_pool=None, recorder=None):
        '''
        Args:
            connection_pool (:class:`.connection.ConnectionPool`): Connection
                pool.
            recorder (:class:`.recorder.BaseRecorder`): Recorder.
            stream_factory: A function that returns a new
                :class:`.http.stream.Stream`.
        '''
        if connection_pool is not None:
            self._connection_pool = connection_pool
        else:
            self._connection_pool = ConnectionPool()

        self._recorder = recorder
Esempio n. 11
0
    def test_connection_pool_release_clean_race_condition(self):
        pool = ConnectionPool(max_host_count=1)

        connection = yield From(pool.acquire('127.0.0.1', 1234))
        connection_2_task = trollius. async (pool.acquire('127.0.0.1', 1234))
        yield From(trollius.sleep(0.01))
        pool.no_wait_release(connection)
        yield From(pool.clean(force=True))
        connection_2 = yield From(connection_2_task)

        # This line should not KeyError crash:
        yield From(pool.release(connection_2))
Esempio n. 12
0
    def test_connection_pool_release_clean_race_condition(self):
        pool = ConnectionPool(max_host_count=1)

        connection = yield From(pool.acquire('127.0.0.1', 1234))
        connection_2_task = trollius.async(pool.acquire('127.0.0.1', 1234))
        yield From(trollius.sleep(0.01))
        pool.no_wait_release(connection)
        yield From(pool.clean(force=True))
        connection_2 = yield From(connection_2_task)

        # This line should not KeyError crash:
        yield From(pool.release(connection_2))
Esempio n. 13
0
    def test_happy_eyeballs_prefer_ipv6(self):
        connection_factory = functools.partial(Connection, connect_timeout=10)
        resolver = Resolver(family=Resolver.PREFER_IPv6)
        pool = ConnectionPool(resolver=resolver,
                              connection_factory=connection_factory)

        conn1 = yield From(pool.acquire('google.com', 80))
        conn2 = yield From(pool.acquire('google.com', 80))

        yield From(conn1.connect())
        yield From(conn2.connect())
        conn1.close()
        conn2.close()

        yield From(pool.release(conn1))
        yield From(pool.release(conn2))

        conn3 = yield From(pool.acquire('google.com', 80))

        yield From(conn3.connect())
        conn3.close()

        yield From(pool.release(conn3))
Esempio n. 14
0
    def __init__(self, connection_pool=None, recorder=None):
        '''
        Args:
            connection_pool (:class:`.connection.ConnectionPool`): Connection
                pool.
            recorder (:class:`.recorder.BaseRecorder`): Recorder.
            stream_factory: A function that returns a new
                :class:`.http.stream.Stream`.
        '''
        if connection_pool is not None:
            self._connection_pool = connection_pool
        else:
            self._connection_pool = ConnectionPool()

        self._recorder = recorder
Esempio n. 15
0
    def test_over_host_max_limit_cycling(self):
        pool = ConnectionPool(max_host_count=10, max_count=10)

        @trollius.coroutine
        def con_fut():
            session = yield From(
                pool.session('localhost', self.get_http_port()))
            with session as connection:
                if connection.closed():
                    yield From(connection.connect())

        futs = [con_fut() for dummy in range(20)]

        yield From(trollius.wait(futs))

        self.assertEqual(1, len(pool.host_pools))
        connection_pool_entry = list(pool.host_pools.values())[0]
        self.assertIsInstance(connection_pool_entry, HostPool)
        self.assertGreaterEqual(10, connection_pool_entry.count())
Esempio n. 16
0
    def test_basic_acquire(self):
        pool = ConnectionPool(max_host_count=2)

        conn1 = yield From(pool.acquire('localhost', self.get_http_port()))
        conn2 = yield From(pool.acquire('localhost', self.get_http_port()))

        yield From(pool.release(conn1))
        yield From(pool.release(conn2))

        conn3 = yield From(pool.acquire('localhost', self.get_http_port()))
        conn4 = yield From(pool.acquire('localhost', self.get_http_port()))

        yield From(pool.release(conn3))
        yield From(pool.release(conn4))
Esempio n. 17
0
    def test_client_exception_recovery(self):
        connection_factory = functools.partial(Connection, timeout=2.0)
        connection_pool = ConnectionPool(connection_factory=connection_factory)
        client = Client(connection_pool=connection_pool)

        for dummy in range(7):
            with client.session() as session:
                request = Request(self.get_url('/header_early_close'))
                try:
                    yield From(session.fetch(request))
                except NetworkError:
                    pass
                else:
                    self.fail()  # pragma: no cover

        for dummy in range(7):
            with client.session() as session:
                request = Request(self.get_url('/'))
                response = yield From(session.fetch(request))
                self.assertEqual(200, response.status_code)
                yield From(session.read_content())
                self.assertTrue(session.done())
Esempio n. 18
0
    def test_happy_eyeballs_prefer_ipv6(self):
        connection_factory = functools.partial(Connection, connect_timeout=10)
        resolver = Resolver(family=Resolver.PREFER_IPv6)
        pool = ConnectionPool(resolver=resolver,
                              connection_factory=connection_factory)

        conn1 = yield From(pool.acquire('google.com', 80))
        conn2 = yield From(pool.acquire('google.com', 80))

        yield From(conn1.connect())
        yield From(conn2.connect())
        conn1.close()
        conn2.close()

        yield From(pool.release(conn1))
        yield From(pool.release(conn2))

        conn3 = yield From(pool.acquire('google.com', 80))

        yield From(conn3.connect())
        conn3.close()

        yield From(pool.release(conn3))
Esempio n. 19
0
class BaseClient(object, metaclass=abc.ABCMeta):
    '''Base client.'''
    def __init__(self, connection_pool=None, recorder=None):
        '''
        Args:
            connection_pool (:class:`.connection.ConnectionPool`): Connection
                pool.
            recorder (:class:`.recorder.BaseRecorder`): Recorder.
            stream_factory: A function that returns a new
                :class:`.http.stream.Stream`.
        '''
        if connection_pool is not None:
            self._connection_pool = connection_pool
        else:
            self._connection_pool = ConnectionPool()

        self._recorder = recorder

    @abc.abstractmethod
    def _session_class(self):
        '''Return session class.'''
        return BaseSession  # return something for code checkers

    @contextlib.contextmanager
    def session(self):
        '''Return a new session.

        Returns:
            BaseSession.

        Context manager: This function is meant be used with the ``with``
        statement.
        '''
        if self._recorder:
            context_manager = self._recorder.session()
        else:
            context_manager = dummy_context_manager()

        with context_manager as recorder_session:
            session = self._session_class()(
                connection_pool=self._connection_pool,
                recorder_session=recorder_session,
            )
            try:
                yield session
            except Exception as error:
                if not isinstance(error, StopIteration):
                    _logger.debug('Early close session.')
                    session.abort()
                    session.recycle()
                raise
            else:
                session.recycle()

    def close(self):
        '''Close the connection pool and recorders.'''
        _logger.debug('Client closing.')
        self._connection_pool.close()

        if self._recorder:
            self._recorder.close()
Esempio n. 20
0
class BaseClient(object, metaclass=abc.ABCMeta):
    '''Base client.'''
    def __init__(self, connection_pool=None, recorder=None):
        '''
        Args:
            connection_pool (:class:`.connection.ConnectionPool`): Connection
                pool.
            recorder (:class:`.recorder.BaseRecorder`): Recorder.
            stream_factory: A function that returns a new
                :class:`.http.stream.Stream`.
        '''
        if connection_pool is not None:
            self._connection_pool = connection_pool
        else:
            self._connection_pool = ConnectionPool()

        self._recorder = recorder

    @abc.abstractmethod
    def _session_class(self):
        '''Return session class.'''
        return BaseSession  # return something for code checkers

    @contextlib.contextmanager
    def session(self):
        '''Return a new session.

        Returns:
            BaseSession.

        Context manager: This function is meant be used with the ``with``
        statement.
        '''
        if self._recorder:
            context_manager = self._recorder.session()
        else:
            context_manager = dummy_context_manager()

        with context_manager as recorder_session:
            session = self._session_class()(
                connection_pool=self._connection_pool,
                recorder_session=recorder_session,
            )
            try:
                yield session
            except Exception as error:
                if not isinstance(error, StopIteration):
                    _logger.debug('Early close session.')
                    session.abort()
                    session.recycle()
                raise
            else:
                session.recycle()

    def close(self):
        '''Close the connection pool and recorders.'''
        _logger.debug('Client closing.')
        self._connection_pool.close()

        if self._recorder:
            self._recorder.close()