| 1 |
import errno |
| 2 |
import fcntl |
| 3 |
import os |
| 4 |
import sys |
| 5 |
import time |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
import tests |
| 10 |
from tests import skip_if_no_itimer, skip_unless |
| 11 |
import eventlet |
| 12 |
from eventlet import debug, hubs |
| 13 |
from eventlet.hubs.asyncio import Hub as AsyncioHub |
| 14 |
from eventlet.support import greenlets |
| 15 |
|
| 16 |
|
| 17 |
DELAY = 0.001 |
| 18 |
|
| 19 |
|
| 20 |
def noop(): |
| 21 |
pass |
| 22 |
|
| 23 |
|
| 24 |
class TestTimerCleanup(tests.LimitedTestCase): |
| 25 |
TEST_TIMEOUT = 2 |
| 26 |
|
| 27 |
def test_cancel_immediate(self): |
| 28 |
hub = hubs.get_hub() |
| 29 |
stimers = hub.get_timers_count() |
| 30 |
scanceled = hub.timers_canceled |
| 31 |
for i in range(2000): |
| 32 |
t = hubs.get_hub().schedule_call_global(60, noop) |
| 33 |
t.cancel() |
| 34 |
self.assert_less_than_equal(hub.timers_canceled, |
| 35 |
hub.get_timers_count() + 1) |
| 36 |
# there should be fewer than 1000 new timers and canceled |
| 37 |
self.assert_less_than_equal(hub.get_timers_count(), 1000 + stimers) |
| 38 |
self.assert_less_than_equal(hub.timers_canceled, 1000) |
| 39 |
|
| 40 |
def test_cancel_accumulated(self): |
| 41 |
hub = hubs.get_hub() |
| 42 |
stimers = hub.get_timers_count() |
| 43 |
scanceled = hub.timers_canceled |
| 44 |
for i in range(2000): |
| 45 |
t = hubs.get_hub().schedule_call_global(60, noop) |
| 46 |
eventlet.sleep() |
| 47 |
self.assert_less_than_equal(hub.timers_canceled, |
| 48 |
hub.get_timers_count() + 1) |
| 49 |
t.cancel() |
| 50 |
self.assert_less_than_equal(hub.timers_canceled, |
| 51 |
hub.get_timers_count() + 1, hub.timers) |
| 52 |
# there should be fewer than 1000 new timers and canceled |
| 53 |
self.assert_less_than_equal(hub.get_timers_count(), 1000 + stimers) |
| 54 |
self.assert_less_than_equal(hub.timers_canceled, 1000) |
| 55 |
|
| 56 |
def test_cancel_proportion(self): |
| 57 |
# if fewer than half the pending timers are canceled, it should |
| 58 |
# not clean them out |
| 59 |
hub = hubs.get_hub() |
| 60 |
uncanceled_timers = [] |
| 61 |
stimers = hub.get_timers_count() |
| 62 |
scanceled = hub.timers_canceled |
| 63 |
for i in range(1000): |
| 64 |
# 2/3rds of new timers are uncanceled |
| 65 |
t = hubs.get_hub().schedule_call_global(60, noop) |
| 66 |
t2 = hubs.get_hub().schedule_call_global(60, noop) |
| 67 |
t3 = hubs.get_hub().schedule_call_global(60, noop) |
| 68 |
eventlet.sleep() |
| 69 |
self.assert_less_than_equal(hub.timers_canceled, |
| 70 |
hub.get_timers_count() + 1) |
| 71 |
t.cancel() |
| 72 |
self.assert_less_than_equal(hub.timers_canceled, |
| 73 |
hub.get_timers_count() + 1) |
| 74 |
uncanceled_timers.append(t2) |
| 75 |
uncanceled_timers.append(t3) |
| 76 |
# 3000 new timers, plus a few extras |
| 77 |
self.assert_less_than_equal(stimers + 3000, |
| 78 |
stimers + hub.get_timers_count()) |
| 79 |
self.assertEqual(hub.timers_canceled, 1000) |
| 80 |
for t in uncanceled_timers: |
| 81 |
t.cancel() |
| 82 |
self.assert_less_than_equal(hub.timers_canceled, |
| 83 |
hub.get_timers_count()) |
| 84 |
eventlet.sleep() |
| 85 |
|
| 86 |
|
| 87 |
@pytest.mark.skipif(isinstance(hubs.get_hub(), AsyncioHub), |
| 88 |
reason="Asyncio hub doesn't yet support multiple readers") |
| 89 |
class TestMultipleListenersCleanup(tests.LimitedTestCase): |
| 90 |
def setUp(self): |
| 91 |
super().setUp() |
| 92 |
debug.hub_prevent_multiple_readers(False) |
| 93 |
debug.hub_exceptions(False) |
| 94 |
|
| 95 |
def tearDown(self): |
| 96 |
super().tearDown() |
| 97 |
debug.hub_prevent_multiple_readers(True) |
| 98 |
debug.hub_exceptions(True) |
| 99 |
|
| 100 |
def test_cleanup(self): |
| 101 |
r, w = os.pipe() |
| 102 |
self.addCleanup(os.close, r) |
| 103 |
self.addCleanup(os.close, w) |
| 104 |
|
| 105 |
fcntl.fcntl(r, fcntl.F_SETFL, |
| 106 |
fcntl.fcntl(r, fcntl.F_GETFL) | os.O_NONBLOCK) |
| 107 |
|
| 108 |
def readfd(fd): |
| 109 |
while True: |
| 110 |
try: |
| 111 |
return os.read(fd, 1) |
| 112 |
except OSError as e: |
| 113 |
if e.errno != errno.EAGAIN: |
| 114 |
raise |
| 115 |
hubs.trampoline(fd, read=True) |
| 116 |
|
| 117 |
first_listener = eventlet.spawn(readfd, r) |
| 118 |
eventlet.sleep() |
| 119 |
|
| 120 |
second_listener = eventlet.spawn(readfd, r) |
| 121 |
eventlet.sleep() |
| 122 |
|
| 123 |
hubs.get_hub().schedule_call_global(0, second_listener.throw, |
| 124 |
eventlet.Timeout(None)) |
| 125 |
eventlet.sleep() |
| 126 |
|
| 127 |
os.write(w, b'.') |
| 128 |
self.assertEqual(first_listener.wait(), b'.') |
| 129 |
|
| 130 |
|
| 131 |
class TestScheduleCall(tests.LimitedTestCase): |
| 132 |
|
| 133 |
def test_local(self): |
| 134 |
lst = [1] |
| 135 |
eventlet.spawn(hubs.get_hub().schedule_call_local, DELAY, lst.pop) |
| 136 |
eventlet.sleep(0) |
| 137 |
eventlet.sleep(DELAY * 2) |
| 138 |
assert lst == [1], lst |
| 139 |
|
| 140 |
def test_global(self): |
| 141 |
lst = [1] |
| 142 |
eventlet.spawn(hubs.get_hub().schedule_call_global, DELAY, lst.pop) |
| 143 |
eventlet.sleep(0) |
| 144 |
eventlet.sleep(DELAY * 2) |
| 145 |
assert lst == [], lst |
| 146 |
|
| 147 |
def test_ordering(self): |
| 148 |
lst = [] |
| 149 |
hubs.get_hub().schedule_call_global(DELAY * 2, lst.append, 3) |
| 150 |
hubs.get_hub().schedule_call_global(DELAY, lst.append, 1) |
| 151 |
hubs.get_hub().schedule_call_global(DELAY, lst.append, 2) |
| 152 |
while len(lst) < 3: |
| 153 |
eventlet.sleep(DELAY) |
| 154 |
self.assertEqual(lst, [1, 2, 3]) |
| 155 |
|
| 156 |
|
| 157 |
class TestDebug(tests.LimitedTestCase): |
| 158 |
|
| 159 |
def test_debug_listeners(self): |
| 160 |
hubs.get_hub().set_debug_listeners(True) |
| 161 |
hubs.get_hub().set_debug_listeners(False) |
| 162 |
|
| 163 |
def test_timer_exceptions(self): |
| 164 |
hubs.get_hub().set_timer_exceptions(True) |
| 165 |
hubs.get_hub().set_timer_exceptions(False) |
| 166 |
|
| 167 |
|
| 168 |
class TestExceptionInMainloop(tests.LimitedTestCase): |
| 169 |
|
| 170 |
def test_sleep(self): |
| 171 |
# even if there was an error in the mainloop, the hub should continue |
| 172 |
# to work |
| 173 |
start = time.time() |
| 174 |
eventlet.sleep(DELAY) |
| 175 |
delay = time.time() - start |
| 176 |
|
| 177 |
assert delay >= DELAY * \ |
| 178 |
0.9, 'sleep returned after %s seconds (was scheduled for %s)' % ( |
| 179 |
delay, DELAY) |
| 180 |
|
| 181 |
def fail(): |
| 182 |
1 // 0 |
| 183 |
|
| 184 |
hubs.get_hub().schedule_call_global(0, fail) |
| 185 |
|
| 186 |
start = time.time() |
| 187 |
eventlet.sleep(DELAY) |
| 188 |
delay = time.time() - start |
| 189 |
|
| 190 |
assert delay >= DELAY * \ |
| 191 |
0.9, 'sleep returned after %s seconds (was scheduled for %s)' % ( |
| 192 |
delay, DELAY) |
| 193 |
|
| 194 |
|
| 195 |
class TestExceptionInGreenthread(tests.LimitedTestCase): |
| 196 |
|
| 197 |
def test_exceptionpreservation(self): |
| 198 |
# events for controlling execution order |
| 199 |
gt1event = eventlet.Event() |
| 200 |
gt2event = eventlet.Event() |
| 201 |
|
| 202 |
def test_gt1(): |
| 203 |
try: |
| 204 |
raise KeyError() |
| 205 |
except KeyError: |
| 206 |
gt1event.send('exception') |
| 207 |
gt2event.wait() |
| 208 |
assert sys.exc_info()[0] is KeyError |
| 209 |
gt1event.send('test passed') |
| 210 |
|
| 211 |
def test_gt2(): |
| 212 |
gt1event.wait() |
| 213 |
gt1event.reset() |
| 214 |
assert sys.exc_info()[0] is None |
| 215 |
try: |
| 216 |
raise ValueError() |
| 217 |
except ValueError: |
| 218 |
gt2event.send('exception') |
| 219 |
gt1event.wait() |
| 220 |
assert sys.exc_info()[0] is ValueError |
| 221 |
|
| 222 |
g1 = eventlet.spawn(test_gt1) |
| 223 |
g2 = eventlet.spawn(test_gt2) |
| 224 |
try: |
| 225 |
g1.wait() |
| 226 |
g2.wait() |
| 227 |
finally: |
| 228 |
g1.kill() |
| 229 |
g2.kill() |
| 230 |
|
| 231 |
def test_exceptionleaks(self): |
| 232 |
# tests expected behaviour with all versions of greenlet |
| 233 |
def test_gt(sem): |
| 234 |
try: |
| 235 |
raise KeyError() |
| 236 |
except KeyError: |
| 237 |
sem.release() |
| 238 |
hubs.get_hub().switch() |
| 239 |
|
| 240 |
# semaphores for controlling execution order |
| 241 |
sem = eventlet.Semaphore() |
| 242 |
sem.acquire() |
| 243 |
g = eventlet.spawn(test_gt, sem) |
| 244 |
try: |
| 245 |
sem.acquire() |
| 246 |
assert sys.exc_info()[0] is None |
| 247 |
finally: |
| 248 |
g.kill() |
| 249 |
|
| 250 |
|
| 251 |
class TestHubBlockingDetector(tests.LimitedTestCase): |
| 252 |
TEST_TIMEOUT = 10 |
| 253 |
|
| 254 |
def test_block_detect(self): |
| 255 |
def look_im_blocking(): |
| 256 |
import time |
| 257 |
time.sleep(2) |
| 258 |
from eventlet import debug |
| 259 |
debug.hub_blocking_detection(True) |
| 260 |
gt = eventlet.spawn(look_im_blocking) |
| 261 |
self.assertRaises(RuntimeError, gt.wait) |
| 262 |
debug.hub_blocking_detection(False) |
| 263 |
|
| 264 |
@tests.skip_if_no_itimer |
| 265 |
def test_block_detect_with_itimer(self): |
| 266 |
def look_im_blocking(): |
| 267 |
import time |
| 268 |
time.sleep(0.5) |
| 269 |
|
| 270 |
from eventlet import debug |
| 271 |
debug.hub_blocking_detection(True, resolution=0.1) |
| 272 |
gt = eventlet.spawn(look_im_blocking) |
| 273 |
self.assertRaises(RuntimeError, gt.wait) |
| 274 |
debug.hub_blocking_detection(False) |
| 275 |
|
| 276 |
|
| 277 |
class TestSuspend(tests.LimitedTestCase): |
| 278 |
TEST_TIMEOUT = 4 |
| 279 |
longMessage = True |
| 280 |
maxDiff = None |
| 281 |
|
| 282 |
def test_suspend_doesnt_crash(self): |
| 283 |
import os |
| 284 |
import shutil |
| 285 |
import signal |
| 286 |
import subprocess |
| 287 |
import sys |
| 288 |
import tempfile |
| 289 |
self.tempdir = tempfile.mkdtemp('test_suspend') |
| 290 |
filename = os.path.join(self.tempdir, 'test_suspend.py') |
| 291 |
fd = open(filename, "w") |
| 292 |
fd.write("""import eventlet |
| 293 |
eventlet.Timeout(0.5) |
| 294 |
try: |
| 295 |
eventlet.listen(("127.0.0.1", 0)).accept() |
| 296 |
except eventlet.Timeout: |
| 297 |
print("exited correctly") |
| 298 |
""") |
| 299 |
fd.close() |
| 300 |
python_path = os.pathsep.join(sys.path + [self.tempdir]) |
| 301 |
new_env = os.environ.copy() |
| 302 |
new_env['PYTHONPATH'] = python_path |
| 303 |
p = subprocess.Popen([sys.executable, |
| 304 |
os.path.join(self.tempdir, filename)], |
| 305 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=new_env) |
| 306 |
eventlet.sleep(0.4) # wait for process to hit accept |
| 307 |
os.kill(p.pid, signal.SIGSTOP) # suspend and resume to generate EINTR |
| 308 |
os.kill(p.pid, signal.SIGCONT) |
| 309 |
output, _ = p.communicate() |
| 310 |
lines = output.decode('utf-8', 'replace').splitlines() |
| 311 |
assert "exited correctly" in lines[-1], output |
| 312 |
shutil.rmtree(self.tempdir) |
| 313 |
|
| 314 |
|
| 315 |
def test_repeated_select_bad_fd(): |
| 316 |
from eventlet.green import select |
| 317 |
|
| 318 |
def once(): |
| 319 |
try: |
| 320 |
select.select([-1], [], []) |
| 321 |
assert False, 'Expected ValueError' |
| 322 |
except ValueError: |
| 323 |
pass |
| 324 |
|
| 325 |
once() |
| 326 |
once() |
| 327 |
|
| 328 |
|
| 329 |
@pytest.mark.skipif(sys.platform == "darwin", reason="on macOS using fork() is discouraged") |
| 330 |
def test_fork(): |
| 331 |
tests.run_isolated('hub_fork.py') |
| 332 |
|
| 333 |
|
| 334 |
@pytest.mark.skipif(sys.platform == "darwin", reason="on macOS using fork() is discouraged") |
| 335 |
def test_fork_simple(): |
| 336 |
tests.run_isolated('hub_fork_simple.py') |
| 337 |
|
| 338 |
|
| 339 |
class TestDeadRunLoop(tests.LimitedTestCase): |
| 340 |
TEST_TIMEOUT = 2 |
| 341 |
|
| 342 |
class CustomException(Exception): |
| 343 |
pass |
| 344 |
|
| 345 |
def test_kill(self): |
| 346 |
""" Checks that killing a process after the hub runloop dies does |
| 347 |
not immediately return to hub greenlet's parent and schedule a |
| 348 |
redundant timer. """ |
| 349 |
hub = hubs.get_hub() |
| 350 |
|
| 351 |
def dummyproc(): |
| 352 |
hub.switch() |
| 353 |
|
| 354 |
g = eventlet.spawn(dummyproc) |
| 355 |
eventlet.sleep(0) # let dummyproc run |
| 356 |
assert hub.greenlet.parent == eventlet.greenthread.getcurrent() |
| 357 |
self.assertRaises(KeyboardInterrupt, hub.greenlet.throw, |
| 358 |
KeyboardInterrupt()) |
| 359 |
|
| 360 |
# kill dummyproc, this schedules a timer to return execution to |
| 361 |
# this greenlet before throwing an exception in dummyproc. |
| 362 |
# it is from this timer that execution should be returned to this |
| 363 |
# greenlet, and not by propagating of the terminating greenlet. |
| 364 |
g.kill() |
| 365 |
with eventlet.Timeout(0.5, self.CustomException()): |
| 366 |
# we now switch to the hub, there should be no existing timers |
| 367 |
# that switch back to this greenlet and so this hub.switch() |
| 368 |
# call should block indefinitely. |
| 369 |
self.assertRaises(self.CustomException, hub.switch) |
| 370 |
|
| 371 |
def test_parent(self): |
| 372 |
""" Checks that a terminating greenthread whose parent |
| 373 |
was a previous, now-defunct hub greenlet returns execution to |
| 374 |
the hub runloop and not the hub greenlet's parent. """ |
| 375 |
hub = hubs.get_hub() |
| 376 |
|
| 377 |
def dummyproc(): |
| 378 |
pass |
| 379 |
|
| 380 |
g = eventlet.spawn(dummyproc) |
| 381 |
assert hub.greenlet.parent == eventlet.greenthread.getcurrent() |
| 382 |
self.assertRaises(KeyboardInterrupt, hub.greenlet.throw, |
| 383 |
KeyboardInterrupt()) |
| 384 |
|
| 385 |
assert not g.dead # check dummyproc hasn't completed |
| 386 |
with eventlet.Timeout(0.5, self.CustomException()): |
| 387 |
# we now switch to the hub which will allow |
| 388 |
# completion of dummyproc. |
| 389 |
# this should return execution back to the runloop and not |
| 390 |
# this greenlet so that hub.switch() would block indefinitely. |
| 391 |
self.assertRaises(self.CustomException, hub.switch) |
| 392 |
assert g.dead # sanity check that dummyproc has completed |
| 393 |
|
| 394 |
|
| 395 |
def test_use_hub_class(): |
| 396 |
tests.run_isolated('hub_use_hub_class.py') |
| 397 |
|
| 398 |
|
| 399 |
def test_kqueue_unsupported(): |
| 400 |
# https://github.com/eventlet/eventlet/issues/38 |
| 401 |
# get_hub on windows broken by kqueue |
| 402 |
tests.run_isolated('hub_kqueue_unsupported.py') |