mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
@@ -10,9 +10,9 @@ fn test_with_cancel() {
|
||||
// The callers of gen need to cancel the context once
|
||||
// they are done consuming generated integers not to leak
|
||||
// the internal routine started by gen.
|
||||
gen := fn (ctx context.Context) chan int {
|
||||
gen := fn (mut ctx context.Context) chan int {
|
||||
dst := chan int{}
|
||||
go fn (ctx context.Context, dst chan int) {
|
||||
go fn (mut ctx context.Context, dst chan int) {
|
||||
mut v := 0
|
||||
ch := ctx.done()
|
||||
for {
|
||||
@@ -26,16 +26,20 @@ fn test_with_cancel() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}(ctx, dst)
|
||||
}(mut ctx, dst)
|
||||
return dst
|
||||
}
|
||||
|
||||
ctx, cancel := context.with_cancel(context.background())
|
||||
mut background := context.background()
|
||||
mut b := &background
|
||||
mut ctx, cancel := context.with_cancel(mut b)
|
||||
defer {
|
||||
cancel()
|
||||
}
|
||||
|
||||
ch := gen(ctx)
|
||||
mut mut_ctx := ctx
|
||||
mut ctx2 := &mut_ctx
|
||||
ch := gen(mut ctx2)
|
||||
for i in 0 .. 5 {
|
||||
v := <-ch
|
||||
assert i == v
|
||||
|
||||
Reference in New Issue
Block a user