v / vlib / x / async / context_internal_test.v
80 lines · 74 sloc · 1.96 KB · 4389a3bf8b71672e61650d457e5b58dacf9e0ef7
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_timeout_result_detects_only_owned_deadline_miss() {
39 mut ctx, cancel := new_timeout_context(context.background(), 1 * time.second)
40 defer {
41 cancel()
42 }
43 assert !TimeoutResult{
44 finished_at: ctx.deadline_at.add(-1 * time.nanosecond)
45 }.finished_after_owned_timeout(ctx)
46 assert TimeoutResult{
47 finished_at: ctx.deadline_at.add(1 * time.nanosecond)
48 }.finished_after_owned_timeout(ctx)
49
50 mut background := context.background()
51 parent, parent_cancel := context.with_timeout(mut background, 1 * time.second)
52 defer {
53 parent_cancel()
54 }
55 mut child, child_cancel := new_timeout_context(parent, 2 * time.second)
56 defer {
57 child_cancel()
58 }
59 assert !TimeoutResult{
60 finished_at: child.deadline_at.add(1 * time.nanosecond)
61 }.finished_after_owned_timeout(child)
62}
63
64fn test_async_context_parent_cancel_propagates_to_child() {
65 parent, parent_cancel := new_cancel_context(context.background())
66 mut child, child_cancel := new_cancel_context(context.Context(parent))
67 defer {
68 child_cancel()
69 }
70 parent_cancel()
71 done := child.done()
72 select {
73 _ := <-done {
74 assert child.err().msg() == context_canceled
75 }
76 1 * time.second {
77 assert false, 'parent cancellation did not reach child AsyncContext'
78 }
79 }
80}
81