Skip to content

Commit 4ada943

Browse files
committed
unix: test nonblocking and close-on-exec behavior of Pipe2
Following TestNonblockingPipe in package runtime. Change-Id: Ifacec3ea6ce372a891273e3ec9fa4194f7d17619 Reviewed-on: https://go-review.googlesource.com/c/sys/+/295869 Trust: Tobias Klauser <[email protected]> Run-TryBot: Tobias Klauser <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 2738c01 commit 4ada943

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

unix/pipe2_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,42 @@ func TestPipe2(t *testing.T) {
5454
t.Fatalf("bad close: %v", err)
5555
}
5656
}
57+
58+
func checkNonblocking(t *testing.T, fd int, name string) {
59+
t.Helper()
60+
flags, err := unix.FcntlInt(uintptr(fd), unix.F_GETFL, 0)
61+
if err != nil {
62+
t.Errorf("fcntl(%s, F_GETFL) failed: %v", name, err)
63+
} else if flags&unix.O_NONBLOCK == 0 {
64+
t.Errorf("O_NONBLOCK not set in %s flags %#x", name, flags)
65+
}
66+
}
67+
68+
func checkCloseonexec(t *testing.T, fd int, name string) {
69+
t.Helper()
70+
flags, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0)
71+
if err != nil {
72+
t.Errorf("fcntl(%s, F_GETFD) failed: %v", name, err)
73+
} else if flags&unix.FD_CLOEXEC == 0 {
74+
t.Errorf("FD_CLOEXEC not set in %s flags %#x", name, flags)
75+
}
76+
}
77+
78+
func TestNonblockingPipe2(t *testing.T) {
79+
var pipes [2]int
80+
err := unix.Pipe2(pipes[:], unix.O_NONBLOCK|unix.O_CLOEXEC)
81+
if err != nil {
82+
t.Fatalf("pipe2: %v", err)
83+
}
84+
r := pipes[0]
85+
w := pipes[1]
86+
defer func() {
87+
unix.Close(r)
88+
unix.Close(w)
89+
}()
90+
91+
checkNonblocking(t, r, "reader")
92+
checkCloseonexec(t, r, "reader")
93+
checkNonblocking(t, w, "writer")
94+
checkCloseonexec(t, w, "writer")
95+
}

0 commit comments

Comments
 (0)