-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_socket_test.py
255 lines (198 loc) · 8.32 KB
/
get_socket_test.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
""" Get Socket Tests """
from unittest import mock
import mocket
import pytest
import adafruit_connection_manager
def test_get_socket():
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_socket_2 = mocket.Mocket()
mock_pool.socket.side_effect = [
mock_socket_1,
mock_socket_2,
]
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
# get socket
socket = connection_manager.get_socket(mocket.MOCK_HOST_1, 80, "http:")
assert socket == mock_socket_1
def test_get_socket_different_session():
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_socket_2 = mocket.Mocket()
mock_pool.socket.side_effect = [
mock_socket_1,
mock_socket_2,
]
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
# get socket
socket = connection_manager.get_socket(
mocket.MOCK_HOST_1, 80, "http:", session_id="1"
)
assert socket == mock_socket_1
# get socket on different session
socket = connection_manager.get_socket(
mocket.MOCK_HOST_1, 80, "http:", session_id="2"
)
assert socket == mock_socket_2
def test_get_socket_flagged_free():
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_socket_2 = mocket.Mocket()
mock_pool.socket.side_effect = [
mock_socket_1,
mock_socket_2,
]
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
# get a socket and then mark as free
socket = connection_manager.get_socket(mocket.MOCK_HOST_1, 80, "http:")
assert socket == mock_socket_1
connection_manager.free_socket(socket)
# get a socket for the same host, should be the same one
socket = connection_manager.get_socket(mocket.MOCK_HOST_1, 80, "http:")
assert socket == mock_socket_1
def test_get_socket_not_flagged_free():
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_socket_2 = mocket.Mocket()
mock_pool.socket.side_effect = [
mock_socket_1,
mock_socket_2,
]
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
# get a socket but don't mark as free
socket = connection_manager.get_socket(mocket.MOCK_HOST_1, 80, "http:")
assert socket == mock_socket_1
# get a socket for the same host, should be a different one
with pytest.raises(RuntimeError) as context:
socket = connection_manager.get_socket(mocket.MOCK_HOST_1, 80, "http:")
assert "Socket already connected" in str(context)
def test_get_socket_os_error():
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_pool.socket.side_effect = [
OSError("OSError"),
mock_socket_1,
]
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
# try to get a socket that returns a OSError
with pytest.raises(RuntimeError) as context:
connection_manager.get_socket(mocket.MOCK_HOST_1, 80, "http:")
assert "Error connecting socket: OSError" in str(context)
def test_get_socket_runtime_error():
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_pool.socket.side_effect = [
RuntimeError("RuntimeError"),
mock_socket_1,
]
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
# try to get a socket that returns a RuntimeError
with pytest.raises(RuntimeError) as context:
connection_manager.get_socket(mocket.MOCK_HOST_1, 80, "http:")
assert "Error connecting socket: RuntimeError" in str(context)
def test_get_socket_connect_memory_error():
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_socket_2 = mocket.Mocket()
mock_pool.socket.side_effect = [
mock_socket_1,
mock_socket_2,
]
mock_socket_1.connect.side_effect = MemoryError("MemoryError")
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
# try to connect a socket that returns a MemoryError
with pytest.raises(RuntimeError) as context:
connection_manager.get_socket(mocket.MOCK_HOST_1, 80, "http:")
assert "Error connecting socket: MemoryError" in str(context)
def test_get_socket_connect_os_error():
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_socket_2 = mocket.Mocket()
mock_pool.socket.side_effect = [
mock_socket_1,
mock_socket_2,
]
mock_socket_1.connect.side_effect = OSError("OSError")
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
# try to connect a socket that returns a OSError
with pytest.raises(RuntimeError) as context:
connection_manager.get_socket(mocket.MOCK_HOST_1, 80, "http:")
assert "Error connecting socket: OSError" in str(context)
def test_get_socket_runtime_error_ties_again_at_least_one_free():
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_socket_2 = mocket.Mocket()
mock_pool.socket.side_effect = [
mock_socket_1,
RuntimeError(),
mock_socket_2,
]
free_sockets_mock = mock.Mock()
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
connection_manager._free_sockets = free_sockets_mock
# get a socket and then mark as free
socket = connection_manager.get_socket(mocket.MOCK_HOST_1, 80, "http:")
assert socket == mock_socket_1
connection_manager.free_socket(socket)
free_sockets_mock.assert_not_called()
# try to get a socket that returns a RuntimeError and at least one is flagged as free
socket = connection_manager.get_socket(mocket.MOCK_HOST_2, 80, "http:")
assert socket == mock_socket_2
free_sockets_mock.assert_called_once()
def test_get_socket_runtime_error_ties_again_only_once():
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_socket_2 = mocket.Mocket()
mock_pool.socket.side_effect = [
mock_socket_1,
RuntimeError("error 1"),
RuntimeError("error 2"),
RuntimeError("error 3"),
mock_socket_2,
]
free_sockets_mock = mock.Mock()
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
connection_manager._free_sockets = free_sockets_mock
# get a socket and then mark as free
socket = connection_manager.get_socket(mocket.MOCK_HOST_1, 80, "http:")
assert socket == mock_socket_1
connection_manager.free_socket(socket)
free_sockets_mock.assert_not_called()
# try to get a socket that returns a RuntimeError twice
with pytest.raises(RuntimeError) as context:
connection_manager.get_socket(mocket.MOCK_HOST_2, 80, "http:")
assert "Error connecting socket: error 2, first error: error 1" in str(context)
free_sockets_mock.assert_called_once()
def test_fake_ssl_context_connect( # pylint: disable=unused-argument
adafruit_esp32spi_socketpool_module,
):
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_pool.socket.return_value = mock_socket_1
radio = mocket.MockRadio.ESP_SPIcontrol()
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
# verify a HTTPS call gets a _FakeSSLSocket
socket = connection_manager.get_socket(
mocket.MOCK_HOST_1, 443, "https:", ssl_context=ssl_context
)
assert socket != mock_socket_1
socket._socket.connect.assert_called_once()
def test_fake_ssl_context_connect_error( # pylint: disable=unused-argument
adafruit_esp32spi_socketpool_module,
):
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_pool.socket.return_value = mock_socket_1
mock_socket_1.connect.side_effect = RuntimeError("RuntimeError")
radio = mocket.MockRadio.ESP_SPIcontrol()
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
with pytest.raises(RuntimeError) as context:
connection_manager.get_socket(
mocket.MOCK_HOST_1, 443, "https:", ssl_context=ssl_context
)
assert "Error connecting socket: [Errno 12] RuntimeError" in str(context)