vxx2 / vlib / x / executor / pump_test.v
88 lines · 74 sloc · 1.8 KB · e80386b6af8a071a2e13fd654ebcdb2105fb9202
Raw
1module executor
2
3import time
4
5fn test_run_one_executes_one_job_and_reports_true() {
6 mut ex := new(queue_size: 4)!
7 values := chan int{cap: 2}
8
9 ex.try_post(fn [values] () ! {
10 values <- 1
11 })!
12 ex.try_post(fn [values] () ! {
13 values <- 2
14 })!
15
16 assert ex.run_one()!
17 assert wait_for_int(values, 'first job did not run') == 1
18 assert ex.run_one()!
19 assert wait_for_int(values, 'second job did not run') == 2
20 assert !ex.run_one()!
21
22 ex.stop()
23 assert !ex.run_one()!
24 ex.wait()!
25}
26
27fn test_drain_pending_runs_at_most_limit() {
28 mut ex := new(queue_size: 4)!
29 values := chan int{cap: 4}
30 for i in 0 .. 4 {
31 ex.try_post(fn [values, i] () ! {
32 values <- i
33 })!
34 }
35
36 assert ex.drain_pending(2)! == 2
37 assert wait_for_int(values, 'first drained job missing') == 0
38 assert wait_for_int(values, 'second drained job missing') == 1
39
40 assert ex.drain_pending(8)! == 2
41 assert wait_for_int(values, 'third drained job missing') == 2
42 assert wait_for_int(values, 'fourth drained job missing') == 3
43
44 ex.stop()
45 assert ex.drain_pending(1)! == 0
46 ex.wait()!
47}
48
49fn test_drain_pending_rejects_non_positive_limit() {
50 mut ex := new(queue_size: 1)!
51 ex.drain_pending(0) or {
52 assert err.msg() == 'executor: drain limit must be positive'
53 ex.stop()
54 return
55 }
56 assert false, 'drain_pending accepted a zero limit'
57}
58
59fn test_single_producer_fifo_order_is_preserved() {
60 mut ex := new(queue_size: 8)!
61 values := chan int{cap: 5}
62 for i in 0 .. 5 {
63 ex.try_post(fn [values, i] () ! {
64 values <- i
65 })!
66 }
67
68 assert ex.drain_pending(5)! == 5
69 for expected in 0 .. 5 {
70 assert wait_for_int(values, 'FIFO job missing') == expected
71 }
72
73 ex.stop()
74 assert !ex.run_one()!
75 ex.wait()!
76}
77
78fn wait_for_int(signal chan int, message string) int {
79 select {
80 value := <-signal {
81 return value
82 }
83 1 * time.second {
84 assert false, message
85 }
86 }
87 return 0
88}
89