vlib/context: some clean up and more readable examples (#9868)

pull/9876/head
Ulises Jeremias Cornejo Fandos 2021-04-25 06:04:07 -07:00 committed by GitHub
parent 44902b5aba
commit 3c8d2bbaec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 65 deletions

View File

@ -83,15 +83,6 @@ const (
short_duration = 1 * time.millisecond short_duration = 1 * time.millisecond
) )
fn after(dur time.Duration) chan int {
dst := chan int{}
go fn (dur time.Duration, dst chan int) {
time.sleep(dur)
dst <- 0
}(dur, dst)
return dst
}
// This example passes a context with an arbitrary deadline to tell a blocking // This example passes a context with an arbitrary deadline to tell a blocking
// function that it should abandon its work as soon as it gets to it. // function that it should abandon its work as soon as it gets to it.
fn example_with_deadline() { fn example_with_deadline() {
@ -105,14 +96,11 @@ fn example_with_deadline() {
context.cancel(ctx) context.cancel(ctx)
} }
after_ch := after(1 * time.second)
ctx_ch := ctx.done() ctx_ch := ctx.done()
select { select {
_ := <-after_ch { _ := <-ctx_ch {}
assert false > 1 * time.second {
} panic('This should not happen')
_ := <-ctx_ch {
assert true
} }
} }
} }
@ -129,15 +117,6 @@ const (
short_duration = 1 * time.millisecond short_duration = 1 * time.millisecond
) )
fn after(dur time.Duration) chan int {
dst := chan int{}
go fn (dur time.Duration, dst chan int) {
time.sleep(dur)
dst <- 0
}(dur, dst)
return dst
}
// This example passes a context with a timeout to tell a blocking function that // This example passes a context with a timeout to tell a blocking function that
// it should abandon its work after the timeout elapses. // it should abandon its work after the timeout elapses.
fn example_with_timeout() { fn example_with_timeout() {
@ -148,14 +127,11 @@ fn example_with_timeout() {
context.cancel(ctx) context.cancel(ctx)
} }
after_ch := after(1 * time.second)
ctx_ch := ctx.done() ctx_ch := ctx.done()
select { select {
_ := <-after_ch { _ := <-ctx_ch {}
assert false > 1 * time.second {
} panic('This should not happen')
_ := <-ctx_ch {
assert true
} }
} }
} }

View File

@ -90,12 +90,12 @@ pub fn (ctx CancelContext) str() string {
} }
fn (mut ctx CancelContext) cancel(remove_from_parent bool, err IError) { fn (mut ctx CancelContext) cancel(remove_from_parent bool, err IError) {
if err.str() == 'none' { if err is none {
panic('context: internal error: missing cancel error') panic('context: internal error: missing cancel error')
} }
ctx.mutex.@lock() ctx.mutex.@lock()
if ctx.err.str() != 'none' { if !(ctx.err is none) {
ctx.mutex.unlock() ctx.mutex.unlock()
// already canceled // already canceled
return return
@ -129,28 +129,24 @@ fn propagate_cancel(parent Context, mut child Canceler) {
child.cancel(false, parent.err()) child.cancel(false, parent.err())
return return
} }
else {}
} }
mut p := parent_cancel_context(parent) or { mut p := parent_cancel_context(parent) or {
go fn (parent Context, mut child Canceler) { go fn (parent Context, mut child Canceler) {
pdone := parent.done() pdone := parent.done()
cdone := child.done()
select { select {
_ := <-pdone { _ := <-pdone {
child.cancel(false, parent.err()) child.cancel(false, parent.err())
} }
_ := <-cdone {}
else {}
} }
}(parent, mut child) }(parent, mut child)
return return
} }
if p.err.str() != 'none' { if p.err is none {
p.children[child.id] = *child
} else {
// parent has already been canceled // parent has already been canceled
child.cancel(false, p.err) child.cancel(false, p.err)
} else {
p.children[child.id] = *child
} }
} }

View File

@ -47,7 +47,7 @@ pub fn with_deadline(parent Context, d time.Time) Context {
return Context(ctx) return Context(ctx)
} }
if ctx.err().str() == 'none' { if ctx.err() is none {
go fn (mut ctx TimerContext, dur time.Duration) { go fn (mut ctx TimerContext, dur time.Duration) {
time.sleep(dur) time.sleep(dur)
ctx.cancel(true, deadline_exceeded) ctx.cancel(true, deadline_exceeded)

View File

@ -6,15 +6,6 @@ const (
short_duration = 1 * time.millisecond short_duration = 1 * time.millisecond
) )
fn after(dur time.Duration) chan int {
dst := chan int{}
go fn (dur time.Duration, dst chan int) {
time.sleep(dur)
dst <- 0
}(dur, dst)
return dst
}
// This example passes a context with an arbitrary deadline to tell a blocking // This example passes a context with an arbitrary deadline to tell a blocking
// function that it should abandon its work as soon as it gets to it. // function that it should abandon its work as soon as it gets to it.
fn test_with_deadline() { fn test_with_deadline() {
@ -28,14 +19,11 @@ fn test_with_deadline() {
context.cancel(ctx) context.cancel(ctx)
} }
after_ch := after(1 * time.second)
ctx_ch := ctx.done() ctx_ch := ctx.done()
select { select {
_ := <-after_ch { _ := <-ctx_ch {}
assert false > 1 * time.second {
} panic('This should not happen')
_ := <-ctx_ch {
assert true
} }
} }
} }
@ -50,14 +38,11 @@ fn test_with_timeout() {
context.cancel(ctx) context.cancel(ctx)
} }
after_ch := after(1 * time.second)
ctx_ch := ctx.done() ctx_ch := ctx.done()
select { select {
_ := <-after_ch { _ := <-ctx_ch {}
assert false > 1 * time.second {
} panic('This should not happen')
_ := <-ctx_ch {
assert true
} }
} }
} }

View File

@ -4,8 +4,7 @@ fn test_background() {
ctx := background() ctx := background()
assert 'context.Background' == ctx.str() assert 'context.Background' == ctx.str()
if _ := ctx.value('') { if _ := ctx.value('') {
println('This should not happen') panic('This should never happen')
assert false
} }
} }
@ -13,7 +12,6 @@ fn test_todo() {
ctx := todo() ctx := todo()
assert 'context.TODO' == ctx.str() assert 'context.TODO' == ctx.str()
if _ := ctx.value('') { if _ := ctx.value('') {
println('This should not happen') panic('This should never happen')
assert false
} }
} }