| 1 |
# Copyright (c) 2007, Linden Research, Inc. |
| 2 |
# Copyright (c) 2007, IBM Corp. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
import gc |
| 17 |
import random |
| 18 |
import re |
| 19 |
import time |
| 20 |
|
| 21 |
import eventlet |
| 22 |
from eventlet import tpool |
| 23 |
import tests |
| 24 |
|
| 25 |
|
| 26 |
one = 1 |
| 27 |
two = 2 |
| 28 |
three = 3 |
| 29 |
none = None |
| 30 |
|
| 31 |
|
| 32 |
def noop(): |
| 33 |
pass |
| 34 |
|
| 35 |
|
| 36 |
def raise_exception(): |
| 37 |
raise RuntimeError("hi") |
| 38 |
|
| 39 |
|
| 40 |
class TestTpool(tests.LimitedTestCase): |
| 41 |
def setUp(self): |
| 42 |
super().setUp() |
| 43 |
|
| 44 |
def tearDown(self): |
| 45 |
tpool.killall() |
| 46 |
super().tearDown() |
| 47 |
|
| 48 |
def test_wrap_tuple(self): |
| 49 |
my_tuple = (1, 2) |
| 50 |
prox = tpool.Proxy(my_tuple) |
| 51 |
self.assertEqual(prox[0], 1) |
| 52 |
self.assertEqual(prox[1], 2) |
| 53 |
self.assertEqual(len(my_tuple), 2) |
| 54 |
|
| 55 |
def test_wrap_string(self): |
| 56 |
my_object = "whatever" |
| 57 |
prox = tpool.Proxy(my_object) |
| 58 |
self.assertEqual(str(my_object), str(prox)) |
| 59 |
self.assertEqual(len(my_object), len(prox)) |
| 60 |
self.assertEqual(my_object.join(['a', 'b']), prox.join(['a', 'b'])) |
| 61 |
|
| 62 |
def test_wrap_uniterable(self): |
| 63 |
prox = tpool.Proxy([]) |
| 64 |
|
| 65 |
def index(): |
| 66 |
prox[0] |
| 67 |
|
| 68 |
def key(): |
| 69 |
prox['a'] |
| 70 |
|
| 71 |
self.assertRaises(IndexError, index) |
| 72 |
self.assertRaises(TypeError, key) |
| 73 |
|
| 74 |
def test_wrap_dict(self): |
| 75 |
my_object = {'a': 1} |
| 76 |
prox = tpool.Proxy(my_object) |
| 77 |
self.assertEqual('a', list(prox.keys())[0]) |
| 78 |
self.assertEqual(1, prox['a']) |
| 79 |
self.assertEqual(str(my_object), str(prox)) |
| 80 |
self.assertEqual(repr(my_object), repr(prox)) |
| 81 |
|
| 82 |
def test_wrap_module_class(self): |
| 83 |
prox = tpool.Proxy(re) |
| 84 |
self.assertEqual(tpool.Proxy, type(prox)) |
| 85 |
exp = prox.compile('(.)(.)(.)') |
| 86 |
self.assertEqual(exp.groups, 3) |
| 87 |
assert repr(prox.compile) |
| 88 |
|
| 89 |
def test_wrap_eq(self): |
| 90 |
prox = tpool.Proxy(re) |
| 91 |
exp1 = prox.compile('.') |
| 92 |
exp2 = prox.compile(exp1.pattern) |
| 93 |
self.assertEqual(exp1, exp2) |
| 94 |
exp3 = prox.compile('/') |
| 95 |
assert exp1 != exp3 |
| 96 |
|
| 97 |
def test_wrap_ints(self): |
| 98 |
p = tpool.Proxy(4) |
| 99 |
assert p == 4 |
| 100 |
|
| 101 |
def test_wrap_hash(self): |
| 102 |
prox1 = tpool.Proxy('' + 'A') |
| 103 |
prox2 = tpool.Proxy('A' + '') |
| 104 |
assert prox1 == 'A' |
| 105 |
assert 'A' == prox2 |
| 106 |
# assert prox1 == prox2 FIXME - could __eq__ unwrap rhs if it is other proxy? |
| 107 |
self.assertEqual(hash(prox1), hash(prox2)) |
| 108 |
proxList = tpool.Proxy([]) |
| 109 |
self.assertRaises(TypeError, hash, proxList) |
| 110 |
|
| 111 |
def test_wrap_nonzero(self): |
| 112 |
prox = tpool.Proxy(re) |
| 113 |
exp1 = prox.compile('.') |
| 114 |
assert bool(exp1) |
| 115 |
prox2 = tpool.Proxy([1, 2, 3]) |
| 116 |
assert bool(prox2) |
| 117 |
|
| 118 |
def test_multiple_wraps(self): |
| 119 |
prox1 = tpool.Proxy(re) |
| 120 |
prox2 = tpool.Proxy(re) |
| 121 |
prox1.compile('.') |
| 122 |
x2 = prox1.compile('.') |
| 123 |
del x2 |
| 124 |
prox2.compile('.') |
| 125 |
|
| 126 |
def test_wrap_getitem(self): |
| 127 |
prox = tpool.Proxy([0, 1, 2]) |
| 128 |
self.assertEqual(prox[0], 0) |
| 129 |
|
| 130 |
def test_wrap_setitem(self): |
| 131 |
prox = tpool.Proxy([0, 1, 2]) |
| 132 |
prox[1] = 2 |
| 133 |
self.assertEqual(prox[1], 2) |
| 134 |
|
| 135 |
def test_wrap_iterator(self): |
| 136 |
self.reset_timeout(2) |
| 137 |
prox = tpool.Proxy(range(10)) |
| 138 |
result = [] |
| 139 |
for i in prox: |
| 140 |
result.append(i) |
| 141 |
self.assertEqual(list(range(10)), result) |
| 142 |
|
| 143 |
def test_wrap_iterator2(self): |
| 144 |
self.reset_timeout(5) # might take a while due to imprecise sleeping |
| 145 |
|
| 146 |
def foo(): |
| 147 |
import time |
| 148 |
for x in range(2): |
| 149 |
yield x |
| 150 |
time.sleep(0.001) |
| 151 |
|
| 152 |
counter = [0] |
| 153 |
|
| 154 |
def tick(): |
| 155 |
for i in range(20000): |
| 156 |
counter[0] += 1 |
| 157 |
if counter[0] % 20 == 0: |
| 158 |
eventlet.sleep(0.0001) |
| 159 |
else: |
| 160 |
eventlet.sleep() |
| 161 |
|
| 162 |
gt = eventlet.spawn(tick) |
| 163 |
previtem = 0 |
| 164 |
for item in tpool.Proxy(foo()): |
| 165 |
assert item >= previtem |
| 166 |
# make sure the tick happened at least a few times so that we know |
| 167 |
# that our iterations in foo() were actually tpooled |
| 168 |
assert counter[0] > 10, counter[0] |
| 169 |
gt.kill() |
| 170 |
|
| 171 |
def test_raising_exceptions(self): |
| 172 |
prox = tpool.Proxy(re) |
| 173 |
|
| 174 |
def nofunc(): |
| 175 |
prox.never_name_a_function_like_this() |
| 176 |
self.assertRaises(AttributeError, nofunc) |
| 177 |
|
| 178 |
from tests import tpool_test |
| 179 |
prox = tpool.Proxy(tpool_test) |
| 180 |
self.assertRaises(RuntimeError, prox.raise_exception) |
| 181 |
|
| 182 |
def test_variable_and_keyword_arguments_with_function_calls(self): |
| 183 |
import optparse |
| 184 |
parser = tpool.Proxy(optparse.OptionParser()) |
| 185 |
parser.add_option('-n', action='store', type='string', dest='n') |
| 186 |
opts, args = parser.parse_args(["-nfoo"]) |
| 187 |
self.assertEqual(opts.n, 'foo') |
| 188 |
|
| 189 |
def test_contention(self): |
| 190 |
from tests import tpool_test |
| 191 |
prox = tpool.Proxy(tpool_test) |
| 192 |
|
| 193 |
pile = eventlet.GreenPile(4) |
| 194 |
pile.spawn(lambda: self.assertEqual(prox.one, 1)) |
| 195 |
pile.spawn(lambda: self.assertEqual(prox.two, 2)) |
| 196 |
pile.spawn(lambda: self.assertEqual(prox.three, 3)) |
| 197 |
results = list(pile) |
| 198 |
self.assertEqual(len(results), 3) |
| 199 |
|
| 200 |
def test_timeout(self): |
| 201 |
blocking = eventlet.patcher.original('time') |
| 202 |
eventlet.Timeout(0.1, eventlet.Timeout()) |
| 203 |
try: |
| 204 |
tpool.execute(blocking.sleep, 0.3) |
| 205 |
assert False, 'Expected Timeout' |
| 206 |
except eventlet.Timeout: |
| 207 |
pass |
| 208 |
|
| 209 |
def test_killall(self): |
| 210 |
tpool.killall() |
| 211 |
tpool.setup() |
| 212 |
|
| 213 |
def test_killall_remaining_results(self): |
| 214 |
semaphore = eventlet.Event() |
| 215 |
|
| 216 |
def native_fun(): |
| 217 |
time.sleep(.5) |
| 218 |
|
| 219 |
def gt_fun(): |
| 220 |
semaphore.send(None) |
| 221 |
tpool.execute(native_fun) |
| 222 |
|
| 223 |
gt = eventlet.spawn(gt_fun) |
| 224 |
semaphore.wait() |
| 225 |
tpool.killall() |
| 226 |
gt.wait() |
| 227 |
|
| 228 |
def test_autowrap(self): |
| 229 |
x = tpool.Proxy({'a': 1, 'b': 2}, autowrap=(int,)) |
| 230 |
assert isinstance(x.get('a'), tpool.Proxy) |
| 231 |
assert not isinstance(x.items(), tpool.Proxy) |
| 232 |
# attributes as well as callables |
| 233 |
from tests import tpool_test |
| 234 |
x = tpool.Proxy(tpool_test, autowrap=(int,)) |
| 235 |
assert isinstance(x.one, tpool.Proxy) |
| 236 |
assert not isinstance(x.none, tpool.Proxy) |
| 237 |
|
| 238 |
def test_autowrap_names(self): |
| 239 |
x = tpool.Proxy({'a': 1, 'b': 2}, autowrap_names=('get',)) |
| 240 |
assert isinstance(x.get('a'), tpool.Proxy) |
| 241 |
assert not isinstance(x.items(), tpool.Proxy) |
| 242 |
from tests import tpool_test |
| 243 |
x = tpool.Proxy(tpool_test, autowrap_names=('one',)) |
| 244 |
assert isinstance(x.one, tpool.Proxy) |
| 245 |
assert not isinstance(x.two, tpool.Proxy) |
| 246 |
|
| 247 |
def test_autowrap_both(self): |
| 248 |
from tests import tpool_test |
| 249 |
x = tpool.Proxy(tpool_test, autowrap=(int,), autowrap_names=('one',)) |
| 250 |
assert isinstance(x.one, tpool.Proxy) |
| 251 |
# violating the abstraction to check that we didn't double-wrap |
| 252 |
assert not isinstance(x._obj, tpool.Proxy) |
| 253 |
|
| 254 |
def test_callable(self): |
| 255 |
def wrapped(arg): |
| 256 |
return arg |
| 257 |
x = tpool.Proxy(wrapped) |
| 258 |
self.assertEqual(4, x(4)) |
| 259 |
# verify that it wraps return values if specified |
| 260 |
x = tpool.Proxy(wrapped, autowrap_names=('__call__',)) |
| 261 |
assert isinstance(x(4), tpool.Proxy) |
| 262 |
self.assertEqual("4", str(x(4))) |
| 263 |
|
| 264 |
def test_callable_iterator(self): |
| 265 |
def wrapped(arg): |
| 266 |
yield arg |
| 267 |
yield arg |
| 268 |
yield arg |
| 269 |
|
| 270 |
x = tpool.Proxy(wrapped, autowrap_names=('__call__',)) |
| 271 |
for r in x(3): |
| 272 |
self.assertEqual(3, r) |
| 273 |
|
| 274 |
def test_eventlet_timeout(self): |
| 275 |
def raise_timeout(): |
| 276 |
raise eventlet.Timeout() |
| 277 |
self.assertRaises(eventlet.Timeout, tpool.execute, raise_timeout) |
| 278 |
|
| 279 |
def test_tpool_set_num_threads(self): |
| 280 |
tpool.set_num_threads(5) |
| 281 |
self.assertEqual(5, tpool._nthreads) |
| 282 |
|
| 283 |
|
| 284 |
class TpoolLongTests(tests.LimitedTestCase): |
| 285 |
TEST_TIMEOUT = 60 |
| 286 |
|
| 287 |
def test_a_buncha_stuff(self): |
| 288 |
class Dummy: |
| 289 |
def foo(self, when, token=None): |
| 290 |
assert token is not None |
| 291 |
time.sleep(random.random() / 200.0) |
| 292 |
return token |
| 293 |
|
| 294 |
def sender_loop(loopnum): |
| 295 |
obj = tpool.Proxy(Dummy()) |
| 296 |
count = 100 |
| 297 |
for n in range(count): |
| 298 |
eventlet.sleep(random.random() / 200.0) |
| 299 |
now = time.time() |
| 300 |
token = loopnum * count + n |
| 301 |
rv = obj.foo(now, token=token) |
| 302 |
self.assertEqual(token, rv) |
| 303 |
eventlet.sleep(random.random() / 200.0) |
| 304 |
|
| 305 |
cnt = 10 |
| 306 |
pile = eventlet.GreenPile(cnt) |
| 307 |
for i in range(cnt): |
| 308 |
pile.spawn(sender_loop, i) |
| 309 |
results = list(pile) |
| 310 |
self.assertEqual(len(results), cnt) |
| 311 |
tpool.killall() |
| 312 |
|
| 313 |
def test_leakage_from_tracebacks(self): |
| 314 |
tpool.execute(noop) # get it started |
| 315 |
gc.collect() |
| 316 |
initial_objs = len(gc.get_objects()) |
| 317 |
for i in range(10): |
| 318 |
self.assertRaises(RuntimeError, tpool.execute, raise_exception) |
| 319 |
gc.collect() |
| 320 |
middle_objs = len(gc.get_objects()) |
| 321 |
# some objects will inevitably be created by the previous loop |
| 322 |
# now we test to ensure that running the loop an order of |
| 323 |
# magnitude more doesn't generate additional objects |
| 324 |
for i in range(100): |
| 325 |
self.assertRaises(RuntimeError, tpool.execute, raise_exception) |
| 326 |
first_created = middle_objs - initial_objs |
| 327 |
gc.collect() |
| 328 |
second_created = len(gc.get_objects()) - middle_objs |
| 329 |
assert second_created - first_created < 10, "first loop: {}, second loop: {}".format(first_created, second_created) |
| 330 |
tpool.killall() |
| 331 |
|
| 332 |
|
| 333 |
def test_isolate_from_socket_default_timeout(): |
| 334 |
tests.run_isolated('tpool_isolate_socket_default_timeout.py', timeout=5) |
| 335 |
|
| 336 |
|
| 337 |
def test_exception_leak(): |
| 338 |
tests.run_isolated('tpool_exception_leak.py') |