2021-12-02 12:15:07 +03:00
|
|
|
module onecontext
|
|
|
|
|
|
|
|
import context
|
|
|
|
import time
|
|
|
|
|
|
|
|
fn eventually(ch chan int) bool {
|
|
|
|
mut background := context.background()
|
2023-06-04 19:12:52 +03:00
|
|
|
mut timeout, cancel := context.with_timeout(mut background, 30 * time.millisecond)
|
2021-12-02 12:15:07 +03:00
|
|
|
defer {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
tdone := timeout.done()
|
|
|
|
select {
|
|
|
|
_ := <-ch {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
_ := <-tdone {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Value {
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
|
2023-06-04 19:12:52 +03:00
|
|
|
fn test_merge_nominal() {
|
2021-12-02 12:15:07 +03:00
|
|
|
mut background := context.background()
|
2023-06-04 19:12:52 +03:00
|
|
|
foo := 'foo'
|
2021-12-02 12:15:07 +03:00
|
|
|
mut value_ctx1 := context.with_value(background, 'foo', foo)
|
2023-06-04 19:12:52 +03:00
|
|
|
mut ctx1, cancel := context.with_cancel(mut value_ctx1)
|
2021-12-02 12:15:07 +03:00
|
|
|
defer {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
|
2023-06-04 19:12:52 +03:00
|
|
|
bar := 'bar'
|
2021-12-02 12:15:07 +03:00
|
|
|
mut value_ctx2 := context.with_value(background, 'bar', bar)
|
2023-06-04 19:12:52 +03:00
|
|
|
mut ctx2, _ := context.with_cancel(mut value_ctx2)
|
2021-12-02 12:15:07 +03:00
|
|
|
|
|
|
|
mut ctx, cancel2 := merge(ctx1, ctx2)
|
|
|
|
|
|
|
|
if deadline := ctx.deadline() {
|
2023-06-04 19:12:52 +03:00
|
|
|
assert false
|
|
|
|
}
|
|
|
|
|
|
|
|
val1 := ctx.value('foo') or {
|
|
|
|
assert false
|
|
|
|
return
|
2021-12-02 12:15:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
match val1 {
|
2023-06-04 19:12:52 +03:00
|
|
|
string {
|
2021-12-02 12:15:07 +03:00
|
|
|
assert foo == val1
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-04 19:12:52 +03:00
|
|
|
val2 := ctx.value('bar') or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2021-12-02 12:15:07 +03:00
|
|
|
match val2 {
|
2023-06-04 19:12:52 +03:00
|
|
|
string {
|
2021-12-02 12:15:07 +03:00
|
|
|
assert bar == val2
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _ := ctx.value('baz') {
|
2023-06-04 19:12:52 +03:00
|
|
|
assert false
|
2021-12-02 12:15:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
assert !eventually(ctx.done())
|
|
|
|
assert ctx.err() is none
|
|
|
|
|
|
|
|
cancel2()
|
|
|
|
assert eventually(ctx.done())
|
2023-06-04 19:12:52 +03:00
|
|
|
assert ctx.err().str() == 'canceled context'
|
2021-12-02 12:15:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_merge_deadline_context_1() {
|
|
|
|
mut background := context.background()
|
2023-06-04 19:12:52 +03:00
|
|
|
mut ctx1, cancel := context.with_timeout(mut background, time.second)
|
2021-12-02 12:15:07 +03:00
|
|
|
defer {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
ctx2 := context.background()
|
|
|
|
mut ctx, _ := merge(ctx1, ctx2)
|
|
|
|
|
|
|
|
if deadline := ctx.deadline() {
|
|
|
|
assert deadline.unix_time() != 0
|
|
|
|
} else {
|
2023-06-04 19:12:52 +03:00
|
|
|
assert false
|
2021-12-02 12:15:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_merge_deadline_context_2() {
|
|
|
|
mut background := context.background()
|
|
|
|
ctx1 := context.background()
|
2023-06-04 19:12:52 +03:00
|
|
|
mut ctx2, cancel := context.with_timeout(mut background, time.second)
|
2021-12-02 12:15:07 +03:00
|
|
|
defer {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
mut ctx, _ := merge(ctx1, ctx2)
|
|
|
|
|
|
|
|
if deadline := ctx.deadline() {
|
|
|
|
assert deadline.unix_time() != 0
|
|
|
|
} else {
|
2023-06-04 19:12:52 +03:00
|
|
|
assert false
|
2021-12-02 12:15:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_merge_deadline_context_n() {
|
|
|
|
mut background := context.background()
|
|
|
|
ctx1 := context.background()
|
|
|
|
|
|
|
|
mut ctxs := []context.Context{cap: 21}
|
|
|
|
for i in 0 .. 10 {
|
|
|
|
ctxs << context.background()
|
|
|
|
}
|
2023-06-04 19:12:52 +03:00
|
|
|
mut ctx_n, _ := context.with_timeout(mut background, time.second)
|
2021-12-02 12:15:07 +03:00
|
|
|
ctxs << ctx_n
|
|
|
|
|
|
|
|
for i in 0 .. 10 {
|
|
|
|
ctxs << context.background()
|
|
|
|
}
|
|
|
|
|
|
|
|
mut ctx, cancel := merge(ctx1, ...ctxs)
|
|
|
|
|
|
|
|
assert !eventually(ctx.done())
|
|
|
|
assert ctx.err() is none
|
|
|
|
cancel()
|
|
|
|
assert eventually(ctx.done())
|
2023-06-04 19:12:52 +03:00
|
|
|
assert ctx.err().str() == 'canceled context'
|
2021-12-02 12:15:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_merge_deadline_none() {
|
|
|
|
ctx1 := context.background()
|
|
|
|
ctx2 := context.background()
|
|
|
|
|
|
|
|
mut ctx, _ := merge(ctx1, ctx2)
|
|
|
|
|
|
|
|
if _ := ctx.deadline() {
|
2023-06-04 19:12:52 +03:00
|
|
|
assert false
|
2021-12-02 12:15:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_merge_cancel_two() {
|
|
|
|
ctx1 := context.background()
|
|
|
|
ctx2 := context.background()
|
|
|
|
|
|
|
|
mut ctx, cancel := merge(ctx1, ctx2)
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
assert eventually(ctx.done())
|
2023-06-04 19:12:52 +03:00
|
|
|
assert ctx.err().str() == 'canceled context'
|
2021-12-02 12:15:07 +03:00
|
|
|
assert ctx.err().str() == 'canceled context'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_merge_cancel_multiple() {
|
|
|
|
ctx1 := context.background()
|
|
|
|
ctx2 := context.background()
|
|
|
|
ctx3 := context.background()
|
|
|
|
|
|
|
|
mut ctx, cancel := merge(ctx1, ctx2, ctx3)
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
assert eventually(ctx.done())
|
2023-06-04 19:12:52 +03:00
|
|
|
assert ctx.err().str() == 'canceled context'
|
2021-12-02 12:15:07 +03:00
|
|
|
assert ctx.err().str() == 'canceled context'
|
|
|
|
}
|