Skip to content

Commit

Permalink
Add connection error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Oct 10, 2023
1 parent 6808b70 commit 3ac1ba0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
8 changes: 4 additions & 4 deletions reportportal_client/aio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
# noinspection PyProtectedMember
from reportportal_client._internal.aio.http import RetryingClientSession
# noinspection PyProtectedMember
from reportportal_client._internal.local import set_current
# noinspection PyProtectedMember
from reportportal_client._internal.aio.tasks import (BatchedTaskFactory, ThreadedTaskFactory,
TriggerTaskBatcher, BackgroundTaskList,
DEFAULT_TASK_TRIGGER_NUM, DEFAULT_TASK_TRIGGER_INTERVAL)
# noinspection PyProtectedMember
from reportportal_client._internal.local import set_current
# noinspection PyProtectedMember
from reportportal_client._internal.logs.batcher import LogBatcher
# noinspection PyProtectedMember
from reportportal_client._internal.services.statistics import async_send_event
Expand Down Expand Up @@ -443,12 +443,12 @@ async def get_launch_info(self, launch_uuid_future: Union[str, Task[str]]) -> Op
response = await AsyncHttpRequest((await self.session()).get, url=url).make()
if not response:
return
launch_info = None
if response.is_success:
launch_info = await response.json
logger.debug('get_launch_info - Launch info: %s', launch_info)
else:
logger.warning('get_launch_info - Launch info: Failed to fetch launch ID from the API.')
launch_info = {}
return launch_info

async def __get_item_uuid_url(self, item_uuid_future: Union[str, Task[str]]) -> Optional[str]:
Expand All @@ -466,7 +466,7 @@ async def get_item_id_by_uuid(self, item_uuid_future: Union[str, Task[str]]) ->
"""
url = self.__get_item_uuid_url(item_uuid_future)
response = await AsyncHttpRequest((await self.session()).get, url=url).make()
return response.id if response else None
return await response.id if response else None

async def get_launch_ui_id(self, launch_uuid_future: Union[str, Task[str]]) -> Optional[int]:
"""Get Launch ID of the given Launch.
Expand Down
49 changes: 47 additions & 2 deletions tests/aio/test_aio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
import pickle
import sys
from io import StringIO
from json import JSONDecodeError
from ssl import SSLContext
from typing import List
from unittest import mock

import aiohttp
# noinspection PyPackageRequirements
import pytest
from aiohttp import ServerTimeoutError

from reportportal_client import OutputType
# noinspection PyProtectedMember
Expand Down Expand Up @@ -318,7 +320,7 @@ async def test_start_launch_event_send(async_send_event):
async def test_launch_uuid_print():
str_io = StringIO()
output_mock = mock.Mock()
output_mock.get_output.side_effect = lambda: str_io
output_mock.get_output.return_value = str_io
client = Client(endpoint='http://endpoint', project='project',
api_key='test', launch_uuid_print=True, print_output=output_mock)
client._session = mock.AsyncMock()
Expand All @@ -333,7 +335,7 @@ async def test_launch_uuid_print():
async def test_no_launch_uuid_print():
str_io = StringIO()
output_mock = mock.Mock()
output_mock.get_output.side_effect = lambda: str_io
output_mock.get_output.return_value = str_io
client = Client(endpoint='http://endpoint', project='project',
api_key='test', launch_uuid_print=False, print_output=output_mock)
client._session = mock.AsyncMock()
Expand Down Expand Up @@ -366,3 +368,46 @@ async def test_launch_uuid_print_default_print(mock_stdout):
client._skip_analytics = True
await client.start_launch('Test Launch', timestamp())
assert 'ReportPortal Launch UUID: ' not in mock_stdout.getvalue()


def connection_error(*args, **kwargs):
raise ServerTimeoutError()


def json_error(*args, **kwargs):
raise JSONDecodeError('invalid Json', '502 Gateway Timeout', 0)


def response_error(*args, **kwargs):
result = mock.AsyncMock()
result.ok = False
result.json.side_effect = json_error
result.status_code = 502
return result


@pytest.mark.parametrize(
'requests_method, client_method, client_params',
[
('post', 'start_launch', ['Test Launch', timestamp()]),
('put', 'finish_launch', ['launch_uuid', timestamp()]),
('post', 'start_test_item', ['launch_uuid', 'Test Item', timestamp(), 'STEP']),
('put', 'finish_test_item', ['launch_uuid', 'test_item_id', timestamp()]),
('put', 'update_test_item', ['test_item_id']),
('get', 'get_item_id_by_uuid', ['test_item_uuid']),
('get', 'get_launch_info', ['launch_uuid']),
('get', 'get_launch_ui_id', ['launch_uuid']),
('get', 'get_launch_ui_url', ['launch_uuid']),
('get', 'get_project_settings', [])
]
)
@pytest.mark.asyncio
async def test_connection_errors(aio_client, requests_method, client_method,
client_params):
getattr(await aio_client.session(), requests_method).side_effect = connection_error
result = await getattr(aio_client, client_method)(*client_params)
assert result is None

getattr(await aio_client.session(), requests_method).side_effect = response_error
result = await getattr(aio_client, client_method)(*client_params)
assert result is None

0 comments on commit 3ac1ba0

Please sign in to comment.