From 3c8d2bbaec5c0933c2cf0302db39e715e555d428 Mon Sep 17 00:00:00 2001 From: Ulises Jeremias Cornejo Fandos Date: Sun, 25 Apr 2021 06:04:07 -0700 Subject: [PATCH] vlib/context: some clean up and more readable examples (#9868) --- vlib/context/README.md | 36 ++++++------------------------------ vlib/context/cancel.v | 14 +++++--------- vlib/context/deadline.v | 2 +- vlib/context/deadline_test.v | 27 ++++++--------------------- vlib/context/empty_test.v | 6 ++---- 5 files changed, 20 insertions(+), 65 deletions(-) diff --git a/vlib/context/README.md b/vlib/context/README.md index fb1b02467b..4743a97f14 100644 --- a/vlib/context/README.md +++ b/vlib/context/README.md @@ -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') } } } diff --git a/vlib/context/cancel.v b/vlib/context/cancel.v index 78ae92f069..e5aaf5f64c 100644 --- a/vlib/context/cancel.v +++ b/vlib/context/cancel.v @@ -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 } } diff --git a/vlib/context/deadline.v b/vlib/context/deadline.v index 3fc050f1f0..43fd056bb2 100644 --- a/vlib/context/deadline.v +++ b/vlib/context/deadline.v @@ -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) diff --git a/vlib/context/deadline_test.v b/vlib/context/deadline_test.v index 0f052e5112..2d6a939923 100644 --- a/vlib/context/deadline_test.v +++ b/vlib/context/deadline_test.v @@ -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') } } } diff --git a/vlib/context/empty_test.v b/vlib/context/empty_test.v index 8900fb58cb..433d8c8ad0 100644 --- a/vlib/context/empty_test.v +++ b/vlib/context/empty_test.v @@ -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') } }