Log(args ...interface{})
Logf(format string, args ...interface{})
Name() string
+ Parallel()
Skip(args ...interface{})
SkipNow()
Skipf(format string, args ...interface{})
}
func XTestDeadline(t testingT) {
+ t.Parallel()
+
c, _ := WithDeadline(Background(), time.Now().Add(shortDuration))
if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) {
t.Errorf("c.String() = %q want prefix %q", got, prefix)
}
func XTestTimeout(t testingT) {
+ t.Parallel()
+
c, _ := WithTimeout(Background(), shortDuration)
if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) {
t.Errorf("c.String() = %q want prefix %q", got, prefix)
gccgoLimit: 3,
},
{
- desc: "WithTimeout(bg, 15*time.Millisecond)",
+ desc: "WithTimeout(bg, 1*time.Nanosecond)",
f: func() {
- c, _ := WithTimeout(bg, 15*time.Millisecond)
+ c, _ := WithTimeout(bg, 1*time.Nanosecond)
<-c.Done()
},
limit: 12,
}
func testLayers(t testingT, seed int64, testTimeout bool) {
- rand.Seed(seed)
+ t.Parallel()
+
+ r := rand.New(rand.NewSource(seed))
errorf := func(format string, a ...interface{}) {
t.Errorf(fmt.Sprintf("seed=%d: %s", seed, format), a...)
}
ctx = Background()
)
for i := 0; i < minLayers || numTimers == 0 || len(cancels) == 0 || len(vals) == 0; i++ {
- switch rand.Intn(3) {
+ switch r.Intn(3) {
case 0:
v := new(value)
ctx = WithValue(ctx, v, v)
}
}
}
- select {
- case <-ctx.Done():
- errorf("ctx should not be canceled yet")
- default:
+ if !testTimeout {
+ select {
+ case <-ctx.Done():
+ errorf("ctx should not be canceled yet")
+ default:
+ }
}
if s, prefix := fmt.Sprint(ctx), "context.Background."; !strings.HasPrefix(s, prefix) {
t.Errorf("ctx.String() = %q want prefix %q", s, prefix)
}
checkValues("after timeout")
} else {
- cancel := cancels[rand.Intn(len(cancels))]
+ cancel := cancels[r.Intn(len(cancels))]
cancel()
select {
case <-ctx.Done():
"time"
)
+const shortDuration = 1 * time.Millisecond // a reasonable duration to block in an example
+
// This example demonstrates the use of a cancelable context to prevent a
// goroutine leak. By the end of the example function, the goroutine started
// by gen will return without leaking.
// 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.
func ExampleWithDeadline() {
- d := time.Now().Add(50 * time.Millisecond)
+ d := time.Now().Add(shortDuration)
ctx, cancel := context.WithDeadline(context.Background(), d)
// Even though ctx will be expired, it is good practice to call its
func ExampleWithTimeout() {
// Pass a context with a timeout to tell a blocking function that it
// should abandon its work after the timeout elapses.
- ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
+ ctx, cancel := context.WithTimeout(context.Background(), shortDuration)
defer cancel()
select {