Skip to content

Commit 094a0dd

Browse files
committed
tests/thread: Add tests that mutate shared objects.
Tests concurrent mutating access to: list, dict, set, bytearray.
1 parent c73cf9d commit 094a0dd

4 files changed

Lines changed: 167 additions & 0 deletions

File tree

tests/thread/mutate_bytearray.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# test concurrent mutating access to a shared bytearray object
2+
#
3+
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
4+
5+
import _thread
6+
7+
# the shared bytearray
8+
ba = bytearray()
9+
10+
# main thread function
11+
def th(n, lo, hi):
12+
for repeat in range(n):
13+
for i in range(lo, hi):
14+
l = len(ba)
15+
ba.append(i)
16+
assert len(ba) >= l + 1
17+
18+
l = len(ba)
19+
ba.extend(bytearray([i]))
20+
assert len(ba) >= l + 1
21+
22+
with lock:
23+
global n_finished
24+
n_finished += 1
25+
26+
lock = _thread.allocate_lock()
27+
n_thread = 4
28+
n_finished = 0
29+
30+
# spawn threads
31+
for i in range(n_thread):
32+
_thread.start_new_thread(th, (40, i * 256 // n_thread, (i + 1) * 256 // n_thread))
33+
34+
# busy wait for threads to finish
35+
while n_finished < n_thread:
36+
pass
37+
38+
# check bytearray has correct contents
39+
print(len(ba))
40+
count = [0 for _ in range(256)]
41+
for b in ba:
42+
count[b] += 1
43+
print(count)
44+

tests/thread/mutate_dict.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# test concurrent mutating access to a shared dict object
2+
#
3+
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
4+
5+
import _thread
6+
7+
# the shared dict
8+
di = {'a':'A', 'b':'B', 'c':'C', 'd':'D'}
9+
10+
# main thread function
11+
def th(n, lo, hi):
12+
for repeat in range(n):
13+
for i in range(lo, hi):
14+
di[i] = repeat + i
15+
assert di[i] == repeat + i
16+
17+
del di[i]
18+
assert i not in di
19+
20+
di[i] = repeat + i
21+
assert di[i] == repeat + i
22+
23+
assert di.pop(i) == repeat + i
24+
25+
with lock:
26+
global n_finished
27+
n_finished += 1
28+
29+
lock = _thread.allocate_lock()
30+
n_thread = 4
31+
n_finished = 0
32+
33+
# spawn threads
34+
for i in range(n_thread):
35+
_thread.start_new_thread(th, (30, i * 300, (i + 1) * 300))
36+
37+
# busy wait for threads to finish
38+
while n_finished < n_thread:
39+
pass
40+
41+
# check dict has correct contents
42+
print(sorted(di.items()))

tests/thread/mutate_list.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# test concurrent mutating access to a shared list object
2+
#
3+
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
4+
5+
import _thread
6+
7+
# the shared list
8+
li = list()
9+
10+
# main thread function
11+
def th(n, lo, hi):
12+
for repeat in range(n):
13+
for i in range(lo, hi):
14+
li.append(i)
15+
assert li.count(i) == repeat + 1
16+
17+
li.extend([i, i])
18+
assert li.count(i) == repeat + 3
19+
20+
li.remove(i)
21+
assert li.count(i) == repeat + 2
22+
23+
li.remove(i)
24+
assert li.count(i) == repeat + 1
25+
26+
with lock:
27+
global n_finished
28+
n_finished += 1
29+
30+
lock = _thread.allocate_lock()
31+
n_thread = 4
32+
n_finished = 0
33+
34+
# spawn threads
35+
for i in range(n_thread):
36+
_thread.start_new_thread(th, (4, i * 60, (i + 1) * 60))
37+
38+
# busy wait for threads to finish
39+
while n_finished < n_thread:
40+
pass
41+
42+
# check list has correct contents
43+
li.sort()
44+
print(li)

tests/thread/mutate_set.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# test concurrent mutating access to a shared set object
2+
#
3+
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
4+
5+
import _thread
6+
7+
# the shared set
8+
se = set([-1, -2, -3, -4])
9+
10+
# main thread function
11+
def th(n, lo, hi):
12+
for repeat in range(n):
13+
for i in range(lo, hi):
14+
se.add(i)
15+
assert i in se
16+
17+
se.remove(i)
18+
assert i not in se
19+
20+
with lock:
21+
global n_finished
22+
n_finished += 1
23+
24+
lock = _thread.allocate_lock()
25+
n_thread = 4
26+
n_finished = 0
27+
28+
# spawn threads
29+
for i in range(n_thread):
30+
_thread.start_new_thread(th, (50, i * 500, (i + 1) * 500))
31+
32+
# busy wait for threads to finish
33+
while n_finished < n_thread:
34+
pass
35+
36+
# check set has correct contents
37+
print(sorted(se))

0 commit comments

Comments
 (0)