v2 / vlib / x / async / context_internal_test.v
54 lines · 50 sloc · 1.2 KB · 15fb60b77ea6073658aa8355b247f2e1ae03b714
Raw
1module async
2
3import context
4import time
5
6fn test_async_context_cancel_closes_done_and_sets_error() {
7 mut ctx, cancel := new_cancel_context(context.background())
8 cancel()
9 done := ctx.done()
10 select {
11 _ := <-done {
12 assert ctx.err().msg() == context_canceled
13 }
14 1 * time.second {
15 assert false, 'cancel did not close AsyncContext.done()'
16 }
17 }
18 cancel()
19 assert ctx.err().msg() == context_canceled
20}
21
22fn test_async_context_timeout_closes_done_and_sets_deadline_error() {
23 mut ctx, cancel := new_timeout_context(context.background(), 20 * time.millisecond)
24 defer {
25 cancel()
26 }
27 done := ctx.done()
28 select {
29 _ := <-done {
30 assert ctx.err().msg() == context_deadline_exceeded
31 }
32 1 * time.second {
33 assert false, 'timeout did not close AsyncContext.done()'
34 }
35 }
36}
37
38fn test_async_context_parent_cancel_propagates_to_child() {
39 parent, parent_cancel := new_cancel_context(context.background())
40 mut child, child_cancel := new_cancel_context(context.Context(parent))
41 defer {
42 child_cancel()
43 }
44 parent_cancel()
45 done := child.done()
46 select {
47 _ := <-done {
48 assert child.err().msg() == context_canceled
49 }
50 1 * time.second {
51 assert false, 'parent cancellation did not reach child AsyncContext'
52 }
53 }
54}
55