|
| 1 | +# Copyright 2016-2024. Couchbase, Inc. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from datetime import timedelta |
| 19 | +from typing import TYPE_CHECKING |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from acouchbase_analytics.cluster import AsyncCluster |
| 24 | +from acouchbase_analytics.credential import Credential |
| 25 | +from acouchbase_analytics.errors import AnalyticsError, TimeoutError |
| 26 | +from acouchbase_analytics.options import QueryOptions |
| 27 | +from tests import AsyncYieldFixture |
| 28 | + |
| 29 | +if TYPE_CHECKING: |
| 30 | + from tests.environments.base_environment import AsyncTestEnvironment |
| 31 | + |
| 32 | + |
| 33 | +class ConnectTestSuite: |
| 34 | + TEST_MANIFEST = [ |
| 35 | + 'test_connect_timeout_max_retry_limit', |
| 36 | + 'test_connect_timeout_query_timeout', |
| 37 | + ] |
| 38 | + |
| 39 | + async def test_connect_timeout_max_retry_limit(self, test_env: AsyncTestEnvironment) -> None: |
| 40 | + statement = 'SELECT sleep("some value", 10000) AS some_field;' |
| 41 | + |
| 42 | + username, pw = test_env.config.get_username_and_pw() |
| 43 | + cred = Credential.from_username_and_password(username, pw) |
| 44 | + # ignoring the port enables the failure |
| 45 | + connstr = test_env.config.get_connection_string(ignore_port=True) |
| 46 | + cluster = AsyncCluster.create_instance(connstr, cred) |
| 47 | + |
| 48 | + allowed_retries = 5 |
| 49 | + q_opts = QueryOptions(max_retries=allowed_retries, timeout=timedelta(seconds=10)) |
| 50 | + with pytest.raises(AnalyticsError) as ex: |
| 51 | + await cluster.execute_query(statement, q_opts) |
| 52 | + |
| 53 | + assert ex.value._message is not None |
| 54 | + assert 'Retry limit exceeded' in ex.value._message |
| 55 | + test_env.assert_error_context_num_attempts(allowed_retries + 1, ex.value._context) |
| 56 | + |
| 57 | + async def test_connect_timeout_query_timeout(self, test_env: AsyncTestEnvironment) -> None: |
| 58 | + statement = 'SELECT sleep("some value", 10000) AS some_field;' |
| 59 | + |
| 60 | + username, pw = test_env.config.get_username_and_pw() |
| 61 | + cred = Credential.from_username_and_password(username, pw) |
| 62 | + # ignoring the port enables the failure |
| 63 | + connstr = test_env.config.get_connection_string(ignore_port=True) |
| 64 | + cluster = AsyncCluster.create_instance(connstr, cred) |
| 65 | + |
| 66 | + q_opts = QueryOptions(timeout=timedelta(seconds=3)) |
| 67 | + with pytest.raises(TimeoutError) as ex: |
| 68 | + await cluster.execute_query(statement, q_opts) |
| 69 | + |
| 70 | + assert ex.value._message is not None |
| 71 | + assert 'Request timed out during retry delay' in ex.value._message |
| 72 | + test_env.assert_error_context_num_attempts(2, ex.value._context, exact=False) |
| 73 | + |
| 74 | + |
| 75 | +class ConnectTests(ConnectTestSuite): |
| 76 | + @pytest.fixture(scope='class', autouse=True) |
| 77 | + def validate_test_manifest(self) -> None: |
| 78 | + def valid_test_method(meth: str) -> bool: |
| 79 | + attr = getattr(ConnectTests, meth) |
| 80 | + return callable(attr) and not meth.startswith('__') and meth.startswith('test') |
| 81 | + |
| 82 | + method_list = [meth for meth in dir(ConnectTests) if valid_test_method(meth)] |
| 83 | + test_list = set(ConnectTestSuite.TEST_MANIFEST).symmetric_difference(method_list) |
| 84 | + if test_list: |
| 85 | + pytest.fail(f'Test manifest invalid. Missing/extra tests: {test_list}.') |
| 86 | + |
| 87 | + @pytest.fixture(scope='class', name='test_env') |
| 88 | + async def couchbase_test_environment( |
| 89 | + self, async_test_env: AsyncTestEnvironment |
| 90 | + ) -> AsyncYieldFixture[AsyncTestEnvironment]: |
| 91 | + await async_test_env.setup() |
| 92 | + yield async_test_env |
| 93 | + await async_test_env.teardown() |
0 commit comments