vxx2 / vlib / x / executor / stress_test.v
112 lines · 99 sloc · 2.14 KB · e80386b6af8a071a2e13fd654ebcdb2105fb9202
Raw
1module executor
2
3import context
4import time
5
6fn test_many_producers_submit_bounded_work() {
7 mut ex := new(queue_size: 16)!
8 accepted := chan bool{cap: 16}
9 ran := chan int{cap: 16}
10 mut producers := []thread{}
11
12 for i in 0 .. 16 {
13 producers << spawn fn [mut ex, accepted, ran, i] () {
14 ex.post_with_context(context.background(), fn [ran, i] () ! {
15 ran <- i
16 }) or {
17 accepted <- false
18 return
19 }
20 accepted <- true
21 }()
22 }
23
24 for _ in 0 .. 16 {
25 assert wait_for_bool(accepted, 'producer did not finish admission')
26 }
27 assert ex.drain_pending(16)! == 16
28
29 mut total := 0
30 for _ in 0 .. 16 {
31 total += wait_for_int(ran, 'producer job did not run')
32 }
33 assert total == 120
34
35 for producer in producers {
36 producer.wait()
37 }
38 ex.stop()
39 assert !ex.run_one()!
40 ex.wait()!
41}
42
43fn test_stop_wakes_waiting_submitter_without_accepting_job() {
44 mut ex := new(queue_size: 1)!
45 attempting := chan bool{cap: 1}
46 result := chan string{cap: 1}
47 ran := chan bool{cap: 1}
48
49 ex.try_post(fn () ! {})!
50 submitter := spawn fn [mut ex, attempting, result, ran] () {
51 attempting <- true
52 ex.post_with_context(context.background(), fn [ran] () ! {
53 ran <- true
54 }) or {
55 result <- err.msg()
56 return
57 }
58 result <- 'accepted'
59 }()
60
61 assert wait_for_bool(attempting, 'waiting submitter did not start')
62 ex.stop()
63 msg := wait_for_string(result, 'waiting submitter was not woken by stop')
64 assert msg == 'executor: executor is closed'
65 assert_no_bool(ran, 'job was accepted after stop')
66 submitter.wait()
67}
68
69fn wait_for_bool(signal chan bool, message string) bool {
70 select {
71 value := <-signal {
72 return value
73 }
74 1 * time.second {
75 assert false, message
76 }
77 }
78 return false
79}
80
81fn wait_for_int(signal chan int, message string) int {
82 select {
83 value := <-signal {
84 return value
85 }
86 1 * time.second {
87 assert false, message
88 }
89 }
90 return 0
91}
92
93fn wait_for_string(signal chan string, message string) string {
94 select {
95 value := <-signal {
96 return value
97 }
98 1 * time.second {
99 assert false, message
100 }
101 }
102 return ''
103}
104
105fn assert_no_bool(signal chan bool, message string) {
106 select {
107 _ := <-signal {
108 assert false, message
109 }
110 50 * time.millisecond {}
111 }
112}
113