1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
Ulises Jeremias Cornejo Fandos
2021-04-25 06:04:07 -07:00
committed by GitHub
parent 44902b5aba
commit 3c8d2bbaec
5 changed files with 20 additions and 65 deletions

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')
}
}
}