github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/toolkit/internal/common/sleep_test.go (about) 1 package common_test 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 "time" 8 9 "github.com/unionj-cloud/go-doudou/toolkit/internal/common" 10 ) 11 12 func TestSleep(test *testing.T) { 13 const dt = 50 * time.Millisecond 14 t := func(name string, ctx context.Context, expected error) { 15 test.Run(name, func(test *testing.T) { 16 err := common.Sleep(ctx, dt) 17 if !errors.Is(err, expected) { 18 test.Errorf("expected %v, got %v", expected, err) 19 } 20 }) 21 } 22 23 ctx := context.Background() 24 canceled, cancel := context.WithCancel(ctx) 25 cancel() 26 27 t("background context", ctx, nil) 28 t("canceled context", canceled, context.Canceled) 29 }