...
1[short] skip
2
3go test -timeout=0 -run=TestNoDeadline
4go test -timeout=1m -run=TestDeadlineWithinMinute
5go test -timeout=1m -run=TestSubtestDeadlineWithinMinute
6
7-- go.mod --
8module m
9
10go 1.16
11-- deadline_test.go --
12package testing_test
13
14import (
15 "testing"
16 "time"
17)
18
19func TestNoDeadline(t *testing.T) {
20 d, ok := t.Deadline()
21 if ok || !d.IsZero() {
22 t.Fatalf("t.Deadline() = %v, %v; want 0, false", d, ok)
23 }
24}
25
26func TestDeadlineWithinMinute(t *testing.T) {
27 now := time.Now()
28 d, ok := t.Deadline()
29 if !ok || d.IsZero() {
30 t.Fatalf("t.Deadline() = %v, %v; want nonzero deadline", d, ok)
31 }
32 if !d.After(now) {
33 t.Fatalf("t.Deadline() = %v; want after start of test (%v)", d, now)
34 }
35 if d.Sub(now) > time.Minute {
36 t.Fatalf("t.Deadline() = %v; want within one minute of start of test (%v)", d, now)
37 }
38}
39
40func TestSubtestDeadlineWithinMinute(t *testing.T) {
41 t.Run("sub", func(t *testing.T) {
42 now := time.Now()
43 d, ok := t.Deadline()
44 if !ok || d.IsZero() {
45 t.Fatalf("t.Deadline() = %v, %v; want nonzero deadline", d, ok)
46 }
47 if !d.After(now) {
48 t.Fatalf("t.Deadline() = %v; want after start of test (%v)", d, now)
49 }
50 if d.Sub(now) > time.Minute {
51 t.Fatalf("t.Deadline() = %v; want within one minute of start of test (%v)", d, now)
52 }
53 })
54}
View as plain text