-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
198 lines (175 loc) · 9.14 KB
/
Copy pathtests.py
File metadata and controls
198 lines (175 loc) · 9.14 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import unittest
from threaded_context import (get_current_context, reset_context,
update_current_context,
ThreadedContext, WeakThreadedContext,
BrutalThreadedContext)
class ThreadedContextTestCase(unittest.TestCase):
def assert_context_equals(self, context):
self.assertEqual(context, get_current_context())
def test_base(self):
self.assert_context_equals({})
with ThreadedContext(knights='ni', eki='patang'):
self.assert_context_equals({'eki': 'patang', 'knights': 'ni'})
with ThreadedContext(knights='round table', color='red'):
self.assert_context_equals({'eki': 'patang', 'color': 'red',
'knights': 'ni'})
self.assert_context_equals({'eki': 'patang', 'knights': 'ni'})
self.assert_context_equals({})
def test_base_weak(self):
self.assert_context_equals({})
with WeakThreadedContext(knights='ni', eki='patang'):
self.assert_context_equals({'eki': 'patang', 'knights': 'ni'})
with ThreadedContext(knights='round table', color='red'):
self.assert_context_equals({'eki': 'patang', 'color': 'red',
'knights': 'round table'})
self.assert_context_equals({'eki': 'patang', 'knights': 'ni'})
self.assert_context_equals({})
def test_double_weak(self):
self.assert_context_equals({})
with WeakThreadedContext(knights='ni', eki='patang'):
self.assert_context_equals({'eki': 'patang', 'knights': 'ni'})
with WeakThreadedContext(knights='round table', color='red'):
self.assert_context_equals({'eki': 'patang', 'color': 'red',
'knights': 'round table'})
self.assert_context_equals({'eki': 'patang', 'knights': 'ni'})
self.assert_context_equals({})
def test_as_decorator(self):
self.assert_context_equals({})
@ThreadedContext(knights='round table', color='red')
def wrapped():
self.assert_context_equals({'eki': 'patang', 'color': 'red',
'knights': 'ni'})
@ThreadedContext(knights='ni', eki='patang')
def wrapping():
self.assert_context_equals({'eki': 'patang', 'knights': 'ni'})
wrapped()
self.assert_context_equals({'eki': 'patang', 'knights': 'ni'})
wrapping()
self.assert_context_equals({})
def test_cant_modify_current_context(self):
with ThreadedContext(knights='ni', eki={'eki': 'patang'}):
ctx = get_current_context()
ctx['knights'] = 'round table'
self.assert_context_equals({'knights': 'ni',
'eki': {'eki': 'patang'}})
ctx = get_current_context()
ctx['eki']['eki'] = 'patong'
ctx['eki']['eko'] = 'shrubbery'
self.assert_context_equals({'knights': 'ni',
'eki': {'eki': 'patang'}})
def test_reseting_context(self):
self.assert_context_equals({})
with ThreadedContext(knights='ni', eki='patang'):
self.assert_context_equals({'eki': 'patang', 'knights': 'ni'})
with ThreadedContext(knights='round table', color='red'):
self.assert_context_equals({'eki': 'patang', 'color': 'red',
'knights': 'ni'})
reset_context()
self.assert_context_equals({})
self.assert_context_equals({})
self.assert_context_equals({})
def test_updating_current_context(self):
self.assert_context_equals({})
with ThreadedContext(knights='ni'):
self.assert_context_equals({'knights': 'ni'})
update_current_context(knights='round table', color='red')
self.assert_context_equals({'knights': 'round table',
'color': 'red'})
with WeakThreadedContext(knights='ni'):
self.assert_context_equals({'knights': 'ni'})
update_current_context(knights='round table', color='red')
self.assert_context_equals({'knights': 'round table',
'color': 'red'})
self.assert_context_equals({})
update_current_context(knights='round table', color='red')
self.assert_context_equals({'knights': 'round table', 'color': 'red'})
reset_context()
self.assert_context_equals({})
def test_recursive_context(self):
my_ctx = ThreadedContext(knights='ni')
with my_ctx:
self.assert_context_equals({'knights': 'ni'})
with my_ctx:
self.assert_context_equals({'knights': 'ni'})
with my_ctx:
self.assert_context_equals({'knights': 'ni'})
def test_post_mortem_context_on_exception(self):
# Post-mortem should hold the innermost resolved context after exception
with self.assertRaises(ValueError):
with WeakThreadedContext(weak='weak', override='no'):
with ThreadedContext(normal='normal', override='yes'):
with BrutalThreadedContext(brutal='brutal'):
raise ValueError('pipo')
self.assert_context_equals({
'weak': 'weak', 'override': 'yes',
'normal': 'normal', 'brutal': 'brutal',
})
def test_post_mortem_cleared_on_reenter(self):
# After an exception, entering a new context clears the post-mortem
with self.assertRaises(ValueError):
with ThreadedContext(key='value'):
raise ValueError('pipo')
self.assert_context_equals({'key': 'value'})
with ThreadedContext(key='new'):
self.assert_context_equals({'key': 'new'})
self.assert_context_equals({})
def test_post_mortem_cleared_by_reset(self):
with self.assertRaises(ValueError):
with ThreadedContext(key='value'):
raise ValueError('pipo')
self.assert_context_equals({'key': 'value'})
reset_context()
self.assert_context_equals({})
def test_post_mortem_not_returned_inside_live_context(self):
# While inside a live context, post-mortem must not bleed in
with self.assertRaises(ValueError):
with ThreadedContext(key='post'):
raise ValueError('pipo')
with ThreadedContext(key='live'):
self.assert_context_equals({'key': 'live'})
self.assert_context_equals({})
def test_context_reusable_after_exception(self):
# The exact scenario from the bug report: same context managers
# must produce identical results whether or not a prior exception occurred
def run():
result = {}
with WeakThreadedContext(weak='weak', weak_should_be_overrided='no'):
with ThreadedContext(normal='normal',
weak_should_be_overrided='yes',
normal_should_be_overrided='no'):
with BrutalThreadedContext(brutal='brutal',
normal_should_be_overrided='yes'):
result['brutal'] = get_current_context()
result['normal'] = get_current_context()
result['weak'] = get_current_context()
result['out'] = get_current_context()
return result
expected = {
'brutal': {'weak': 'weak', 'weak_should_be_overrided': 'yes',
'normal': 'normal', 'normal_should_be_overrided': 'yes',
'brutal': 'brutal'},
'normal': {'weak': 'weak', 'weak_should_be_overrided': 'yes',
'normal': 'normal', 'normal_should_be_overrided': 'no'},
'weak': {'weak': 'weak', 'weak_should_be_overrided': 'no'},
'out': {},
}
self.assertEqual(run(), expected)
# Trigger an exception inside the same structure, then re-run
with self.assertRaises(ValueError):
with WeakThreadedContext(weak='weak', weak_should_be_overrided='no'):
with ThreadedContext(normal='normal',
weak_should_be_overrided='yes',
normal_should_be_overrided='no'):
with BrutalThreadedContext(brutal='brutal',
normal_should_be_overrided='yes'):
raise ValueError('pipo')
self.assertEqual(run(), expected)
def test_resilient_thread_type(self):
self.assert_context_equals({})
with WeakThreadedContext(knights='ni'):
self.assert_context_equals({'knights': 'ni'})
with ThreadedContext(color='red'):
self.assert_context_equals({'knights': 'ni', 'color': 'red'})
with WeakThreadedContext(knights='eki'):
self.assert_context_equals({'knights': 'eki',
'color': 'red'})