Revision 9c6c3d69
b/kamaki/clients/__init__.py | ||
---|---|---|
107 | 107 |
|
108 | 108 |
|
109 | 109 |
class Client(object): |
110 |
MAX_THREADS = 7 |
|
111 | 110 |
|
112 | 111 |
def __init__(self, base_url, token, http_client=KamakiHTTPConnection()): |
113 | 112 |
self.base_url = base_url |
... | ... | |
118 | 117 |
'%A, %d-%b-%y %H:%M:%S GMT', |
119 | 118 |
'%a, %d %b %Y %H:%M:%S GMT'] |
120 | 119 |
self.http_client = http_client |
120 |
self.MAX_THREADS = 7 |
|
121 | 121 |
|
122 | 122 |
def _init_thread_limit(self, limit=1): |
123 |
assert isinstance(limit, int) and limit > 0, 'Thread limit not a +int' |
|
123 | 124 |
self._thread_limit = limit |
124 | 125 |
self._elapsed_old = 0.0 |
125 | 126 |
self._elapsed_new = 0.0 |
126 | 127 |
|
127 | 128 |
def _watch_thread_limit(self, threadlist): |
129 |
self._thread_limit = getattr(self, '_thread_limit', 1) |
|
130 |
self._elapsed_new = getattr(self, '_elapsed_new', 0.0) |
|
131 |
self._elapsed_old = getattr(self, '_elapsed_old', 0.0) |
|
128 | 132 |
recvlog.debug('# running threads: %s' % len(threadlist)) |
129 |
if (self._elapsed_old > self._elapsed_new) and (
|
|
133 |
if self._elapsed_old and self._elapsed_old >= self._elapsed_new and (
|
|
130 | 134 |
self._thread_limit < self.MAX_THREADS): |
131 | 135 |
self._thread_limit += 1 |
132 |
elif self._elapsed_old < self._elapsed_new and self._thread_limit > 1: |
|
136 |
elif self._elapsed_old <= self._elapsed_new and self._thread_limit > 1:
|
|
133 | 137 |
self._thread_limit -= 1 |
134 | 138 |
|
135 | 139 |
self._elapsed_old = self._elapsed_new |
b/kamaki/clients/test.py | ||
---|---|---|
35 | 35 |
from time import sleep |
36 | 36 |
from inspect import getmembers, isclass |
37 | 37 |
from json import loads |
38 |
from random import randint |
|
38 | 39 |
|
39 | 40 |
from kamaki.clients.connection.test import ( |
40 | 41 |
KamakiConnection, |
... | ... | |
182 | 183 |
self.assertEqual(self.client.DATE_FORMATS, DATE_FORMATS) |
183 | 184 |
self.assertTrue(isinstance(self.client.http_client, FakeConnection)) |
184 | 185 |
|
186 |
def test__init_thread_limit(self): |
|
187 |
exp = 'Nothing set here' |
|
188 |
for faulty in (-1, 0.5, 'a string', {}): |
|
189 |
self.assertRaises( |
|
190 |
AssertionError, |
|
191 |
self.client._init_thread_limit, |
|
192 |
faulty) |
|
193 |
self.assertEqual(exp, getattr(self.client, '_thread_limit', exp)) |
|
194 |
self.assertEqual(exp, getattr(self.client, '_elapsed_old', exp)) |
|
195 |
self.assertEqual(exp, getattr(self.client, '_elapsed_new', exp)) |
|
196 |
self.client._init_thread_limit(42) |
|
197 |
self.assertEqual(42, self.client._thread_limit) |
|
198 |
self.assertEqual(0.0, self.client._elapsed_old) |
|
199 |
self.assertEqual(0.0, self.client._elapsed_new) |
|
200 |
|
|
201 |
def test__watch_thread_limit(self): |
|
202 |
waits = ( |
|
203 |
dict(args=((0.1, 1), (0.1, 2), (0.2, 1), (0.7, 1), (0.3, 2))), |
|
204 |
dict(args=((1.0 - (i / 10.0), (i + 1)) for i in range(7))), |
|
205 |
dict(max=1, args=tuple([(randint(1, 10) / 3.0, 1), ] * 10)), |
|
206 |
dict( |
|
207 |
limit=5, |
|
208 |
args=tuple([ |
|
209 |
(1.0 + (i / 10.0), (5 - i - 1)) for i in range(4)] + [ |
|
210 |
(2.0, 1), (1.9, 2), (2.0, 1), (2.0, 2)])), |
|
211 |
dict(args=tuple( |
|
212 |
[(1.0 - (i / 10.0), (i + 1)) for i in range(7)] + [ |
|
213 |
(0.1, 7), (0.2, 6), (0.4, 5), (0.3, 6), (0.2, 7), (0.1, 7)])),) |
|
214 |
for wait_dict in waits: |
|
215 |
if 'max' in wait_dict: |
|
216 |
self.client.MAX_THREADS = wait_dict['max'] |
|
217 |
else: |
|
218 |
self.client.MAX_THREADS = 7 |
|
219 |
if 'limit' in wait_dict: |
|
220 |
self.client._init_thread_limit(wait_dict['limit']) |
|
221 |
else: |
|
222 |
self.client._init_thread_limit() |
|
223 |
self.client._watch_thread_limit(list()) |
|
224 |
self.assertEqual(1, self.client._thread_limit) |
|
225 |
for wait, exp_limit in wait_dict['args']: |
|
226 |
self.client._elapsed_new = wait |
|
227 |
self.client._watch_thread_limit(list()) |
|
228 |
self.assertEqual(exp_limit, self.client._thread_limit) |
|
229 |
|
|
185 | 230 |
|
186 | 231 |
# TestCase auxiliary methods |
187 | 232 |
|
Also available in: Unified diff