Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Oct 12, 2023
1 parent b79b664 commit 979e7f3
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
3 changes: 2 additions & 1 deletion reportportal_client/_internal/logs/batcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _append(self, size: int, log_req: T_co) -> Optional[List[T_co]]:
if len(self._batch) > 0:
batch = self._batch
self._batch = [log_req]
self._payload_size = 0
self._payload_size = size
return batch
self._batch.append(log_req)
self._payload_size += size
Expand Down Expand Up @@ -91,6 +91,7 @@ def flush(self) -> Optional[List[T_co]]:
if len(self._batch) > 0:
batch = self._batch
self._batch = []
self._payload_size = 0
return batch

def __getstate__(self) -> Dict[str, Any]:
Expand Down
104 changes: 104 additions & 0 deletions tests/_internal/logs/test_log_batcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Copyright (c) 2022 EPAM Systems
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License

import os

from reportportal_client import helpers
# noinspection PyProtectedMember
from reportportal_client._internal.logs.batcher import LogBatcher
from reportportal_client.core.rp_file import RPFile
from reportportal_client.core.rp_requests import RPRequestLog
from reportportal_client.logs import MAX_LOG_BATCH_PAYLOAD_SIZE

TEST_LAUNCH_ID = 'test_launch_uuid'
TEST_ITEM_ID = 'test_item_id'
PROJECT_NAME = 'test_project'
TEST_MASSAGE = 'test_message'
TEST_LEVEL = 'DEBUG'
TEST_BATCH_SIZE = 5
TEST_ATTACHMENT_NAME = 'test_file.bin'
TEST_ATTACHMENT_TYPE = 'application/zip'


def test_log_batch_send_by_length():
log_batcher = LogBatcher(entry_num=TEST_BATCH_SIZE)

for i in range(TEST_BATCH_SIZE):
result = log_batcher.append(
RPRequestLog(launch_uuid=TEST_LAUNCH_ID, time=helpers.timestamp(), message=TEST_MASSAGE,
level=TEST_LEVEL, item_uuid=TEST_ITEM_ID))
if i < TEST_BATCH_SIZE - 1:
assert result is None
assert len(log_batcher._batch) == i + 1
assert log_batcher._payload_size > 0
else:
assert len(result) == TEST_BATCH_SIZE
assert len(log_batcher._batch) == 0
assert log_batcher._payload_size == 0


def test_log_batch_send_by_flush():
log_batcher = LogBatcher(entry_num=TEST_BATCH_SIZE)

for _ in range(TEST_BATCH_SIZE - 1):
log_batcher.append(
RPRequestLog(launch_uuid=TEST_LAUNCH_ID, time=helpers.timestamp(), message=TEST_MASSAGE,
level=TEST_LEVEL, item_uuid=TEST_ITEM_ID))
result = log_batcher.flush()

assert len(result) == TEST_BATCH_SIZE - 1
assert len(log_batcher._batch) == 0
assert log_batcher._payload_size == 0


def test_log_batch_send_by_size():
log_batcher = LogBatcher(entry_num=TEST_BATCH_SIZE)

random_byte_array = bytearray(os.urandom(MAX_LOG_BATCH_PAYLOAD_SIZE))
binary_result = log_batcher.append(
RPRequestLog(launch_uuid=TEST_LAUNCH_ID, time=helpers.timestamp(), message=TEST_MASSAGE,
level=TEST_LEVEL, item_uuid=TEST_ITEM_ID,
file=RPFile(name=TEST_ATTACHMENT_NAME, content=random_byte_array,
content_type=TEST_ATTACHMENT_TYPE)))
message_result = log_batcher.append(
RPRequestLog(launch_uuid=TEST_LAUNCH_ID, time=helpers.timestamp(), message=TEST_MASSAGE,
level=TEST_LEVEL, item_uuid=TEST_ITEM_ID))

assert binary_result is None
assert message_result is not None
assert len(message_result) == 1
assert message_result[0].file is not None
assert log_batcher._payload_size > 0
assert len(log_batcher._batch) == 1


def test_log_batch_triggers_previous_request_to_send():
log_batcher = LogBatcher(entry_num=TEST_BATCH_SIZE)

random_byte_array = bytearray(os.urandom(MAX_LOG_BATCH_PAYLOAD_SIZE))

message_result = log_batcher.append(
RPRequestLog(launch_uuid=TEST_LAUNCH_ID, time=helpers.timestamp(), message=TEST_MASSAGE,
level=TEST_LEVEL, item_uuid=TEST_ITEM_ID))

binary_result = log_batcher.append(
RPRequestLog(launch_uuid=TEST_LAUNCH_ID, time=helpers.timestamp(), message=TEST_MASSAGE,
level=TEST_LEVEL, item_uuid=TEST_ITEM_ID,
file=RPFile(name=TEST_ATTACHMENT_NAME, content=random_byte_array,
content_type=TEST_ATTACHMENT_TYPE)))

assert binary_result is not None
assert message_result is None
assert len(binary_result) == 1
assert binary_result[0].file is None
assert MAX_LOG_BATCH_PAYLOAD_SIZE < log_batcher._payload_size < MAX_LOG_BATCH_PAYLOAD_SIZE * 1.1
File renamed without changes.

0 comments on commit 979e7f3

Please sign in to comment.