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
)
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
// function that it should abandon its work as soon as it gets to it.
fn example_with_deadline() {
@ -105,14 +96,11 @@ fn example_with_deadline() {
context.cancel(ctx)
}
after_ch := after(1 * time.second)
ctx_ch := ctx.done()
select {
_ := <-after_ch {
assert false
}
_ := <-ctx_ch {
assert true
_ := <-ctx_ch {}
> 1 * time.second {
panic('This should not happen')
}
}
}
@ -129,15 +117,6 @@ const (
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
// it should abandon its work after the timeout elapses.
fn example_with_timeout() {
@ -148,14 +127,11 @@ fn example_with_timeout() {
context.cancel(ctx)
}
after_ch := after(1 * time.second)
ctx_ch := ctx.done()
select {
_ := <-after_ch {
assert false
}
_ := <-ctx_ch {
assert true
_ := <-ctx_ch {}
> 1 * time.second {
panic('This should not happen')
}
}
}

View File

@ -90,12 +90,12 @@ pub fn (ctx CancelContext) str() string {
}
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')
}
ctx.mutex.@lock()
if ctx.err.str() != 'none' {
if !(ctx.err is none) {
ctx.mutex.unlock()
// already canceled
return
@ -129,28 +129,24 @@ fn propagate_cancel(parent Context, mut child Canceler) {
child.cancel(false, parent.err())
return
}
else {}
}
mut p := parent_cancel_context(parent) or {
go fn (parent Context, mut child Canceler) {
pdone := parent.done()
cdone := child.done()
select {
_ := <-pdone {
child.cancel(false, parent.err())
}
_ := <-cdone {}
else {}
}
}(parent, mut child)
return
}
if p.err.str() != 'none' {
if p.err is none {
p.children[child.id] = *child
} else {
// parent has already been canceled
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)
}
if ctx.err().str() == 'none' {
if ctx.err() is none {
go fn (mut ctx TimerContext, dur time.Duration) {
time.sleep(dur)
ctx.cancel(true, deadline_exceeded)

View File

@ -6,15 +6,6 @@ const (
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
// function that it should abandon its work as soon as it gets to it.
fn test_with_deadline() {
@ -28,14 +19,11 @@ fn test_with_deadline() {
context.cancel(ctx)
}
after_ch := after(1 * time.second)
ctx_ch := ctx.done()
select {
_ := <-after_ch {
assert false
}
_ := <-ctx_ch {
assert true
_ := <-ctx_ch {}
> 1 * time.second {
panic('This should not happen')
}
}
}
@ -50,14 +38,11 @@ fn test_with_timeout() {
context.cancel(ctx)
}
after_ch := after(1 * time.second)
ctx_ch := ctx.done()
select {
_ := <-after_ch {
assert false
}
_ := <-ctx_ch {
assert true
_ := <-ctx_ch {}
> 1 * time.second {
panic('This should not happen')
}
}
}

View File

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