-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_octopus.py
199 lines (136 loc) · 5.57 KB
/
test_octopus.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from preggy import expect
from mock import Mock
import requests.exceptions
from octopus import Octopus, TimeoutError, ResponseError
from tests import TestCase
class TestOctopus(TestCase):
def setUp(self):
self.response = None
self.responses = {}
def test_can_create_octopus(self):
otto = Octopus(concurrency=20)
expect(otto.concurrency).to_equal(20)
expect(otto.auto_start).to_be_false()
expect(otto.cache).to_be_false()
def test_has_default_concurrency(self):
otto = Octopus()
expect(otto.concurrency).to_equal(10)
def test_queue_is_empty(self):
otto = Octopus()
expect(otto.is_empty).to_be_true()
def test_can_enqueue_url(self):
otto = Octopus()
otto.enqueue('http://www.google.com', None)
expect(otto.queue_size).to_equal(1)
def test_can_get_after_started(self):
otto = Octopus(concurrency=1)
def handle_url_response(url, response):
self.response = response
otto.enqueue('http://www.google.com', handle_url_response)
otto.start()
otto.wait(5)
expect(self.response).not_to_be_null()
expect(self.response.status_code).to_equal(200)
def test_can_get_with_auto_start(self):
otto = Octopus(concurrency=1, auto_start=True)
def handle_url_response(url, response):
self.response = response
otto.enqueue('http://www.google.com', handle_url_response)
otto.wait(5)
expect(self.response).not_to_be_null()
expect(self.response.status_code).to_equal(200)
def test_can_wait(self):
otto = Octopus(concurrency=1)
def handle_url_response(url, response):
self.response = response
otto.enqueue('http://www.google.com', handle_url_response)
otto.start()
otto.wait(0)
expect(self.response).not_to_be_null()
expect(self.response.status_code).to_equal(200)
def test_wait_returns_automatically_when_empty(self):
otto = Octopus(concurrency=1)
otto.start()
otto.wait(5)
expect(otto.is_empty).to_be_true()
def test_times_out_on_wait(self):
otto = Octopus(concurrency=1)
def handle_url_response(url, response):
self.response = response
otto.enqueue('http://www.google.com', handle_url_response)
try:
otto.wait(0.1)
except TimeoutError:
err = sys.exc_info()[1]
expect(err).to_have_an_error_message_of("")
else:
assert False, "Should not have gotten this far"
def test_can_handle_more_urls_concurrently(self):
urls = [
'http://www.google.com',
'http://www.globo.com',
'http://www.cnn.com',
'http://www.bbc.com'
]
otto = Octopus(concurrency=4)
def handle_url_response(url, response):
self.responses[url] = response
for url in urls:
otto.enqueue(url, handle_url_response)
otto.start()
otto.wait(10)
expect(self.responses).to_length(4)
for url in urls:
expect(self.responses).to_include(url)
expect(self.responses[url].status_code).to_equal(200)
def test_can_handle_cached_responses(self):
response = Mock(status_code=200, body="whatever")
url = 'http://www.google.com'
otto = Octopus(concurrency=1, cache=True)
otto.response_cache.put(url, response)
def handle_url_response(url, response):
self.response = response
otto.enqueue(url, handle_url_response)
expect(self.response).not_to_be_null()
expect(self.response.status_code).to_equal(200)
expect(self.response.body).to_equal("whatever")
def test_can_handle_cached_responses_when_not_cached(self):
url = 'http://www.google.com'
otto = Octopus(concurrency=1, cache=True)
def handle_url_response(url, response):
self.response = response
otto.enqueue(url, handle_url_response)
otto.enqueue(url, handle_url_response)
otto.enqueue(url, handle_url_response)
otto.enqueue(url, handle_url_response)
otto.start()
otto.wait(5)
expect(self.response).not_to_be_null()
expect(self.response.status_code).to_equal(200)
def test_can_handle_invalid_urls(self):
url = 'http://kagdjdkjgka.fk'
otto = Octopus(concurrency=1)
def handle_url_response(url, response):
self.response = response
otto.enqueue(url, handle_url_response)
otto.start()
otto.wait(5)
expect(self.response).not_to_be_null()
expect(self.response).to_be_instance_of(ResponseError)
expect(self.response.status_code).to_equal(500)
expect(self.response.body).to_include("HTTPConnectionPool(host='kagdjdkjgka.fk', port=80)")
expect(self.response.body).to_include('Max retries exceeded with url: /')
expect(self.response.error).to_be_instance_of(requests.exceptions.ConnectionError)
def test_can_handle_timeouts(self):
url = 'http://baidu.com'
otto = Octopus(concurrency=1, request_timeout_in_seconds=0.1)
def handle_url_response(url, response):
self.response = response
otto.enqueue(url, handle_url_response)
otto.start()
otto.wait(5)
expect(self.response.body).to_include('Connection to baidu.com timed out')
expect(self.response.error).to_be_instance_of(requests.exceptions.Timeout)