Skip to content

Commit d7ca690

Browse files
committed
Fix #28. Ready to release 2.0.1
* Change to use a requests.session for each connection. Session builds in connection pooling (max 10 in a pool, which seems excessive, but in fact each connection only issues one call at a time so only one connection will ever be open). * Update all test mocks to capture Session calls. This change cut the time for live tests in half. So it's quite a nice enhancement.
1 parent 8b306e4 commit d7ca690

File tree

5 files changed

+65
-64
lines changed

5 files changed

+65
-64
lines changed

tests/test_actions.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ def test_action_create_two_dofirst():
5858

5959

6060
def test_execute_single_success_immediate():
61-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
61+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
6262
mock_post.return_value = MockResponse(200, {"result": "success"})
6363
conn = Connection(**mock_connection_params)
6464
action = Action(top="top").append(a="a")
6565
assert conn.execute_single(action, immediate=True) == (0, 1, 1)
6666

6767

6868
def test_execute_single_success_queued():
69-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
69+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
7070
mock_post.return_value = MockResponse(200, {"result": "success"})
7171
conn = Connection(throttle_actions=2, **mock_connection_params)
7272
action = Action(top="top").append(a="a")
@@ -75,7 +75,7 @@ def test_execute_single_success_queued():
7575

7676

7777
def test_execute_single_error_queued_throttled():
78-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
78+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
7979
mock_post.side_effect = [MockResponse(200, {"result": "success"}),
8080
MockResponse(200, {"result": "partial",
8181
"completed": 1,
@@ -93,7 +93,7 @@ def test_execute_single_error_queued_throttled():
9393

9494

9595
def test_execute_single_error_immediate_throttled():
96-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
96+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
9797
mock_post.return_value = MockResponse(200, {"result": "partial",
9898
"completed": 1,
9999
"notCompleted": 1,
@@ -105,15 +105,15 @@ def test_execute_single_error_immediate_throttled():
105105

106106

107107
def test_execute_single_dofirst_success_immediate():
108-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
108+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
109109
mock_post.return_value = MockResponse(200, {"result": "success"})
110110
conn = Connection(**mock_connection_params)
111111
action = Action(top="top").insert(a="a")
112112
assert conn.execute_single(action, immediate=True) == (0, 1, 1)
113113

114114

115115
def test_execute_single_error_immediate():
116-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
116+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
117117
mock_post.return_value = MockResponse(200, {"result": "error",
118118
"errors": [{"index": 0, "step": 0,
119119
"errorCode": "test.error",
@@ -128,7 +128,7 @@ def test_execute_single_error_immediate():
128128

129129

130130
def test_execute_single_multi_error_immediate():
131-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
131+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
132132
mock_post.return_value = MockResponse(200, {"result": "error",
133133
"errors": [{"index": 0, "step": 0,
134134
"errorCode": "error1",
@@ -150,7 +150,7 @@ def test_execute_single_multi_error_immediate():
150150

151151

152152
def test_execute_single_dofirst_error_immediate():
153-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
153+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
154154
mock_post.return_value = MockResponse(200, {"result": "error",
155155
"errors": [{"index": 0, "step": 0,
156156
"errorCode": "test.error",
@@ -165,7 +165,7 @@ def test_execute_single_dofirst_error_immediate():
165165

166166

167167
def test_execute_multiple_success():
168-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
168+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
169169
mock_post.return_value = MockResponse(200, {"result": "success"})
170170
conn = Connection(**mock_connection_params)
171171
action0 = Action(top="top0").append(a="a0").append(b="b")
@@ -174,7 +174,7 @@ def test_execute_multiple_success():
174174

175175

176176
def test_execute_multiple_success_queued():
177-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
177+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
178178
mock_post.return_value = MockResponse(200, {"result": "success"})
179179
conn = Connection(**mock_connection_params)
180180
action0 = Action(top="top0").append(a="a0").append(b="b")
@@ -184,7 +184,7 @@ def test_execute_multiple_success_queued():
184184

185185

186186
def test_execute_multiple_dofirst_success():
187-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
187+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
188188
mock_post.return_value = MockResponse(200, {"result": "success"})
189189
conn = Connection(**mock_connection_params)
190190
action0 = Action(top="top0").append(a="a0").insert(b="b")
@@ -193,7 +193,7 @@ def test_execute_multiple_dofirst_success():
193193

194194

195195
def test_execute_multiple_error():
196-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
196+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
197197
mock_post.return_value = MockResponse(200, {"result": "partial",
198198
"completed": 1,
199199
"notCompleted": 1,
@@ -212,7 +212,7 @@ def test_execute_multiple_error():
212212

213213

214214
def test_execute_multiple_multi_error():
215-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
215+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
216216
mock_post.return_value = MockResponse(200, {"result": "error",
217217
"completed": 1,
218218
"notCompleted": 1,
@@ -238,7 +238,7 @@ def test_execute_multiple_multi_error():
238238

239239

240240
def test_execute_multiple_dofirst_error():
241-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
241+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
242242
mock_post.return_value = MockResponse(200, {"result": "error",
243243
"completed": 1,
244244
"notCompleted": 1,
@@ -257,7 +257,7 @@ def test_execute_multiple_dofirst_error():
257257

258258

259259
def test_execute_multiple_single_queued_throttle_actions():
260-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
260+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
261261
mock_post.side_effect = [MockResponse(200, {"result": "success"}),
262262
MockResponse(200, {"result": "partial",
263263
"completed": 1,
@@ -291,7 +291,7 @@ def test_execute_multiple_single_queued_throttle_actions():
291291

292292

293293
def test_execute_multiple_queued_throttle_actions_error():
294-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
294+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
295295
mock_post.return_value = MockResponse(500)
296296
conn = Connection(throttle_actions=2, **mock_connection_params)
297297
action0 = Action(top="top0").append(a="a0")

tests/test_connections.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,130 +30,130 @@
3030

3131

3232
def test_remote_status_success():
33-
with mock.patch("umapi_client.connection.requests.get") as mock_get:
33+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
3434
mock_get.return_value = MockResponse(200, body={"build": "2559", "version": "2.1.54", "state":"LIVE"})
3535
conn = Connection(**mock_connection_params)
3636
_, remote_status = conn.status(remote=True)
3737
assert remote_status == {"endpoint": "https://test/", "build": "2559", "version": "2.1.54", "state":"LIVE"}
3838

3939

4040
def test_remote_status_failure():
41-
with mock.patch("umapi_client.connection.requests.get") as mock_get:
41+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
4242
mock_get.return_value = MockResponse(404, text="404 Not Found")
4343
conn = Connection(**mock_connection_params)
4444
_, remote_status = conn.status(remote=True)
4545
assert remote_status["status"].startswith("Unexpected")
4646

4747

4848
def test_remote_status_timeout():
49-
with mock.patch("umapi_client.connection.requests.get") as mock_get:
49+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
5050
mock_get.side_effect = requests.Timeout
5151
conn = Connection(**mock_connection_params)
5252
_, remote_status = conn.status(remote=True)
5353
assert remote_status["status"].startswith("Unreachable")
5454

5555

5656
def test_get_success():
57-
with mock.patch("umapi_client.connection.requests.get") as mock_get:
57+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
5858
mock_get.return_value = MockResponse(200, body=["test", "body"])
5959
conn = Connection(**mock_connection_params)
6060
assert conn.make_call("").json() == ["test", "body"]
6161

6262

6363
def test_get_success_test_mode():
64-
with mock.patch("umapi_client.connection.requests.get") as mock_get:
64+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
6565
mock_get.return_value = MockResponse(200, body=["test", "body"])
6666
conn = Connection(test_mode=True, **mock_connection_params)
6767
assert conn.make_call("").json() == ["test", "body"]
6868

6969

7070
def test_post_success():
71-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
71+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
7272
mock_post.return_value = MockResponse(200, body=["test", "body"])
7373
conn = Connection(**mock_connection_params)
7474
assert conn.make_call("", [3, 5]).json() == ["test", "body"]
7575

7676

7777
def test_post_success_test_mode():
78-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
78+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
7979
mock_post.return_value = MockResponse(200, body=["test", "body"])
8080
conn = Connection(test_mode=True, **mock_connection_params)
8181
assert conn.make_call("", [3, 5]).json() == ["test", "body"]
8282

8383

8484
def test_get_timeout():
85-
with mock.patch("umapi_client.connection.requests.get") as mock_get:
85+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
8686
mock_get.side_effect = requests.Timeout
8787
conn = Connection(**mock_connection_params)
8888
pytest.raises(UnavailableError, conn.make_call, "")
8989

9090

9191
def test_post_timeout():
92-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
92+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
9393
mock_post.side_effect = requests.Timeout
9494
conn = Connection(**mock_connection_params)
9595
pytest.raises(UnavailableError, conn.make_call, "", [3, 5])
9696

9797

9898
def test_get_retry_header_1():
99-
with mock.patch("umapi_client.connection.requests.get") as mock_get:
99+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
100100
mock_get.return_value = MockResponse(429, headers={"Retry-After": "1"})
101101
conn = Connection(**mock_connection_params)
102102
pytest.raises(UnavailableError, conn.make_call, "")
103103

104104

105105
def test_post_retry_header_1():
106-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
106+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
107107
mock_post.return_value = MockResponse(429, headers={"Retry-After": "1"})
108108
conn = Connection(**mock_connection_params)
109109
pytest.raises(UnavailableError, conn.make_call, "", "[3, 5]")
110110

111111

112112
def test_get_retry_header_time_2():
113-
with mock.patch("umapi_client.connection.requests.get") as mock_get:
113+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
114114
mock_get.return_value = MockResponse(502, headers={"Retry-After": formatdate(time.time() + 2.5)})
115115
conn = Connection(**mock_connection_params)
116116
pytest.raises(UnavailableError, conn.make_call, "")
117117

118118

119119
def test_post_retry_header_time_2():
120-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
120+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
121121
mock_post.return_value = MockResponse(502, headers={"Retry-After": formatdate(time.time() + 2.5)})
122122
conn = Connection(**mock_connection_params)
123123
pytest.raises(UnavailableError, conn.make_call, "", "[3, 5]")
124124

125125

126126
def test_get_retry_header_0():
127-
with mock.patch("umapi_client.connection.requests.get") as mock_get:
127+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
128128
mock_get.return_value = MockResponse(503, headers={"Retry-After": "0"})
129129
conn = Connection(**mock_connection_params)
130130
pytest.raises(UnavailableError, conn.make_call, "")
131131

132132

133133
def test_post_retry_header_0():
134-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
134+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
135135
mock_post.return_value = MockResponse(503, headers={"Retry-After": "0"})
136136
conn = Connection(**mock_connection_params)
137137
pytest.raises(UnavailableError, conn.make_call, "", "[3, 5]")
138138

139139

140140
def test_get_retry_no_header():
141-
with mock.patch("umapi_client.connection.requests.get") as mock_get:
141+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
142142
mock_get.return_value = MockResponse(504)
143143
conn = Connection(**mock_connection_params)
144144
pytest.raises(UnavailableError, conn.make_call, "")
145145

146146

147147
def test_post_retry_no_header():
148-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
148+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
149149
mock_post.return_value = MockResponse(504)
150150
conn = Connection(**mock_connection_params)
151151
pytest.raises(UnavailableError, conn.make_call, "", "[3, 5]")
152152

153153

154154
# log_stream fixture defined in conftest.py
155155
def test_get_retry_logging(log_stream):
156-
with mock.patch("umapi_client.connection.requests.get") as mock_get:
156+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
157157
mock_get.return_value = MockResponse(429, headers={"Retry-After": "3"})
158158
stream, logger = log_stream
159159
params = dict(mock_connection_params)
@@ -174,7 +174,7 @@ def test_get_retry_logging(log_stream):
174174

175175
# log_stream fixture defined in conftest.py
176176
def test_post_retry_logging(log_stream):
177-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
177+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
178178
mock_post.return_value = MockResponse(429, headers={"Retry-After": "3"})
179179
stream, logger = log_stream
180180
params = dict(mock_connection_params)
@@ -194,28 +194,28 @@ def test_post_retry_logging(log_stream):
194194

195195

196196
def test_get_server_fail():
197-
with mock.patch("umapi_client.connection.requests.get") as mock_get:
197+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
198198
mock_get.return_value = MockResponse(500, text="500 test server failure")
199199
conn = Connection(**mock_connection_params)
200200
pytest.raises(ServerError, conn.make_call, "")
201201

202202

203203
def test_post_server_fail():
204-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
204+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
205205
mock_post.return_value = MockResponse(500, text="500 test server failure")
206206
conn = Connection(**mock_connection_params)
207207
pytest.raises(ServerError, conn.make_call, "", "[3, 5]")
208208

209209

210210
def test_get_request_fail():
211-
with mock.patch("umapi_client.connection.requests.get") as mock_get:
211+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
212212
mock_get.return_value = MockResponse(400, text="400 test request failure")
213213
conn = Connection(**mock_connection_params)
214214
pytest.raises(RequestError, conn.make_call, "")
215215

216216

217217
def test_post_request_fail():
218-
with mock.patch("umapi_client.connection.requests.post") as mock_post:
218+
with mock.patch("umapi_client.connection.requests.Session.post") as mock_post:
219219
mock_post.return_value = MockResponse(400, text="400 test request failure")
220220
conn = Connection(**mock_connection_params)
221221
pytest.raises(RequestError, conn.make_call, "", "[3, 5]")

0 commit comments

Comments
 (0)