unix: deflake TestPselect and TestSelect on linux
On some platforms (namely Linux), Select and Pselect update the
passed-in timeval/timespec, which might lead to failing tests in
case of an EINTR. Fix this by always resetting the timeval/timespec
before calling Select/Pselect.
Also change accept timeouts within 2/3 margin of the expected timeout,
like already done in TestSelect. While at it, fix the failure log to use
the common "got X, expected Y" message.
Fixes golang/go#42210
Change-Id: Id0efbbecc9c0bf44c102d5d1880b1bae32980ad1
Reviewed-on: https://go-review.googlesource.com/c/sys/+/265020
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Tobias Klauser authored 3 years ago
Tobias Klauser committed 3 years ago
233 | 233 |
}
|
234 | 234 |
|
235 | 235 |
dur := 2500 * time.Microsecond
|
236 | |
ts := unix.NsecToTimespec(int64(dur))
|
237 | 236 |
var took time.Duration
|
238 | 237 |
for {
|
|
238 |
// On some platforms (e.g. Linux), the passed-in timespec is
|
|
239 |
// updated by pselect(2). Make sure to reset to the full
|
|
240 |
// duration in case of an EINTR.
|
|
241 |
ts := unix.NsecToTimespec(int64(dur))
|
239 | 242 |
start := time.Now()
|
240 | 243 |
n, err := unix.Pselect(0, nil, nil, nil, &ts, nil)
|
241 | 244 |
took = time.Since(start)
|
|
251 | 254 |
break
|
252 | 255 |
}
|
253 | 256 |
|
254 | |
if took < dur {
|
255 | |
t.Errorf("Pselect: timeout should have been at least %v, got %v", dur, took)
|
|
257 |
// On some builder the actual timeout might also be slightly less than the requested.
|
|
258 |
// Add an acceptable margin to avoid flaky tests.
|
|
259 |
if took < dur*2/3 {
|
|
260 |
t.Errorf("Pselect: got %v timeout, expected at least %v", took, dur)
|
256 | 261 |
}
|
257 | 262 |
}
|
258 | 263 |
|
536 | 536 |
}
|
537 | 537 |
|
538 | 538 |
dur := 250 * time.Millisecond
|
539 | |
tv := unix.NsecToTimeval(int64(dur))
|
540 | 539 |
var took time.Duration
|
541 | 540 |
for {
|
|
541 |
// On some platforms (e.g. Linux), the passed-in timeval is
|
|
542 |
// updated by select(2). Make sure to reset to the full duration
|
|
543 |
// in case of an EINTR.
|
|
544 |
tv := unix.NsecToTimeval(int64(dur))
|
542 | 545 |
start := time.Now()
|
543 | 546 |
n, err := unix.Select(0, nil, nil, nil, &tv)
|
544 | 547 |
took = time.Since(start)
|