forked from ctheiss/simple-requests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
executable file
·624 lines (507 loc) · 30.6 KB
/
tests.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Test suite for simple-requests
Please note that many of the tests check that the timing of a certain number
of synthetic requests fall within a very tight time range (0.04 seconds).
Slow computers, or running the tests in the background may fail these tests.
"""
from gevent import sleep
from random import random
from re import compile
from requests import Response, Session, Timeout
from time import time
from types import MethodType
from unittest import main, TestCase
from simple_requests import *
patch(allowIncompleteResponses = True, avoidTooManyOpenFiles = True)
class NoRaiseHTTPError(ResponsePreprocessor):
def error(self, bundle):
if isinstance(bundle.exception, HTTPError):
return bundle.ret()
else:
raise bundle.exception
class NoRaiseTimeoutError(ResponsePreprocessor):
def error(self, bundle):
if isinstance(bundle.exception, Timeout):
return bundle.ret()
else:
raise bundle.exception
class Test1Logic(TestCase):
def setUp(self):
self.default = Requests()
self.highConcurrency = Requests(concurrent = 5)
self.noRaise = Requests(responsePreprocessor = NoRaiseHTTPError())
self.defaultSendTime = defaultSendTime = 0.4
self.defaultRetryWait = 2
parser = compile('^(.+)/([^:]+):([0-9]+):?([.0-9]+)?$')
# Monkey patch the actual send to make testing timings easier
def fake_send(self, request):
g = parser.match(request.url).groups()
response = Response()
response.url = g[0]
response.reason = g[1]
response.status_code = int(g[2])
if g[3] is not None:
wait = float(g[3])
else:
wait = defaultSendTime - 0.001 # Epsilon, since sleep is defined as "will wait at *least* as long as..."
sleep(wait)
if response.status_code >= 600:
# Special case for testing exception handling
raise Exception('[%d] %s' % ( response.status_code, response.reason ))
return response
self.default.session.send = MethodType(fake_send, self.default.session, Session)
self.highConcurrency.session.send = MethodType(fake_send, self.highConcurrency.session, Session)
self.noRaise.session.send = MethodType(fake_send, self.noRaise.session, Session)
# The first request always suffers through various init times of lazily-loaded objects;
# make a throw-away one here to avoid affecting the tests
self.default.one('http://cat-videos.net/setup/OK:200').url
def test_sync(self):
start = time()
self.assertEqual(self.default.one('http://cat-videos.net/1/OK:200').url, 'http://cat-videos.net/1')
self.assertEqual(self.default.one('http://cat-videos.net/2/OK:200').url, 'http://cat-videos.net/2')
self.assertEqual(self.default.one('http://cat-videos.net/3/OK:200').url, 'http://cat-videos.net/3')
self.assertEqual(self.default.one('http://cat-videos.net/4/OK:200').url, 'http://cat-videos.net/4')
self.assertEqual(self.default.one('http://cat-videos.net/5/OK:200').url, 'http://cat-videos.net/5')
self.assertAlmostEqual(time() - start, self.defaultSendTime * 5, delta = 0.04)
def test_async(self):
responses = []
start = time()
for r1 in self.default.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200', 'http://cat-videos.net/3/OK:200', 'http://cat-videos.net/4/OK:200', 'http://cat-videos.net/5/OK:200' ]):
responses.append(r1.url)
self.assertAlmostEqual(time() - start, self.defaultSendTime * 3, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1', 'http://cat-videos.net/2', 'http://cat-videos.net/3', 'http://cat-videos.net/4', 'http://cat-videos.net/5' ], responses)
def test_async_high_mintime(self):
responses = []
oldValue = self.default.minSecondsBetweenRequests
self.default.minSecondsBetweenRequests = 0.25
start = time()
for r1 in self.default.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200', 'http://cat-videos.net/3/OK:200', 'http://cat-videos.net/4/OK:200', 'http://cat-videos.net/5/OK:200' ]):
responses.append(r1.url)
self.assertAlmostEqual(time() - start, self.default.minSecondsBetweenRequests * 4 + self.defaultSendTime, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1', 'http://cat-videos.net/2', 'http://cat-videos.net/3', 'http://cat-videos.net/4', 'http://cat-videos.net/5' ], responses)
self.default.minSecondsBetweenRequests = oldValue
def test_async_high_concurrency(self):
responses = []
start = time()
for r1 in self.highConcurrency.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200', 'http://cat-videos.net/3/OK:200', 'http://cat-videos.net/4/OK:200', 'http://cat-videos.net/5/OK:200', 'http://cat-videos.net/6/OK:200' ]):
responses.append(r1.url)
self.assertAlmostEqual(time() - start, self.highConcurrency.minSecondsBetweenRequests * 5 + self.defaultSendTime, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1', 'http://cat-videos.net/2', 'http://cat-videos.net/3', 'http://cat-videos.net/4', 'http://cat-videos.net/5', 'http://cat-videos.net/6' ], responses)
def test_async_low_mintime1(self):
responses = []
oldValue = self.highConcurrency.minSecondsBetweenRequests
self.highConcurrency.minSecondsBetweenRequests = 0.05
start = time()
for r1 in self.highConcurrency.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200', 'http://cat-videos.net/3/OK:200', 'http://cat-videos.net/4/OK:200', 'http://cat-videos.net/5/OK:200' ]):
responses.append(r1.url)
self.assertAlmostEqual(time() - start, self.highConcurrency.minSecondsBetweenRequests * 4 + self.defaultSendTime, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1', 'http://cat-videos.net/2', 'http://cat-videos.net/3', 'http://cat-videos.net/4', 'http://cat-videos.net/5' ], responses)
self.highConcurrency.minSecondsBetweenRequests = oldValue
def test_async_low_mintime2(self):
responses = []
oldValue = self.highConcurrency.minSecondsBetweenRequests
self.highConcurrency.minSecondsBetweenRequests = 0.05
start = time()
for r1 in self.highConcurrency.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200', 'http://cat-videos.net/3/OK:200', 'http://cat-videos.net/4/OK:200', 'http://cat-videos.net/5/OK:200', 'http://cat-videos.net/6/OK:200' ]):
responses.append(r1.url)
self.assertAlmostEqual(time() - start, 0.8, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1', 'http://cat-videos.net/2', 'http://cat-videos.net/3', 'http://cat-videos.net/4', 'http://cat-videos.net/5', 'http://cat-videos.net/6' ], responses)
self.highConcurrency.minSecondsBetweenRequests = oldValue
def test_async_order1(self):
responses = []
start = time()
for r1 in self.default.swarm([ 'http://cat-videos.net/1/OK:200:3', 'http://cat-videos.net/2/OK:200', 'http://cat-videos.net/3/OK:200', 'http://cat-videos.net/4/OK:200', 'http://cat-videos.net/5/OK:200' ]):
responses.append(r1.url)
sleep(0.1)
self.assertAlmostEqual(time() - start, 3.5, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1', 'http://cat-videos.net/2', 'http://cat-videos.net/3', 'http://cat-videos.net/4', 'http://cat-videos.net/5' ], responses)
def test_async_order2(self):
responses = []
start = time()
for r1 in self.default.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200', 'http://cat-videos.net/3/OK:200:3', 'http://cat-videos.net/4/OK:200', 'http://cat-videos.net/5/OK:200' ]):
responses.append(r1.url)
sleep(0.1)
self.assertAlmostEqual(time() - start, 3.7, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1', 'http://cat-videos.net/2', 'http://cat-videos.net/3', 'http://cat-videos.net/4', 'http://cat-videos.net/5' ], responses)
def test_async_noorder1(self):
responses = set()
start = time()
for r1 in self.default.swarm([ 'http://cat-videos.net/1/OK:200:3', 'http://cat-videos.net/2/OK:200', 'http://cat-videos.net/3/OK:200', 'http://cat-videos.net/4/OK:200', 'http://cat-videos.net/5/OK:200' ], maintainOrder = False):
responses.add(r1.url)
sleep(0.1)
self.assertAlmostEqual(time() - start, 3.1, delta = 0.04)
self.assertEqual({ 'http://cat-videos.net/1', 'http://cat-videos.net/2', 'http://cat-videos.net/3', 'http://cat-videos.net/4', 'http://cat-videos.net/5' }, responses)
def test_async_noorder2(self):
responses = set()
start = time()
for r1 in self.default.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200', 'http://cat-videos.net/3/OK:200:3', 'http://cat-videos.net/4/OK:200', 'http://cat-videos.net/5/OK:200' ], maintainOrder = False):
responses.add(r1.url)
sleep(0.1)
self.assertAlmostEqual(time() - start, 3.5, delta = 0.04)
self.assertEqual({ 'http://cat-videos.net/1', 'http://cat-videos.net/2', 'http://cat-videos.net/3', 'http://cat-videos.net/4', 'http://cat-videos.net/5' }, responses)
def test_empty(self):
responses = set()
start = time()
for r1 in self.default.swarm([]):
self.fail()
self.assertAlmostEqual(time() - start, 0, delta = 0.04)
def test_sync_exception1(self):
start = time()
try:
self.default.one('http://cat-videos.net/1/Test:450')
self.fail()
except HTTPError as err:
self.assertEqual(err.msg, 'Test')
self.assertEqual(err.code, 450)
self.assertAlmostEqual(time() - start, 5.2, delta = 0.04)
def test_sync_exception2(self):
start = time()
try:
self.default.one('http://cat-videos.net/1/Test:640')
self.fail()
except Exception as err:
self.assertEqual(str(err), '[640] Test')
self.assertAlmostEqual(time() - start, 0.4, delta = 0.04)
def test_sync_noraise_exception1(self):
start = time()
r1 = self.noRaise.one('http://cat-videos.net/1/Test:450')
self.assertEqual(r1.reason, 'Test')
self.assertEqual(r1.status_code, 450)
self.assertAlmostEqual(time() - start, 5.2, delta = 0.04)
def test_sync_noraise_exception2(self):
start = time()
try:
self.noRaise.one('http://cat-videos.net/1/Test:640')
self.fail()
except Exception as err:
self.assertEqual(str(err), '[640] Test')
self.assertAlmostEqual(time() - start, 0.4, delta = 0.04)
def test_sync_notrequest(self):
start = time()
try:
self.default.one(123)
self.fail()
except TypeError as err:
pass
self.assertAlmostEqual(time() - start, 0, delta = 0.04)
def test_sync_lenient1(self):
oldValue = self.default.retryStrategy
self.default.retryStrategy = Lenient()
start = time()
try:
self.default.one('http://cat-videos.net/1/Test:550')
self.fail()
except HTTPError as err:
self.assertEqual(err.msg, 'Test')
self.assertEqual(err.code, 550)
self.assertAlmostEqual(time() - start, 242, delta = 0.08)
self.default.retryStrategy = oldValue
def test_sync_lenient2(self):
oldValue = self.default.retryStrategy
self.default.retryStrategy = Lenient()
start = time()
try:
self.default.one('http://cat-videos.net/1/Test:650')
self.fail()
except Exception as err:
self.assertEqual(str(err), '[650] Test')
self.assertAlmostEqual(time() - start, 60.8, delta = 0.04)
self.default.retryStrategy = oldValue
def test_sync_backoff1(self):
oldValue = self.default.retryStrategy
self.default.retryStrategy = Backoff()
start = time()
try:
self.default.one('http://cat-videos.net/1/Test:560')
self.fail()
except HTTPError as err:
self.assertEqual(err.msg, 'Test')
self.assertEqual(err.code, 560)
self.assertAlmostEqual(time() - start, 247.9, delta = 0.08)
self.default.retryStrategy = oldValue
def test_sync_backoff2(self):
oldValue = self.default.retryStrategy
self.default.retryStrategy = Backoff()
start = time()
try:
self.default.one('http://cat-videos.net/1/Test:660')
self.fail()
except Exception as err:
self.assertEqual(str(err), '[660] Test')
self.assertAlmostEqual(time() - start, 10.8, delta = 0.04)
self.default.retryStrategy = oldValue
def test_swarm_in_swarm_order1(self):
responses = []
start = time()
for r1 in self.default.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200' ]):
for r2 in self.default.swarm([ r1.url + '/A/OK:200', r1.url + '/B/OK:200', r1.url + '/C/OK:200' ]):
responses.append(r2.url)
sleep(0.1)
self.assertAlmostEqual(time() - start, 2.2, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1/A', 'http://cat-videos.net/1/B', 'http://cat-videos.net/1/C', 'http://cat-videos.net/2/A', 'http://cat-videos.net/2/B', 'http://cat-videos.net/2/C' ], responses)
def test_swarm_in_swarm_order2(self):
responses = []
start = time()
for r1 in self.highConcurrency.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200' ]):
for r2 in self.highConcurrency.swarm([ r1.url + '/A/OK:200', r1.url + '/B/OK:200', r1.url + '/C/OK:200' ]):
responses.append(r2.url)
sleep(0.1)
self.assertAlmostEqual(time() - start, 2, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1/A', 'http://cat-videos.net/1/B', 'http://cat-videos.net/1/C', 'http://cat-videos.net/2/A', 'http://cat-videos.net/2/B', 'http://cat-videos.net/2/C' ], responses)
def test_swarm_in_swarm_order3(self):
responses = []
start = time()
for r1 in self.default.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200' ]):
for r2 in self.default.swarm([ r1.url + '/A/OK:200', r1.url + '/B/OK:200', r1.url + '/C/OK:200:0.6' ]):
responses.append(r2.url)
sleep(0.1)
self.assertAlmostEqual(time() - start, 2.6, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1/A', 'http://cat-videos.net/1/B', 'http://cat-videos.net/1/C', 'http://cat-videos.net/2/A', 'http://cat-videos.net/2/B', 'http://cat-videos.net/2/C' ], responses)
def test_swarm_in_swarm_order4(self):
responses = []
start = time()
for r1 in self.highConcurrency.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200' ]):
for r2 in self.highConcurrency.swarm([ r1.url + '/A/OK:200', r1.url + '/B/OK:200', r1.url + '/C/OK:200:0.6' ]):
responses.append(r2.url)
sleep(0.1)
self.assertAlmostEqual(time() - start, 2.4, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1/A', 'http://cat-videos.net/1/B', 'http://cat-videos.net/1/C', 'http://cat-videos.net/2/A', 'http://cat-videos.net/2/B', 'http://cat-videos.net/2/C' ], responses)
def test_big_swarm_in_swarm_order(self):
responses = []
oldValue = self.default.minSecondsBetweenRequests
self.default.minSecondsBetweenRequests = 0
start = time()
for r1 in self.default.swarm([ 'http://cat-videos.net/1/OK:200:3', 'http://cat-videos.net/2/OK:200:1', 'http://cat-videos.net/3/OK:200:3', 'http://cat-videos.net/4/OK:200:5' ]):
r2 = self.default.one(r1.url + '/X/OK:200:1')
for r3 in self.default.swarm([ r2.url + '/A/OK:200:2', r2.url + '/B/OK:200:1', r2.url + '/C/OK:200:1' ]):
responses.append(r3.url[22:])
self.assertAlmostEqual(time() - start, 17, delta = 0.1)
self.assertEqual([ '1/X/A', '1/X/B', '1/X/C', '2/X/A', '2/X/B', '2/X/C', '3/X/A', '3/X/B', '3/X/C', '4/X/A', '4/X/B', '4/X/C' ], responses)
self.default.minSecondsBetweenRequests = oldValue
def test_big_swarm_in_swarm_noorder(self):
responses = set()
oldValue = self.default.minSecondsBetweenRequests
self.default.minSecondsBetweenRequests = 0
start = time()
for r1 in self.default.swarm([ 'http://cat-videos.net/1/OK:200:3', 'http://cat-videos.net/2/OK:200:1', 'http://cat-videos.net/3/OK:200:3', 'http://cat-videos.net/4/OK:200:5' ], maintainOrder = False):
r2 = self.default.one(r1.url + '/X/OK:200:1')
for r3 in self.default.swarm([ r2.url + '/A/OK:200:2', r2.url + '/B/OK:200:1', r2.url + '/C/OK:200:1' ], maintainOrder = False):
responses.add(r3.url[22:])
self.assertAlmostEqual(time() - start, 17, delta = 0.1)
self.assertEqual({ '1/X/A', '1/X/B', '1/X/C', '2/X/A', '2/X/B', '2/X/C', '3/X/A', '3/X/B', '3/X/C', '4/X/A', '4/X/B', '4/X/C' }, responses)
self.default.minSecondsBetweenRequests = oldValue
def test_big_swarm_in_swarm_noorder(self):
responses = set()
oldValue = self.default.minSecondsBetweenRequests
self.default.minSecondsBetweenRequests = 0
start = time()
for r1 in self.default.swarm([ 'http://cat-videos.net/1/OK:200:3', 'http://cat-videos.net/2/OK:200:1', 'http://cat-videos.net/3/OK:200:3', 'http://cat-videos.net/4/OK:200:5' ], maintainOrder = False):
r2 = self.default.one(r1.url + '/X/OK:200:1')
for r3 in self.default.swarm([ r2.url + '/A/OK:200:2', r2.url + '/B/OK:200:1', r2.url + '/C/OK:200:1' ], maintainOrder = False):
responses.add(r3.url[22:])
self.assertEqual({ '1/X/A', '1/X/B', '1/X/C', '2/X/A', '2/X/B', '2/X/C', '3/X/A', '3/X/B', '3/X/C', '4/X/A', '4/X/B', '4/X/C' }, responses)
self.default.minSecondsBetweenRequests = oldValue
def test_swarm_in_swarm_noorder1(self):
responses = set()
start = time()
for r1 in self.default.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200' ]):
for r2 in self.default.swarm([ r1.url + '/A/OK:200:1', r1.url + '/B/OK:200', r1.url + '/C/OK:200' ], maintainOrder = False):
responses.add(r2.url)
sleep(0.1)
self.assertAlmostEqual(time() - start, 2.7, delta = 0.04)
self.assertEqual({ 'http://cat-videos.net/1/A', 'http://cat-videos.net/1/B', 'http://cat-videos.net/1/C', 'http://cat-videos.net/2/A', 'http://cat-videos.net/2/B', 'http://cat-videos.net/2/C' }, responses)
def test_swarm_in_swarm_noorder2(self):
responses = set()
start = time()
for r1 in self.highConcurrency.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200' ]):
for r2 in self.highConcurrency.swarm([ r1.url + '/A/OK:200:1', r1.url + '/B/OK:200', r1.url + '/C/OK:200' ], maintainOrder = False):
responses.add(r2.url)
sleep(0.1)
self.assertAlmostEqual(time() - start, 2.6, delta = 0.04)
self.assertEqual({ 'http://cat-videos.net/1/A', 'http://cat-videos.net/1/B', 'http://cat-videos.net/1/C', 'http://cat-videos.net/2/A', 'http://cat-videos.net/2/B', 'http://cat-videos.net/2/C' }, responses)
def test_swarm_in_swarm_order_exception(self):
responses = []
start = time()
for r1 in self.noRaise.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200:1', 'http://cat-videos.net/3/OK:200' ]):
for r2 in self.noRaise.swarm([ r1.url + '/A/Gone:410', r1.url + '/B/OK:200' ]):
responses.append(r2.url)
sleep(0.1)
self.assertAlmostEqual(time() - start, 16.6, delta = 0.08)
self.assertEqual([ 'http://cat-videos.net/1/A', 'http://cat-videos.net/1/B', 'http://cat-videos.net/2/A', 'http://cat-videos.net/2/B', 'http://cat-videos.net/3/A', 'http://cat-videos.net/3/B' ], responses)
def test_swarm_in_swarm_noorder_exception(self):
responses = []
start = time()
for r1 in self.noRaise.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200:1', 'http://cat-videos.net/3/OK:200' ]):
for r2 in self.noRaise.swarm([ r1.url + '/A/Gone:410', r1.url + '/B/OK:200' ], maintainOrder = False):
responses.append(r2.url)
sleep(0.1)
self.assertAlmostEqual(time() - start, 16.3, delta = 0.08)
self.assertEqual([ 'http://cat-videos.net/1/B', 'http://cat-videos.net/1/A', 'http://cat-videos.net/2/B', 'http://cat-videos.net/2/A', 'http://cat-videos.net/3/B', 'http://cat-videos.net/3/A' ], responses)
def test_swarm_stop1(self):
responses = []
start = time()
for r1 in self.noRaise.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200', 'http://cat-videos.net/3/OK:200', 'http://cat-videos.net/4/OK:200' ]):
responses.append(r1.url)
# Without the following sleep to yield, sometimes the third request would be sent,
# and sometimes it wouldn't (depending on whether the pool wait is released after _execute completes,
# or the iterator event is fired in _response)
# The sleep will "guarantee" that the third request is sent
# This also means that the third response will sleep for 0.1 seconds
sleep(0.1)
self.noRaise.stop(killExecuting = False)
self.assertAlmostEqual(time() - start, self.defaultSendTime * 2 + 0.1, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1', 'http://cat-videos.net/2', 'http://cat-videos.net/3' ], responses)
def test_swarm_stop2(self):
responses = []
start = time()
for r1 in self.noRaise.swarm([ 'http://cat-videos.net/1/Test:418', 'http://cat-videos.net/2/OK:200', 'http://cat-videos.net/3/OK:200', 'http://cat-videos.net/4/OK:200', 'http://cat-videos.net/5/OK:200' ], maintainOrder = False):
responses.append(r1.url)
sleep(0.1)
self.noRaise.stop(killExecuting = False)
self.assertAlmostEqual(time() - start, self.defaultSendTime * 2 + self.noRaise.minSecondsBetweenRequests + 0.1, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/2', 'http://cat-videos.net/3', 'http://cat-videos.net/4' ], responses)
def test_swarm_stop3(self):
responses = []
start = time()
for r1 in self.noRaise.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/Test:418', 'http://cat-videos.net/3/OK:200', 'http://cat-videos.net/4/OK:200' ]):
responses.append(r1.url)
sleep(0.1)
self.noRaise.stop(killExecuting = False)
self.assertAlmostEqual(time() - start, self.defaultSendTime * 2 + 0.1, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1', 'http://cat-videos.net/2', 'http://cat-videos.net/3' ], responses)
def test_swarm_stop4(self):
responses = []
start = time()
for r1 in self.noRaise.swarm([ 'http://cat-videos.net/1/Test:418', 'http://cat-videos.net/2/OK:200', 'http://cat-videos.net/3/OK:200', 'http://cat-videos.net/4/OK:200' ]):
responses.append(r1.url)
self.noRaise.stop(killExecuting = False)
self.assertAlmostEqual(time() - start, self.defaultSendTime * 3 + self.defaultRetryWait * 2, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1', 'http://cat-videos.net/2', 'http://cat-videos.net/3', 'http://cat-videos.net/4' ], responses)
def test_swarm_stop5(self):
start = time()
it = self.noRaise.swarm([ 'http://cat-videos.net/1/Test:418' ])
sleep(0.1)
self.noRaise.stop(killExecuting = False)
response = it.next()
self.assertAlmostEqual(time() - start, self.defaultSendTime, delta = 0.04)
self.assertEqual('http://cat-videos.net/1', response.url)
def test_swarm_stop_and_kill1(self):
responses = []
start = time()
for r1 in self.default.swarm([ 'http://cat-videos.net/1/OK:200', 'http://cat-videos.net/2/OK:200', 'http://cat-videos.net/3/OK:200' ]):
responses.append(r1.url)
self.default.stop()
self.assertAlmostEqual(time() - start, 0.4, delta = 0.04)
self.assertEqual([ 'http://cat-videos.net/1' ], responses)
def test_swarm_stop_and_kill2(self):
start = time()
it = self.noRaise.swarm([ 'http://cat-videos.net/1/Test:418' ])
sleep(0.1)
self.noRaise.stop()
try:
it.next()
self.fail()
except StopIteration:
self.assertAlmostEqual(time() - start, 0.1, delta = 0.04)
def test_custom_preprocessor(self):
class CustomPreprocessor(ResponsePreprocessor):
def success(self, bundle):
bundle.response.url += '!'
return bundle.ret()
start = time()
self.assertEqual(self.default.one('http://cat-videos.net/1/OK:200', responsePreprocessor = CustomPreprocessor()).url, 'http://cat-videos.net/1!')
self.assertAlmostEqual(time() - start, 0.4, delta = 0.04)
def test_each(self):
class Obj(object):
def __init__(self, data, request):
self.data = data
self.request = request
responses = []
start = time()
for r1, obj in self.noRaise.each([ Obj('AAA', 'http://cat-videos.net/1/Test:416'), Obj('BBB', 'http://cat-videos.net/2/OK:200') ]):
responses.append(( r1.url, r1.status_code, obj.data ))
self.assertAlmostEqual(time() - start, 5.2, delta = 0.04)
self.assertEqual([ ( 'http://cat-videos.net/2', 200, 'BBB' ), ( 'http://cat-videos.net/1', 416, 'AAA' ) ], responses)
def test_each_custom_map(self):
class Obj(object):
def __init__(self, data, status):
self.data = data
self.status = status
class Mapper(object):
def __init__(self):
self.count = 0
def torequest(self, i):
self.count += 1
return 'http://cat-videos.net/%d/%s' % ( self.count, i.status)
responses = []
start = time()
for r1, obj in self.noRaise.each([ Obj('XXX', 'OK:200:1'), Obj('YYY', 'OK:200') ], mapToRequest = Mapper().torequest):
responses.append(( r1.url, r1.status_code, obj.data ))
self.assertAlmostEqual(time() - start, 1, delta = 0.04)
self.assertEqual([ ( 'http://cat-videos.net/2', 200, 'YYY' ), ( 'http://cat-videos.net/1', 200, 'XXX' ) ], responses)
class Test2RealRequests(TestCase):
def setUp(self):
self.requests = Requests(concurrent = 4)
def url(self, delay, key):
return 'http://httpbin.org/delay/%s?key=%s' % ( delay, key )
def key(self, response):
return response.json()['args']['key']
def test_big_swarm_in_swarm_order(self):
responses = []
start = time()
for r1 in self.requests.swarm([ self.url(3, '1'), self.url(1, '2'), self.url(3, '3'), self.url(5, '4') ]):
r2 = self.requests.one(self.url(1, self.key(r1) + 'x'))
for r3 in self.requests.swarm([ self.url(2, self.key(r2) + 'A'), self.url(1, self.key(r2) + 'B'), self.url(1, self.key(r2) + 'C') ]):
responses.append(self.key(r3))
self.assertLess(time() - start, 30) # Non-async has a minimum bound of 32 seconds
self.assertEqual([ '1xA', '1xB', '1xC', '2xA', '2xB', '2xC', '3xA', '3xB', '3xC', '4xA', '4xB', '4xC' ], responses)
def test_timeout(self):
self.requests.defaultTimeout = 3
start = time()
try:
response = self.key(self.requests.one(self.url(4, 'R')))
self.fail()
except Timeout as e:
self.assertLess(time() - start, 3.3)
self.requests.defaultTimeout = None
def test_timeout_retry(self):
oldValue = self.requests.retryStrategy
self.requests.retryStrategy = Backoff() # Retries a timed-out request after a 10 second wait
self.requests.defaultTimeout = 3
start = time()
try:
response = self.key(self.requests.one(self.url(4, 'S')))
self.fail()
except Timeout:
self.assertLess(time() - start, 16.3)
finally:
self.requests.defaultTimeout = None
self.requests.retryStrategy = oldValue
def test_timeout_retry_noerror(self):
oldValue = self.requests.retryStrategy, self.requests.responsePreprocessor
self.requests.retryStrategy = Backoff() # Retries a timed-out request after a 10 second wait
self.requests.responsePreprocessor = NoRaiseTimeoutError()
self.requests.defaultTimeout = 3
start = time()
response = self.requests.one(self.url(4, 'W'))
self.assertLess(time() - start, 16.3)
self.assertIsNone(response)
self.requests.defaultTimeout = None
self.requests.retryStrategy, self.requests.responsePreprocessor = oldValue
def test_timeout_none(self):
start = time()
response = self.key(self.requests.one(self.url(4, 'Q')))
self.assertLess(time() - start, 5)
self.assertEqual(response, 'Q')
class Test3InFlight(TestCase):
def test_all_swarm_get_executed(self):
requests = Requests()
# Monkey patch the actual send to print the url to console, so we can see if it worked
def fake_send(self, request):
print request.url
requests.session.send = MethodType(fake_send, requests.session, Session)
print '\n*** This is an eyeball test: make sure all 5 urls are printed to the console ***'
requests.swarm([ 'http://cat-videos.net/1-of-5', 'http://cat-videos.net/2-of-5', 'http://cat-videos.net/3-of-5', 'http://cat-videos.net/4-of-5', 'http://cat-videos.net/5-of-5' ])
if __name__ == '__main__':
main(verbosity = 2, catchbreak = True)