-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathtest_http.py
166 lines (134 loc) · 5.54 KB
/
test_http.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
Test rp_http.py module.
"""
# pylint: disable=too-few-public-methods
import gc
import json
import unittest
from unittest.mock import AsyncMock, patch
import aiohttp
from runpod.serverless.modules import rp_http
class MockRequestInfo:
"""Mock aiohttp.RequestInfo class."""
def __init__(self, *args, **kwargs):
del args, kwargs
self.url = "http://test_url"
self.method = "POST"
self.headers = {"Content-Type": "application/json"}
self.real_url = "http://test_url"
real_url = "http://test_url"
class TestHTTP(unittest.IsolatedAsyncioTestCase):
"""Test HTTP module."""
async def asyncSetUp(self) -> None:
self.job = {"id": "test_id"}
self.job_data = {"output": "test_output"}
async def asyncTearDown(self) -> None:
gc.collect()
async def test_send_result(self):
"""
Test send_result function.
"""
with patch("runpod.serverless.modules.rp_http.log") as mock_log, patch(
"runpod.serverless.modules.rp_http.RetryClient"
) as mock_retry:
mock_retry.return_value.post.return_value = AsyncMock()
mock_retry.return_value.post.return_value.__aenter__.return_value.text.return_value = (
"response text" # pylint: disable=line-too-long
)
send_return_local = await rp_http.send_result(
AsyncMock(), self.job_data, self.job
)
assert send_return_local is None
assert mock_log.debug.call_count == 1
assert mock_log.error.call_count == 0
assert mock_log.info.call_count == 1
mock_retry.return_value.post.assert_called_with(
"JOB_DONE_URL" + "&isStream=false",
data=str(json.dumps(self.job_data, ensure_ascii=False)),
headers={
"charset": "utf-8",
"Content-Type": "application/x-www-form-urlencoded",
},
raise_for_status=True,
)
async def test_send_result_client_response_error(self):
"""
Test send_result function with ClientResponseError.
"""
def mock_request_info_init(self, *args, **kwargs):
"""
Mock aiohttp.RequestInfo.__init__ method.
"""
del args, kwargs
self.url = "http://test_url"
self.method = "POST"
self.headers = {"Content-Type": "application/json"}
self.real_url = "http://test_url"
with patch("runpod.serverless.modules.rp_http.log") as mock_log, patch(
"runpod.serverless.modules.rp_http.RetryClient"
) as mock_retry, patch.object(
aiohttp.RequestInfo, "__init__", mock_request_info_init
):
mock_retry.side_effect = aiohttp.ClientResponseError(
request_info=MockRequestInfo,
history=None,
status=500,
message="Error message",
)
send_return_local = await rp_http.send_result(
AsyncMock(), self.job_data, self.job
)
assert send_return_local is None
assert mock_log.debug.call_count == 0
assert mock_log.error.call_count == 1
assert mock_log.info.call_count == 1
async def test_send_result_type_error(self):
"""
Test send_result function with TypeError.
"""
with patch("runpod.serverless.modules.rp_http.log") as mock_log, patch(
"runpod.serverless.modules.rp_http.json.dumps"
) as mock_dumps, patch(
"runpod.serverless.modules.rp_http.RetryClient"
) as mock_retry:
mock_dumps.side_effect = TypeError("Forced exception")
send_return_local = await rp_http.send_result(
aiohttp.ClientSession(), self.job_data, self.job
)
assert send_return_local is None
assert mock_log.debug.call_count == 0
assert mock_log.error.call_count == 1
assert mock_log.info.call_count == 1
assert mock_retry.return_value.post.call_count == 0
mock_log.error.assert_called_with(
"Error while returning job result. | Forced exception", "test_id"
) # pylint: disable=line-too-long
async def test_stream_result(self):
"""
Test stream_result function.
"""
with patch("runpod.serverless.modules.rp_http.log") as mock_log, patch(
"runpod.serverless.modules.rp_http.RetryClient"
) as mock_retry:
mock_retry.return_value.post.return_value = AsyncMock()
mock_retry.return_value.post.return_value.__aenter__.return_value.text.return_value = (
"response text" # pylint: disable=line-too-long
)
send_return_local = await rp_http.stream_result(
AsyncMock(), self.job_data, self.job
)
assert send_return_local is None
assert mock_log.debug.call_count == 1
assert mock_log.error.call_count == 0
assert mock_log.info.call_count == 0
mock_retry.return_value.post.assert_called_with(
"JOB_STREAM_URL" + "&isStream=false",
data=str(json.dumps(self.job_data, ensure_ascii=False)),
headers={
"charset": "utf-8",
"Content-Type": "application/x-www-form-urlencoded",
},
raise_for_status=True,
)
if __name__ == "__main__":
unittest.main()