-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_10_context_get_and_set.py
More file actions
53 lines (38 loc) · 1.3 KB
/
test_10_context_get_and_set.py
File metadata and controls
53 lines (38 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from ghoshell_container import get_container, set_container, Container
from contextvars import copy_context
def test_container_is_the_same():
container = get_container()
def foo():
assert container is get_container()
def bar():
foo()
assert container is get_container()
# the container is always the same cause context
bar()
assert container is get_container()
def test_set_container_in_same_ctx():
container = get_container()
def foo():
set_container(Container(name="foo"))
# container has been changed by foo
foo()
assert container is not get_container()
def test_set_container_in_defer_ctx():
set_container(Container(name="root"))
container = get_container()
assert container.name != "foo"
def foo():
assert get_container().name == "foo"
def bar():
assert container.name != "foo"
# assert get_container().name != "foo"
set_container(Container(container, name="foo"))
assert get_container().name == "foo"
foo()
# bar change the container in copied context
ctx = copy_context()
ctx.run(bar)
# but outside bar, the container name is the same
assert get_container().name != "foo"
# container has not been changed
assert get_container() is container