Skip to content

Commit cd64d72

Browse files
committed
Logging batch flush on client close
1 parent 2833c75 commit cd64d72

File tree

8 files changed

+93
-1
lines changed

8 files changed

+93
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## [Unreleased]
44
### Added
55
- Issue [#225](https://github.com/reportportal/client-Python/issues/225): JSON decoding error logging, by @HardNorth
6+
### Fixed
7+
- Issue [#226](https://github.com/reportportal/client-Python/issues/226): Logging batch flush on client close, by @HardNorth
68

79
## [5.5.3]
810
### Fixed

reportportal_client/aio/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,7 @@ def clone(self) -> 'AsyncRPClient':
924924

925925
async def close(self) -> None:
926926
"""Close current client connections."""
927+
await self.__client.log_batch(self._log_batcher.flush())
927928
await self.__client.close()
928929

929930

@@ -1307,6 +1308,7 @@ def log(self, time: str, message: str, level: Optional[Union[int, str]] = None,
13071308

13081309
def close(self) -> None:
13091310
"""Close current client connections."""
1311+
self.finish_tasks()
13101312
if self.own_client:
13111313
self.create_task(self.__client.close()).blocking_result()
13121314

reportportal_client/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ def clone(self) -> 'RPClient':
886886

887887
def close(self) -> None:
888888
"""Close current client connections."""
889+
self._log(self._log_batcher.flush())
889890
self.session.close()
890891

891892
def __getstate__(self) -> Dict[str, Any]:

tests/aio/test_async_client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import pytest
2020

2121
from reportportal_client.aio import AsyncRPClient
22+
from reportportal_client.core.rp_requests import AsyncRPRequestLog
2223
from reportportal_client.helpers import timestamp
2324

2425

@@ -171,3 +172,20 @@ async def test_start_item_tracking(async_client: AsyncRPClient):
171172

172173
await async_client.finish_test_item(actual_item_id, timestamp())
173174
assert async_client.current_item() is None
175+
176+
177+
@pytest.mark.skipif(sys.version_info < (3, 8),
178+
reason='the test requires AsyncMock which was introduced in Python 3.8')
179+
@pytest.mark.asyncio
180+
async def test_logs_flush_on_close(async_client: AsyncRPClient):
181+
# noinspection PyTypeChecker
182+
client: mock.Mock = async_client.client
183+
batcher: mock.Mock = mock.Mock()
184+
batcher.flush.return_value = [AsyncRPRequestLog('test_launch_uuid', timestamp(), message='test_message')]
185+
async_client._log_batcher = batcher
186+
187+
await async_client.close()
188+
189+
batcher.flush.assert_called_once()
190+
client.log_batch.assert_called_once()
191+
client.close.assert_called_once()

tests/aio/test_batched_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License
13+
1314
import pickle
1415
import sys
1516
from unittest import mock
@@ -18,6 +19,7 @@
1819
import pytest
1920

2021
from reportportal_client.aio import BatchedRPClient
22+
from reportportal_client.core.rp_requests import AsyncRPRequestLog
2123
from reportportal_client.helpers import timestamp
2224

2325

@@ -137,3 +139,20 @@ def test_launch_uuid_usage(launch_uuid, method, params):
137139
assert args[0].blocking_result() == actual_launch_uuid
138140
for i, param in enumerate(params):
139141
assert args[i + 1] == param
142+
143+
144+
@pytest.mark.skipif(sys.version_info < (3, 8),
145+
reason='the test requires AsyncMock which was introduced in Python 3.8')
146+
def test_logs_flush_on_close(batched_client: BatchedRPClient):
147+
batched_client.own_client = True
148+
# noinspection PyTypeChecker
149+
client: mock.Mock = batched_client.client
150+
batcher: mock.Mock = mock.Mock()
151+
batcher.flush.return_value = [AsyncRPRequestLog('test_launch_uuid', timestamp(), message='test_message')]
152+
batched_client._log_batcher = batcher
153+
154+
batched_client.close()
155+
156+
batcher.flush.assert_called_once()
157+
client.log_batch.assert_called_once()
158+
client.close.assert_called_once()

tests/aio/test_threaded_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License
13+
1314
import pickle
1415
import sys
1516
from unittest import mock
1617

1718
import pytest
1819

1920
from reportportal_client.aio import ThreadedRPClient
21+
from reportportal_client.core.rp_requests import AsyncRPRequestLog
2022
from reportportal_client.helpers import timestamp
2123

2224

@@ -133,3 +135,20 @@ def test_launch_uuid_usage(launch_uuid, method, params):
133135
assert args[0].blocking_result() == actual_launch_uuid
134136
for i, param in enumerate(params):
135137
assert args[i + 1] == param
138+
139+
140+
@pytest.mark.skipif(sys.version_info < (3, 8),
141+
reason='the test requires AsyncMock which was introduced in Python 3.8')
142+
def test_logs_flush_on_close(batched_client: ThreadedRPClient):
143+
batched_client.own_client = True
144+
# noinspection PyTypeChecker
145+
client: mock.Mock = batched_client.client
146+
batcher: mock.Mock = mock.Mock()
147+
batcher.flush.return_value = [AsyncRPRequestLog('test_launch_uuid', timestamp(), message='test_message')]
148+
batched_client._log_batcher = batcher
149+
150+
batched_client.close()
151+
152+
batcher.flush.assert_called_once()
153+
client.log_batch.assert_called_once()
154+
client.close.assert_called_once()

tests/conftest.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
# noinspection PyPackageRequirements
1919
from pytest import fixture
2020

21+
from reportportal_client.aio.client import Client, AsyncRPClient, BatchedRPClient, ThreadedRPClient
2122
from reportportal_client.client import RPClient
22-
from reportportal_client.aio.client import Client, AsyncRPClient
2323

2424

2525
@fixture
@@ -65,3 +65,19 @@ def async_client():
6565
client = AsyncRPClient('http://endpoint', 'project', api_key='api_key',
6666
client=mock.AsyncMock())
6767
return client
68+
69+
70+
@fixture
71+
def batched_client():
72+
"""Prepare instance of the AsyncRPClient for testing."""
73+
client = BatchedRPClient('http://endpoint', 'project', api_key='api_key',
74+
client=mock.AsyncMock())
75+
return client
76+
77+
78+
@fixture
79+
def threaded_client():
80+
"""Prepare instance of the AsyncRPClient for testing."""
81+
client = ThreadedRPClient('http://endpoint', 'project', api_key='api_key',
82+
client=mock.AsyncMock())
83+
return client

tests/test_client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from requests.exceptions import ReadTimeout
2222

2323
from reportportal_client import RPClient
24+
from reportportal_client.core.rp_requests import RPRequestLog
2425
from reportportal_client.helpers import timestamp
2526

2627

@@ -293,3 +294,17 @@ def test_http_timeout_bypass(method, call_method, arguments):
293294
kwargs = getattr(session, call_method).call_args_list[0][1]
294295
assert 'timeout' in kwargs
295296
assert kwargs['timeout'] == http_timeout
297+
298+
299+
def test_logs_flush_on_close(rp_client: RPClient):
300+
# noinspection PyTypeChecker
301+
session: mock.Mock = rp_client.session
302+
batcher: mock.Mock = mock.Mock()
303+
batcher.flush.return_value = [RPRequestLog('test_launch_uuid', timestamp(), message='test_message')]
304+
rp_client._log_batcher = batcher
305+
306+
rp_client.close()
307+
308+
batcher.flush.assert_called_once()
309+
session.post.assert_called_once()
310+
session.close.assert_called_once()

0 commit comments

Comments
 (0)