Skip to content

Commit db8947c

Browse files
committed
add tests
1 parent eebccac commit db8947c

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import unittest
2+
from threading import Thread
3+
from test.test_json import CTest
4+
from test.support import threading_helper
5+
6+
7+
def encode_json_helper(json, worker, data, number_of_threads, number_of_json_encodings=100):
8+
worker_threads = []
9+
for index in range(number_of_threads):
10+
worker_threads.append(Thread(target=worker, args=[data, index]))
11+
for t in worker_threads:
12+
t.start()
13+
for ii in range(number_of_json_encodings):
14+
json.dumps(data)
15+
data.clear()
16+
for t in worker_threads:
17+
t.join()
18+
19+
20+
class MyMapping(dict):
21+
def __init__(self):
22+
self.mapping = []
23+
24+
def items(self):
25+
return self.mapping
26+
27+
28+
@threading_helper.reap_threads
29+
@threading_helper.requires_working_threading()
30+
class TestJsonEncoding(CTest):
31+
# Test encoding json with multiple threads modifying the data cannot
32+
# corrupt the interpreter
33+
34+
def test_json_mutating_list(self):
35+
36+
def worker(data, index):
37+
while data:
38+
for d in data:
39+
if len(d) > 5:
40+
d.clear()
41+
else:
42+
d.append(index)
43+
d.append(index)
44+
d.append(index)
45+
encode_json_helper(self.json, worker, [[], []], number_of_threads=16)
46+
47+
def test_json_mutating_dict(self):
48+
49+
def worker(data, index):
50+
while data:
51+
for d in data:
52+
if len(d) > 5:
53+
try:
54+
d.pop(list(d)[0])
55+
except (KeyError, IndexError):
56+
pass
57+
else:
58+
d[index] = index
59+
encode_json_helper(self.json, worker, [{}, {}], number_of_threads=16)
60+
61+
def test_json_mutating_mapping(self):
62+
63+
def worker(data, index):
64+
while data:
65+
for d in data:
66+
if len(d.mapping) > 3:
67+
d.mapping.clear()
68+
else:
69+
d.mapping.append((index, index))
70+
encode_json_helper(self.json,
71+
worker, [MyMapping(), MyMapping()], number_of_threads=16)
72+
73+
74+
if __name__ == "__main__":
75+
import time
76+
77+
t0 = time.time()
78+
unittest.main()
79+
dt = time.time()-t0
80+
print(f'Done: {dt:.2f}')

0 commit comments

Comments
 (0)