| 1 |
import gc |
| 2 |
import weakref |
| 3 |
|
| 4 |
import eventlet |
| 5 |
from eventlet import corolocal |
| 6 |
from eventlet import event |
| 7 |
from eventlet import greenthread |
| 8 |
from eventlet import patcher |
| 9 |
from eventlet.green import thread |
| 10 |
|
| 11 |
from tests import LimitedTestCase, run_isolated |
| 12 |
|
| 13 |
|
| 14 |
class Locals(LimitedTestCase): |
| 15 |
def passthru(self, *args, **kw): |
| 16 |
self.results.append((args, kw)) |
| 17 |
return args, kw |
| 18 |
|
| 19 |
def setUp(self): |
| 20 |
self.results = [] |
| 21 |
super().setUp() |
| 22 |
|
| 23 |
def tearDown(self): |
| 24 |
self.results = [] |
| 25 |
super().tearDown() |
| 26 |
|
| 27 |
def test_assignment(self): |
| 28 |
my_local = corolocal.local() |
| 29 |
my_local.a = 1 |
| 30 |
|
| 31 |
def do_something(): |
| 32 |
my_local.b = 2 |
| 33 |
self.assertEqual(my_local.b, 2) |
| 34 |
try: |
| 35 |
my_local.a |
| 36 |
self.fail() |
| 37 |
except AttributeError: |
| 38 |
pass |
| 39 |
|
| 40 |
eventlet.spawn(do_something).wait() |
| 41 |
self.assertEqual(my_local.a, 1) |
| 42 |
|
| 43 |
def test_calls_init(self): |
| 44 |
init_args = [] |
| 45 |
|
| 46 |
class Init(corolocal.local): |
| 47 |
def __init__(self, *args): |
| 48 |
init_args.append((args, eventlet.getcurrent())) |
| 49 |
|
| 50 |
my_local = Init(1, 2, 3) |
| 51 |
self.assertEqual(init_args[0][0], (1, 2, 3)) |
| 52 |
self.assertEqual(init_args[0][1], eventlet.getcurrent()) |
| 53 |
|
| 54 |
def do_something(): |
| 55 |
my_local.foo = 'bar' |
| 56 |
self.assertEqual(len(init_args), 2, init_args) |
| 57 |
self.assertEqual(init_args[1][0], (1, 2, 3)) |
| 58 |
self.assertEqual(init_args[1][1], eventlet.getcurrent()) |
| 59 |
|
| 60 |
eventlet.spawn(do_something).wait() |
| 61 |
|
| 62 |
def test_calling_methods(self): |
| 63 |
class Caller(corolocal.local): |
| 64 |
def callme(self): |
| 65 |
return self.foo |
| 66 |
|
| 67 |
my_local = Caller() |
| 68 |
my_local.foo = "foo1" |
| 69 |
self.assertEqual("foo1", my_local.callme()) |
| 70 |
|
| 71 |
def do_something(): |
| 72 |
my_local.foo = "foo2" |
| 73 |
self.assertEqual("foo2", my_local.callme()) |
| 74 |
|
| 75 |
eventlet.spawn(do_something).wait() |
| 76 |
|
| 77 |
my_local.foo = "foo3" |
| 78 |
self.assertEqual("foo3", my_local.callme()) |
| 79 |
|
| 80 |
def test_no_leaking(self): |
| 81 |
refs = weakref.WeakKeyDictionary() |
| 82 |
my_local = corolocal.local() |
| 83 |
|
| 84 |
class X: |
| 85 |
pass |
| 86 |
|
| 87 |
def do_something(i): |
| 88 |
o = X() |
| 89 |
refs[o] = True |
| 90 |
my_local.foo = o |
| 91 |
|
| 92 |
p = eventlet.GreenPool() |
| 93 |
for i in range(100): |
| 94 |
p.spawn(do_something, i) |
| 95 |
p.waitall() |
| 96 |
del p |
| 97 |
gc.collect() |
| 98 |
eventlet.sleep(0) |
| 99 |
gc.collect() |
| 100 |
# at this point all our coros have terminated |
| 101 |
self.assertEqual(len(refs), 1) |
| 102 |
|
| 103 |
|
| 104 |
def test_compat_lock_release(): |
| 105 |
# https://github.com/eventlet/eventlet/issues/697 |
| 106 |
for mod in (patcher.original("threading"), thread): |
| 107 |
try: |
| 108 |
mod.Lock().release() |
| 109 |
except RuntimeError as e: |
| 110 |
# python3 |
| 111 |
assert "release unlocked lock" in str(e).lower(), str((mod, e)) |
| 112 |
except thread.error as e: |
| 113 |
# python2.7 |
| 114 |
assert "release unlocked lock" in str(e).lower(), str((mod, e)) |
| 115 |
|
| 116 |
|
| 117 |
def test_reinit(): |
| 118 |
# py39+ expects locks to have a _at_fork_reinit() method |
| 119 |
# https://github.com/eventlet/eventlet/pull/721#pullrequestreview-769377850 |
| 120 |
lk = thread.Lock() |
| 121 |
lk.acquire() |
| 122 |
lk._at_fork_reinit() |
| 123 |
assert lk.acquire(blocking=False) |
| 124 |
assert not lk.acquire(blocking=False) |
| 125 |
|
| 126 |
|
| 127 |
def test_can_use_eventlet_in_os_threads(): |
| 128 |
run_isolated("osthreads.py") |