|
| 1 | +# Copyright (c) 2022 EPAM Systems |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License |
| 13 | + |
| 14 | + |
| 15 | +from six.moves import mock |
| 16 | + |
| 17 | +from reportportal_client import helpers |
| 18 | +from reportportal_client.core.log_manager import LogManager |
| 19 | + |
| 20 | +RP_URL = 'http://docker.local:8080' |
| 21 | +API_VERSION = 'api/v2' |
| 22 | +TEST_LAUNCH_ID = 'test_launch_id' |
| 23 | +TEST_ITEM_ID = 'test_item_id' |
| 24 | +PROJECT_NAME = 'test_project' |
| 25 | +KILOBYTE = 2 ** 10 |
| 26 | +MEGABYTE = KILOBYTE ** KILOBYTE |
| 27 | +ABOVE_LIMIT_SIZE = MEGABYTE * 65 |
| 28 | +TEST_MASSAGE = 'test_message' |
| 29 | +TEST_LEVEL = 'DEBUG' |
| 30 | +TEST_BATCH_SIZE = 5 |
| 31 | + |
| 32 | + |
| 33 | +# noinspection PyUnresolvedReferences |
| 34 | +def test_log_batch_send_by_length(): |
| 35 | + session = mock.Mock() |
| 36 | + log_manager = LogManager(RP_URL, session, API_VERSION, TEST_LAUNCH_ID, |
| 37 | + PROJECT_NAME, max_entry_number=TEST_BATCH_SIZE, |
| 38 | + verify_ssl=False) |
| 39 | + log_manager._worker = mock.Mock() |
| 40 | + |
| 41 | + for _ in range(TEST_BATCH_SIZE): |
| 42 | + log_manager.log(helpers.timestamp(), TEST_MASSAGE, TEST_LEVEL, |
| 43 | + item_id=TEST_ITEM_ID) |
| 44 | + |
| 45 | + assert log_manager._worker.send.call_count == 1 |
| 46 | + batch = log_manager._worker.send.call_args[0][0] |
| 47 | + assert len(batch.log_reqs) == 5 |
| 48 | + assert batch.http_request is not None |
| 49 | + assert 'post' in session._mock_children |
| 50 | + assert len(log_manager._batch) == 0 |
| 51 | + assert log_manager._payload_size == helpers.TYPICAL_MULTIPART_FOOTER_LENGTH |
| 52 | + |
| 53 | + |
| 54 | +# noinspection PyUnresolvedReferences |
| 55 | +def test_log_batch_not_send_by_length(): |
| 56 | + session = mock.Mock() |
| 57 | + log_manager = LogManager(RP_URL, session, API_VERSION, TEST_LAUNCH_ID, |
| 58 | + PROJECT_NAME, max_entry_number=TEST_BATCH_SIZE, |
| 59 | + verify_ssl=False) |
| 60 | + log_manager._worker = mock.Mock() |
| 61 | + |
| 62 | + for _ in range(TEST_BATCH_SIZE - 1): |
| 63 | + log_manager.log(helpers.timestamp(), TEST_MASSAGE, TEST_LEVEL, |
| 64 | + item_id=TEST_ITEM_ID) |
| 65 | + |
| 66 | + assert log_manager._worker.send.call_count == 0 |
| 67 | + assert 'post' not in session._mock_children |
| 68 | + assert len(log_manager._batch) == 4 |
| 69 | + assert log_manager._payload_size > helpers.TYPICAL_MULTIPART_FOOTER_LENGTH |
| 70 | + |
| 71 | + |
| 72 | +# noinspection PyUnresolvedReferences |
| 73 | +def test_log_batch_send_by_stop(): |
| 74 | + session = mock.Mock() |
| 75 | + log_manager = LogManager(RP_URL, session, API_VERSION, TEST_LAUNCH_ID, |
| 76 | + PROJECT_NAME, max_entry_number=TEST_BATCH_SIZE, |
| 77 | + verify_ssl=False) |
| 78 | + log_manager._worker = mock.Mock() |
| 79 | + |
| 80 | + for _ in range(TEST_BATCH_SIZE - 1): |
| 81 | + log_manager.log(helpers.timestamp(), TEST_MASSAGE, TEST_LEVEL, |
| 82 | + item_id=TEST_ITEM_ID) |
| 83 | + log_manager.stop() |
| 84 | + |
| 85 | + assert log_manager._worker.send.call_count == 1 |
| 86 | + batch = log_manager._worker.send.call_args[0][0] |
| 87 | + assert len(batch.log_reqs) == 4 |
| 88 | + assert batch.http_request is not None |
| 89 | + assert 'post' in session._mock_children |
| 90 | + assert len(log_manager._batch) == 0 |
| 91 | + assert log_manager._payload_size == helpers.TYPICAL_MULTIPART_FOOTER_LENGTH |
0 commit comments