unix: add NFDBITS const on Linux, update TestSelect

Adjust TestSelect on Linux to match the Darwin/*BSD changes in CL
196802.

Updates golang/go#34458

Change-Id: Ia17fdadf7091001ea785391da23aaf9d3ec4ac5e
Reviewed-on: https://go-review.googlesource.com/c/sys/+/196806
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Tobias Klauser
2019-09-24 15:45:21 +00:00
committed by Tobias Klauser
parent e2fefa8ec2
commit 2837fb4f24
15 changed files with 46 additions and 2 deletions
+32 -2
View File
@@ -238,23 +238,53 @@ func TestRlimitAs(t *testing.T) {
}
func TestSelect(t *testing.T) {
_, err := unix.Select(0, nil, nil, nil, &unix.Timeval{Sec: 0, Usec: 0})
n, err := unix.Select(0, nil, nil, nil, &unix.Timeval{Sec: 0, Usec: 0})
if err != nil {
t.Fatalf("Select: %v", err)
}
if n != 0 {
t.Fatalf("Select: expected 0 ready file descriptors, got %v", n)
}
dur := 150 * time.Millisecond
tv := unix.NsecToTimeval(int64(dur))
start := time.Now()
_, err = unix.Select(0, nil, nil, nil, &tv)
n, err = unix.Select(0, nil, nil, nil, &tv)
took := time.Since(start)
if err != nil {
t.Fatalf("Select: %v", err)
}
if n != 0 {
t.Fatalf("Select: expected 0 ready file descriptors, got %v", n)
}
if took < dur {
t.Errorf("Select: timeout should have been at least %v, got %v", dur, took)
}
rr, ww, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
defer rr.Close()
defer ww.Close()
if _, err := ww.Write([]byte("HELLO GOPHER")); err != nil {
t.Fatal(err)
}
rFdSet := &unix.FdSet{}
fd := rr.Fd()
// FD_SET(fd, rFdSet)
rFdSet.Bits[fd/unix.NFDBITS] |= (1 << (fd % unix.NFDBITS))
n, err = unix.Select(int(fd+1), rFdSet, nil, nil, nil)
if err != nil {
t.Fatalf("Select: %v", err)
}
if n != 1 {
t.Fatalf("Select: expected 1 ready file descriptors, got %v", n)
}
}
func TestPselect(t *testing.T) {