Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatDiscovery committed Dec 16, 2023
2 parents 4bbad4d + 5c57b34 commit 7754c80
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test/concurrent/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package concurrent
import (
"context"
"fmt"
"sync"
"testing"
"time"
)
Expand All @@ -13,6 +14,33 @@ type otherContext struct {
context.Context
}

func TestTimeOut(t *testing.T) {
root := context.Background()
tm := time.Now().Add(3 * time.Second)
ctxb, cancel := context.WithDeadline(root, tm)
defer cancel()

ch := make(chan struct{})
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
time.Sleep(2 * time.Second)
ch <- struct{}{}
}()
go func(ctx context.Context) {
defer wg.Done()
select {
case <-ctx.Done():
fmt.Printf("get msg to cancel")
return
case <-ch:
fmt.Println("work is done")
}
}(ctxb)
wg.Wait()

}

func TestMultiValue(t *testing.T) {
root := context.Background()
c1 := context.WithValue(root, "key1", "value1")
Expand Down

0 comments on commit 7754c80

Please sign in to comment.