-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testing: testing: add (*T).Deadline method for test timeout
Fixes #28135 Change-Id: I62818595eaf4a59d8b5c26cd6848c08fec795ad1 Reviewed-on: https://go-review.googlesource.com/c/go/+/202758 Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
- Loading branch information
Bryan C. Mills
committed
Feb 21, 2020
1 parent
1b47fde
commit 9ca5792
Showing
4 changed files
with
91 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pkg testing, method (*T) Deadline() (time.Time, bool) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
[short] skip | ||
|
||
go test -timeout=0 -run=TestNoDeadline | ||
go test -timeout=1m -run=TestDeadlineWithinMinute | ||
go test -timeout=1m -run=TestSubtestDeadlineWithinMinute | ||
|
||
-- deadline_test.go -- | ||
package testing_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNoDeadline(t *testing.T) { | ||
d, ok := t.Deadline() | ||
if ok || !d.IsZero() { | ||
t.Fatalf("t.Deadline() = %v, %v; want 0, false", d, ok) | ||
} | ||
} | ||
|
||
func TestDeadlineWithinMinute(t *testing.T) { | ||
now := time.Now() | ||
d, ok := t.Deadline() | ||
if !ok || d.IsZero() { | ||
t.Fatalf("t.Deadline() = %v, %v; want nonzero deadline", d, ok) | ||
} | ||
if !d.After(now) { | ||
t.Fatalf("t.Deadline() = %v; want after start of test (%v)", d, now) | ||
} | ||
if d.Sub(now) > time.Minute { | ||
t.Fatalf("t.Deadline() = %v; want within one minute of start of test (%v)", d, now) | ||
} | ||
} | ||
|
||
func TestSubtestDeadlineWithinMinute(t *testing.T) { | ||
t.Run("sub", func(t *testing.T) { | ||
now := time.Now() | ||
d, ok := t.Deadline() | ||
if !ok || d.IsZero() { | ||
t.Fatalf("t.Deadline() = %v, %v; want nonzero deadline", d, ok) | ||
} | ||
if !d.After(now) { | ||
t.Fatalf("t.Deadline() = %v; want after start of test (%v)", d, now) | ||
} | ||
if d.Sub(now) > time.Minute { | ||
t.Fatalf("t.Deadline() = %v; want within one minute of start of test (%v)", d, now) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters