1 [short] skip
2
3 go test -timeout=0 -run=TestNoDeadline
4 go test -timeout=1m -run=TestDeadlineWithinMinute
5 go test -timeout=1m -run=TestSubtestDeadlineWithinMinute
6
7 -- go.mod --
8 module m
9
10 go 1.16
11 -- deadline_test.go --
12 package testing_test
13
14 import (
15 "testing"
16 "time"
17 )
18
19 func 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
26 func 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
40 func 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 }
55
View as plain text