v2 / vlib / v / fmt / tests / chan_ops_keep.vv
63 lines · 56 sloc · 1.0 KB · f09826e928f9612bab9299faefff7cf34a503362
Raw
1import sync
2
3const num_iterations = 10000
4
5struct St {
6 a int
7}
8
9fn get_val_from_chan(ch chan i64) ?i64 {
10 r := <-ch?
11 return r
12}
13
14fn get_val_from_chan2(ch chan i64) ?i64 {
15 r := <-ch or {
16 println('error')
17 return err
18 }
19 return r
20}
21
22// this function gets an array of channels for `i64`
23fn do_rec_calc_send(chs []chan i64, sem sync.Semaphore) {
24 mut msg := ''
25 for {
26 mut s := get_val_from_chan(chs[0]) or {
27 msg = err.str()
28 break
29 }
30 s++
31 chs[1] <- s
32 }
33 assert msg == 'channel closed'
34 sem.post()
35}
36
37fn test_channel_array_mut() {
38 mut chs := [chan i64{}, chan i64{cap: 10}]
39 sem := sync.new_semaphore()
40 spawn do_rec_calc_send(chs, sem)
41 mut t := i64(100)
42 for _ in 0 .. num_iterations {
43 chs[0] <- t
44 t = <-chs[1]
45 }
46 (&sync.Channel(chs[0])).close()
47 orr := &sync.Channel(chs[0])
48 chs[1].close()
49 ch := chan int{}
50 ch.close()
51 a := ch.cap
52 b := ch.len
53 c := ch[1].cap
54 d := ch[o].len
55 sem.wait()
56 assert t == 100 + num_iterations
57 ch2 := chan mut St{cap: 10}
58 spawn g(ch2)
59}
60
61fn g(ch chan mut St) {
62 return
63}
64