| 1 | module onecontext |
| 2 | |
| 3 | import context |
| 4 | import time |
| 5 | |
| 6 | fn eventually(ch chan int) bool { |
| 7 | mut background := context.background() |
| 8 | mut timeout, cancel := context.with_timeout(mut background, 30 * time.millisecond) |
| 9 | defer { |
| 10 | cancel() |
| 11 | } |
| 12 | |
| 13 | tdone := timeout.done() |
| 14 | select { |
| 15 | _ := <-ch { |
| 16 | return true |
| 17 | } |
| 18 | _ := <-tdone { |
| 19 | return false |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | return false |
| 24 | } |
| 25 | |
| 26 | struct Value { |
| 27 | val string |
| 28 | } |
| 29 | |
| 30 | fn test_merge_nominal() { |
| 31 | mut background := context.background() |
| 32 | foo := 'foo' |
| 33 | mut value_ctx1 := context.with_value(background, 'foo', foo) |
| 34 | mut ctx1, cancel := context.with_cancel(mut value_ctx1) |
| 35 | defer { |
| 36 | cancel() |
| 37 | } |
| 38 | |
| 39 | bar := 'bar' |
| 40 | mut value_ctx2 := context.with_value(background, 'bar', bar) |
| 41 | mut ctx2, _ := context.with_cancel(mut value_ctx2) |
| 42 | |
| 43 | mut ctx, cancel2 := merge(ctx1, ctx2) |
| 44 | |
| 45 | if deadline := ctx.deadline() { |
| 46 | assert false |
| 47 | } |
| 48 | |
| 49 | val1 := ctx.value('foo') or { |
| 50 | assert false |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | match val1 { |
| 55 | string { |
| 56 | assert foo == val1 |
| 57 | } |
| 58 | else { |
| 59 | assert false |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | val2 := ctx.value('bar') or { |
| 64 | assert false |
| 65 | return |
| 66 | } |
| 67 | match val2 { |
| 68 | string { |
| 69 | assert bar == val2 |
| 70 | } |
| 71 | else { |
| 72 | assert false |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if _ := ctx.value('baz') { |
| 77 | assert false |
| 78 | } |
| 79 | |
| 80 | assert !eventually(ctx.done()) |
| 81 | assert ctx.err() is none |
| 82 | |
| 83 | cancel2() |
| 84 | assert eventually(ctx.done()) |
| 85 | assert ctx.err().str() == 'canceled context' |
| 86 | } |
| 87 | |
| 88 | fn test_merge_deadline_context_1() { |
| 89 | mut background := context.background() |
| 90 | mut ctx1, cancel := context.with_timeout(mut background, time.second) |
| 91 | defer { |
| 92 | cancel() |
| 93 | } |
| 94 | ctx2 := context.background() |
| 95 | mut ctx, _ := merge(ctx1, ctx2) |
| 96 | |
| 97 | if deadline := ctx.deadline() { |
| 98 | assert deadline.unix() != 0 |
| 99 | } else { |
| 100 | assert false |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | fn test_merge_deadline_context_2() { |
| 105 | mut background := context.background() |
| 106 | ctx1 := context.background() |
| 107 | mut ctx2, cancel := context.with_timeout(mut background, time.second) |
| 108 | defer { |
| 109 | cancel() |
| 110 | } |
| 111 | mut ctx, _ := merge(ctx1, ctx2) |
| 112 | |
| 113 | if deadline := ctx.deadline() { |
| 114 | assert deadline.unix() != 0 |
| 115 | } else { |
| 116 | assert false |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | fn test_merge_deadline_context_n() { |
| 121 | mut background := context.background() |
| 122 | ctx1 := context.background() |
| 123 | |
| 124 | mut ctxs := []context.Context{cap: 21} |
| 125 | for i in 0 .. 10 { |
| 126 | ctxs << context.background() |
| 127 | } |
| 128 | mut ctx_n, _ := context.with_timeout(mut background, time.second) |
| 129 | ctxs << ctx_n |
| 130 | |
| 131 | for i in 0 .. 10 { |
| 132 | ctxs << context.background() |
| 133 | } |
| 134 | |
| 135 | mut ctx, cancel := merge(ctx1, ...ctxs) |
| 136 | |
| 137 | assert !eventually(ctx.done()) |
| 138 | assert ctx.err() is none |
| 139 | cancel() |
| 140 | assert eventually(ctx.done()) |
| 141 | assert ctx.err().str() == 'canceled context' |
| 142 | } |
| 143 | |
| 144 | fn test_merge_deadline_none() { |
| 145 | ctx1 := context.background() |
| 146 | ctx2 := context.background() |
| 147 | |
| 148 | mut ctx, _ := merge(ctx1, ctx2) |
| 149 | |
| 150 | if _ := ctx.deadline() { |
| 151 | assert false |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | fn test_merge_cancel_two() { |
| 156 | ctx1 := context.background() |
| 157 | ctx2 := context.background() |
| 158 | |
| 159 | mut ctx, cancel := merge(ctx1, ctx2) |
| 160 | cancel() |
| 161 | |
| 162 | assert eventually(ctx.done()) |
| 163 | assert ctx.err().str() == 'canceled context' |
| 164 | assert ctx.err().str() == 'canceled context' |
| 165 | } |
| 166 | |
| 167 | fn test_merge_cancel_multiple() { |
| 168 | ctx1 := context.background() |
| 169 | ctx2 := context.background() |
| 170 | ctx3 := context.background() |
| 171 | |
| 172 | mut ctx, cancel := merge(ctx1, ctx2, ctx3) |
| 173 | cancel() |
| 174 | |
| 175 | assert eventually(ctx.done()) |
| 176 | assert ctx.err().str() == 'canceled context' |
| 177 | assert ctx.err().str() == 'canceled context' |
| 178 | } |
| 179 | |