| 1 | import sync |
| 2 | |
| 3 | const num_iterations = 10000 |
| 4 | |
| 5 | fn get_val_from_chan(ch chan i64) ?i64 { |
| 6 | r := <-ch? |
| 7 | return r |
| 8 | } |
| 9 | |
| 10 | // this function gets an array of channels for `i64` |
| 11 | fn do_rec_calc_send(chs []chan i64, mut sem sync.Semaphore) { |
| 12 | mut msg := '' |
| 13 | for { |
| 14 | mut s := get_val_from_chan(chs[0]) or { |
| 15 | msg = err.msg() |
| 16 | break |
| 17 | } |
| 18 | s++ |
| 19 | chs[1] <- s |
| 20 | } |
| 21 | assert msg == 'channel closed' |
| 22 | sem.post() |
| 23 | } |
| 24 | |
| 25 | fn test_channel_array_mut() { |
| 26 | mut chs := [chan i64{}, chan i64{cap: 10}] |
| 27 | mut sem := sync.new_semaphore() |
| 28 | spawn do_rec_calc_send(chs, mut sem) |
| 29 | mut t := i64(100) |
| 30 | for _ in 0 .. num_iterations { |
| 31 | chs[0] <- t |
| 32 | t = <-chs[1] |
| 33 | } |
| 34 | chs[0].close() |
| 35 | sem.wait() |
| 36 | assert t == 100 + num_iterations |
| 37 | } |
| 38 | |
| 39 | fn test_channel_close_with_error_propagates_after_buffer_drain() { |
| 40 | ch := chan i64{cap: 1} |
| 41 | ch <- i64(7) |
| 42 | ch.close(error('async failure')) |
| 43 | assert <-ch == i64(7) |
| 44 | _ := get_val_from_chan(ch) or { |
| 45 | assert err.msg() == 'async failure' |
| 46 | return |
| 47 | } |
| 48 | assert false |
| 49 | } |
| 50 | |