v2 / vlib / v / tests / concurrency / shared_autolock_test.v
42 lines · 38 sloc · 644 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1const iterations = 100000
2
3fn inc_array_elem(shared b []int, i int) {
4 for _ in 0 .. iterations {
5 b[i]++
6 }
7}
8
9fn test_autolock_array() {
10 shared a := [1, 2, 7, 5]
11 t := spawn inc_array_elem(shared a, 2)
12 for _ in 0 .. iterations {
13 a[2]++
14 }
15 t.wait()
16 rlock a {
17 assert a[2] == 2 * iterations + 7
18 }
19}
20
21fn inc_map_elem(shared b map[string]int, k string) {
22 for _ in 0 .. iterations {
23 b[k]++
24 }
25}
26
27fn test_autolock_map() {
28 shared m := {
29 'xy': 1
30 'qwe': 2
31 'asd': 7
32 'iop': 5
33 }
34 t := spawn inc_map_elem(shared m, 'asd')
35 for _ in 0 .. iterations {
36 m['asd']++
37 }
38 t.wait()
39 rlock m {
40 assert m['asd'] == 2 * iterations + 7
41 }
42}
43