Skip to content

Commit

Permalink
refine boundary test
Browse files Browse the repository at this point in the history
  • Loading branch information
amyangfei committed May 8, 2019
1 parent ca54fdb commit 444c539
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions pkg/hello/hello.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hello

import (
"context"
"fmt"
"os"
"sync"
Expand Down Expand Up @@ -67,15 +68,26 @@ func SubRoutineExit() {

func Boundary() {
var wg sync.WaitGroup

ctx, cancel := context.WithCancel(context.Background())

go func(cctx context.Context) {
fmt.Println("before sleep")
select {
case <-cctx.Done():
fmt.Println("sleep boundary exit")
return
case <-time.After(time.Second * 300):
}
fmt.Println("after long sleep")
}(ctx)

wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(time.Millisecond * 100)
}()
go func() {
fmt.Println("before sleep")
time.Sleep(time.Second * 300)
fmt.Println("after sleep")
}()

wg.Wait()
cancel()
}

0 comments on commit 444c539

Please sign in to comment.