Skip to content

Commit be07d32

Browse files
committed
Logging continuation test add
1 parent 77f17c7 commit be07d32

File tree

4 files changed

+96
-12
lines changed

4 files changed

+96
-12
lines changed

reportportal_client/core/worker.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ def _command_process(self, cmd):
8787
else:
8888
self._stop()
8989

90+
def _request_process(self, request):
91+
"""Send request to RP and update response attribute of the request."""
92+
logger.debug('[%s] Processing {%s} request', self.name, request)
93+
try:
94+
request.response = request.http_request.make()
95+
except Exception as err:
96+
logger.exception('[%s] Unknown exception has occurred. '
97+
'Skipping it.', err)
98+
self._queue.task_done()
99+
90100
def _monitor(self):
91101
"""Monitor worker queues and process them.
92102
@@ -111,16 +121,6 @@ def _monitor(self):
111121
logger.debug('[%s] Received {%s} request', self.name, cmd)
112122
self._request_process(cmd)
113123

114-
def _request_process(self, request):
115-
"""Send request to RP and update response attribute of the request."""
116-
logger.debug('[%s] Processing {%s} request', self.name, request)
117-
try:
118-
request.response = request.http_request.make()
119-
except Exception as err:
120-
logger.exception('[%s] Unknown exception has occurred. Terminating'
121-
' the worker.', err)
122-
self._queue.task_done()
123-
124124
def _stop(self):
125125
"""Routine that stops the worker thread(s).
126126

reportportal_client/core/worker.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ class APIWorker:
5353

5454
def _command_process(self, cmd: Optional[ControlCommand]) -> None: ...
5555

56-
def _monitor(self) -> None: ...
57-
5856
def _request_process(self, request: Optional[RPRequest]) -> None: ...
5957

58+
def _monitor(self) -> None: ...
59+
6060
def _stop(self) -> None: ...
6161

6262
def _stop_immediately(self) -> None: ...

tests/core/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""This package contains unit tests for core module."""
2+
3+
# Copyright (c) 2022 EPAM Systems
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+
# https://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

tests/core/test_worker.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""Worker class tests."""
2+
3+
# Copyright (c) 2022 EPAM Systems
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+
# https://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+
import time
17+
from six.moves import queue, mock
18+
19+
from requests.exceptions import ConnectionError
20+
from reportportal_client.core.rp_requests import (
21+
HttpRequest,
22+
RPLogBatch,
23+
RPRequestLog
24+
)
25+
from reportportal_client.core.worker import APIWorker
26+
from reportportal_client.helpers import timestamp
27+
28+
LOG_REQUEST_URL = 'http://docker.local:8080/api/v1/default_personal/log'
29+
TEST_LAUNCH_UUID = 'test_uuid'
30+
TEST_MASSAGE = "test message"
31+
32+
33+
def test_worker_continue_working_on_request_error():
34+
test_queue = queue.PriorityQueue()
35+
worker = APIWorker(test_queue)
36+
worker.start()
37+
38+
log_request = RPRequestLog(TEST_LAUNCH_UUID, timestamp(),
39+
message=TEST_MASSAGE)
40+
log_batch = RPLogBatch([log_request])
41+
42+
fail_session = mock.Mock()
43+
fail_session.side_effect = Exception()
44+
http_fail = HttpRequest(
45+
fail_session, LOG_REQUEST_URL, files=log_batch.payload,
46+
verify_ssl=False)
47+
log_batch.http_request = http_fail
48+
worker.send(log_batch)
49+
50+
start_time = time.time()
51+
while fail_session.call_count < 1 and time.time() - start_time < 10:
52+
time.sleep(0.1)
53+
54+
assert fail_session.call_count == 1
55+
assert log_batch.response is None
56+
57+
pass_session = mock.Mock()
58+
http_pass = HttpRequest(
59+
pass_session, LOG_REQUEST_URL, files=log_batch.payload,
60+
verify_ssl=False)
61+
log_batch.http_request = http_pass
62+
worker.send(log_batch)
63+
64+
start_time = time.time()
65+
while pass_session.call_count < 1 and time.time() - start_time < 10:
66+
time.sleep(0.1)
67+
68+
assert pass_session.call_count == 1
69+
assert log_batch.response
70+
worker.stop()

0 commit comments

Comments
 (0)