| 1 |
import pytest, sys |
| 2 |
|
| 3 |
from functools import partial |
| 4 |
|
| 5 |
from greenlet import greenlet |
| 6 |
from greenlet import getcurrent |
| 7 |
from greenlet import GREENLET_USE_CONTEXT_VARS |
| 8 |
|
| 9 |
from contextvars import Context |
| 10 |
from contextvars import ContextVar |
| 11 |
from contextvars import copy_context |
| 12 |
|
| 13 |
def test_context_vars_enabled_on_py37(): |
| 14 |
assert GREENLET_USE_CONTEXT_VARS is True |
| 15 |
|
| 16 |
def test_minimal_set(): |
| 17 |
def _increment(greenlet_id, ctx_var, callback): |
| 18 |
assert ctx_var.get() == "not started" |
| 19 |
ctx_var.set(greenlet_id) |
| 20 |
for _ in range(2): |
| 21 |
callback() |
| 22 |
assert id_var.get() == greenlet_id |
| 23 |
assert getcurrent().gr_context[id_var] == greenlet_id |
| 24 |
|
| 25 |
id_var = ContextVar("id", default=None) |
| 26 |
id_var.set("not started") |
| 27 |
|
| 28 |
callback = getcurrent().switch |
| 29 |
|
| 30 |
lets = [ |
| 31 |
greenlet(partial( |
| 32 |
_increment, |
| 33 |
greenlet_id=i, |
| 34 |
ctx_var=id_var, |
| 35 |
callback=callback, |
| 36 |
)) |
| 37 |
for i in range(1, 5) |
| 38 |
] |
| 39 |
|
| 40 |
for let in lets: |
| 41 |
let.gr_context = copy_context() |
| 42 |
assert let.gr_context[id_var] == "not started" |
| 43 |
|
| 44 |
id_var.set("in main") |
| 45 |
for let in lets: |
| 46 |
assert let.gr_context[id_var] == "not started" |
| 47 |
|
| 48 |
for i in range(3): |
| 49 |
assert id_var.get() == "in main" |
| 50 |
for let in lets: |
| 51 |
let.switch() |
| 52 |
|
| 53 |
assert id_var.get() == "in main" |
| 54 |
for (i, let) in zip(range(1, 5), lets): |
| 55 |
assert let.dead |
| 56 |
assert let.gr_context[id_var] == i |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
class TestContextVars: |
| 61 |
def assertEqual(self, x, y): assert x == y |
| 62 |
def assertIs(self, x, y): assert x is y |
| 63 |
def assertIsNone(self, x): assert x is None |
| 64 |
def assertTrue(self, x): assert x |
| 65 |
def assertIsInstance(self, x, y): assert isinstance(x, y) |
| 66 |
|
| 67 |
def _new_ctx_run(self, *args, **kwargs): |
| 68 |
return copy_context().run(*args, **kwargs) |
| 69 |
|
| 70 |
def _increment(self, greenlet_id, ctx_var, callback, counts, expect): |
| 71 |
if expect is None: |
| 72 |
self.assertIsNone(ctx_var.get()) |
| 73 |
else: |
| 74 |
self.assertEqual(ctx_var.get(), expect) |
| 75 |
ctx_var.set(greenlet_id) |
| 76 |
for _ in range(2): |
| 77 |
counts[ctx_var.get()] += 1 |
| 78 |
callback() |
| 79 |
|
| 80 |
def _test_context(self, propagate_by): |
| 81 |
id_var = ContextVar("id", default=None) |
| 82 |
id_var.set(0) |
| 83 |
|
| 84 |
callback = getcurrent().switch |
| 85 |
counts = dict((i, 0) for i in range(5)) |
| 86 |
|
| 87 |
lets = [ |
| 88 |
greenlet(partial( |
| 89 |
partial( |
| 90 |
copy_context().run, |
| 91 |
self._increment |
| 92 |
) if propagate_by == "run" else self._increment, |
| 93 |
greenlet_id=i, |
| 94 |
ctx_var=id_var, |
| 95 |
callback=callback, |
| 96 |
counts=counts, |
| 97 |
expect=( |
| 98 |
i - 1 if propagate_by == "share" else |
| 99 |
0 if propagate_by in ("set", "run") else None |
| 100 |
) |
| 101 |
)) |
| 102 |
for i in range(1, 5) |
| 103 |
] |
| 104 |
|
| 105 |
for let in lets: |
| 106 |
if propagate_by == "set": |
| 107 |
let.gr_context = copy_context() |
| 108 |
elif propagate_by == "share": |
| 109 |
let.gr_context = getcurrent().gr_context |
| 110 |
else: |
| 111 |
self.assertIsNone(let.gr_context) |
| 112 |
|
| 113 |
for i in range(2): |
| 114 |
counts[id_var.get()] += 1 |
| 115 |
for let in lets: |
| 116 |
let.switch() |
| 117 |
|
| 118 |
if propagate_by == "run": |
| 119 |
|
| 120 |
for let in reversed(lets): |
| 121 |
let.switch() |
| 122 |
else: |
| 123 |
|
| 124 |
for let in lets: |
| 125 |
let.switch() |
| 126 |
|
| 127 |
for let in lets: |
| 128 |
self.assertTrue(let.dead) |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
self.assertEqual(let.gr_context is None, propagate_by == "run") |
| 134 |
|
| 135 |
if propagate_by == "share": |
| 136 |
self.assertEqual(counts, {0: 1, 1: 1, 2: 1, 3: 1, 4: 6}) |
| 137 |
else: |
| 138 |
self.assertEqual(set(counts.values()), set([2])) |
| 139 |
|
| 140 |
def test_context_propagated_by_context_run(self): |
| 141 |
self._new_ctx_run(self._test_context, "run") |
| 142 |
|
| 143 |
def test_context_propagated_by_setting_attribute(self): |
| 144 |
self._new_ctx_run(self._test_context, "set") |
| 145 |
|
| 146 |
def test_context_not_propagated(self): |
| 147 |
self._new_ctx_run(self._test_context, None) |
| 148 |
|
| 149 |
def test_context_shared(self): |
| 150 |
self._new_ctx_run(self._test_context, "share") |
| 151 |
|
| 152 |
def test_break_ctxvars(self): |
| 153 |
let1 = greenlet(copy_context().run) |
| 154 |
let2 = greenlet(copy_context().run) |
| 155 |
let1.switch(getcurrent().switch) |
| 156 |
let2.switch(getcurrent().switch) |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
let1.switch() |
| 161 |
|
| 162 |
def test_not_broken_if_using_attribute_instead_of_context_run(self): |
| 163 |
let1 = greenlet(getcurrent().switch) |
| 164 |
let2 = greenlet(getcurrent().switch) |
| 165 |
let1.gr_context = copy_context() |
| 166 |
let2.gr_context = copy_context() |
| 167 |
let1.switch() |
| 168 |
let2.switch() |
| 169 |
let1.switch() |
| 170 |
let2.switch() |
| 171 |
|
| 172 |
def test_context_assignment_while_running(self): |
| 173 |
id_var = ContextVar("id", default=None) |
| 174 |
|
| 175 |
def target(): |
| 176 |
self.assertIsNone(id_var.get()) |
| 177 |
self.assertIsNone(gr.gr_context) |
| 178 |
|
| 179 |
|
| 180 |
id_var.set(1) |
| 181 |
self.assertIsInstance(gr.gr_context, Context) |
| 182 |
self.assertEqual(id_var.get(), 1) |
| 183 |
self.assertEqual(gr.gr_context[id_var], 1) |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
old_context = gr.gr_context |
| 188 |
gr.gr_context = None |
| 189 |
self.assertIsNone(id_var.get()) |
| 190 |
self.assertIsNone(gr.gr_context) |
| 191 |
id_var.set(2) |
| 192 |
self.assertIsInstance(gr.gr_context, Context) |
| 193 |
self.assertEqual(id_var.get(), 2) |
| 194 |
self.assertEqual(gr.gr_context[id_var], 2) |
| 195 |
|
| 196 |
new_context = gr.gr_context |
| 197 |
getcurrent().parent.switch((old_context, new_context)) |
| 198 |
|
| 199 |
|
| 200 |
self.assertEqual(id_var.get(), 1) |
| 201 |
gr.gr_context = new_context |
| 202 |
self.assertEqual(id_var.get(), 2) |
| 203 |
|
| 204 |
getcurrent().parent.switch() |
| 205 |
|
| 206 |
self.assertIsNone(id_var.get()) |
| 207 |
self.assertIsNone(gr.gr_context) |
| 208 |
gr.gr_context = old_context |
| 209 |
self.assertEqual(id_var.get(), 1) |
| 210 |
|
| 211 |
getcurrent().parent.switch() |
| 212 |
|
| 213 |
self.assertIsNone(id_var.get()) |
| 214 |
self.assertIsNone(gr.gr_context) |
| 215 |
|
| 216 |
gr = greenlet(target) |
| 217 |
|
| 218 |
with pytest.raises(AttributeError) as e: |
| 219 |
del gr.gr_context |
| 220 |
assert "object has no deleter" in str(e.value) |
| 221 |
|
| 222 |
self.assertIsNone(gr.gr_context) |
| 223 |
old_context, new_context = gr.switch() |
| 224 |
self.assertIs(new_context, gr.gr_context) |
| 225 |
self.assertEqual(old_context[id_var], 1) |
| 226 |
self.assertEqual(new_context[id_var], 2) |
| 227 |
self.assertEqual(new_context.run(id_var.get), 2) |
| 228 |
gr.gr_context = old_context |
| 229 |
gr.switch() |
| 230 |
self.assertIs(gr.gr_context, new_context) |
| 231 |
gr.gr_context = None |
| 232 |
gr.switch() |
| 233 |
self.assertIs(gr.gr_context, old_context) |
| 234 |
gr.gr_context = None |
| 235 |
gr.switch() |
| 236 |
self.assertIsNone(gr.gr_context) |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
def test_context_assignment_different_thread(self): |
| 245 |
import threading |
| 246 |
|
| 247 |
ctx = Context() |
| 248 |
var = ContextVar("var", default=None) |
| 249 |
is_running = threading.Event() |
| 250 |
should_suspend = threading.Event() |
| 251 |
did_suspend = threading.Event() |
| 252 |
should_exit = threading.Event() |
| 253 |
holder = [] |
| 254 |
|
| 255 |
def greenlet_in_thread_fn(): |
| 256 |
var.set(1) |
| 257 |
is_running.set() |
| 258 |
should_suspend.wait() |
| 259 |
var.set(2) |
| 260 |
getcurrent().parent.switch() |
| 261 |
holder.append(var.get()) |
| 262 |
|
| 263 |
def thread_fn(): |
| 264 |
gr = greenlet(greenlet_in_thread_fn) |
| 265 |
gr.gr_context = ctx |
| 266 |
holder.append(gr) |
| 267 |
gr.switch() |
| 268 |
did_suspend.set() |
| 269 |
should_exit.wait() |
| 270 |
gr.switch() |
| 271 |
|
| 272 |
thread = threading.Thread(target=thread_fn, daemon=True) |
| 273 |
thread.start() |
| 274 |
is_running.wait() |
| 275 |
gr = holder[0] |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
if sys.implementation.name == 'cpython': |
| 283 |
with pytest.raises(ValueError) as e: |
| 284 |
getattr(gr, 'gr_context') |
| 285 |
assert "running in a different" in str(e.value) |
| 286 |
with pytest.raises(ValueError) as e: |
| 287 |
gr.gr_context = None |
| 288 |
assert "running in a different" in str(e.value) |
| 289 |
|
| 290 |
should_suspend.set() |
| 291 |
did_suspend.wait() |
| 292 |
|
| 293 |
|
| 294 |
self.assertIs(gr.gr_context, ctx) |
| 295 |
self.assertEqual(gr.gr_context[var], 2) |
| 296 |
gr.gr_context = None |
| 297 |
|
| 298 |
should_exit.set() |
| 299 |
thread.join() |
| 300 |
|
| 301 |
self.assertEqual(holder, [gr, None]) |
| 302 |
|
| 303 |
|
| 304 |
self.assertIsNone(gr.gr_context) |
| 305 |
gr.gr_context = ctx |
| 306 |
self.assertIs(gr.gr_context, ctx) |