os: deflake TestNewFileNonBlock

Fixes #32325

Change-Id: Ic06938c36a25ef1a6623e35e128b73729d02d955
Reviewed-on: https://go-review.googlesource.com/c/go/+/179698
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Ian Lance Taylor 2019-05-30 18:08:08 -04:00
parent ba66d89d78
commit 3cee55057f
1 changed files with 10 additions and 3 deletions

View File

@ -259,12 +259,19 @@ func newFileTest(t *testing.T, blocking bool) {
}
defer file.Close()
timeToWrite := 100 * time.Millisecond
timeToDeadline := 1 * time.Millisecond
if !blocking {
// Use a longer time to avoid flakes.
// We won't be waiting this long anyhow.
timeToWrite = 1 * time.Second
}
// Try to read with deadline (but don't block forever).
b := make([]byte, 1)
// Send something after 100ms.
timer := time.AfterFunc(100*time.Millisecond, func() { syscall.Write(p[1], []byte("a")) })
timer := time.AfterFunc(timeToWrite, func() { syscall.Write(p[1], []byte("a")) })
defer timer.Stop()
file.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
file.SetReadDeadline(time.Now().Add(timeToDeadline))
_, err := file.Read(b)
if !blocking {
// We want it to fail with a timeout.