1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
10 "internal/syscall/unix"
14 // For backward compatibility, opening a net.Conn, turning it into an os.File,
15 // and calling the Fd method should return a blocking descriptor.
16 func TestFileFdBlocks(t *testing.T) {
17 if !testableNetwork("unix") {
18 t.Skipf("skipping: unix sockets not supported")
21 ls := newLocalServer(t, "unix")
24 errc := make(chan error, 1)
25 done := make(chan bool)
26 handler := func(ls *localServer, ln Listener) {
27 server, err := ln.Accept()
35 if err := ls.buildup(handler); err != nil {
40 client, err := Dial(ls.Listener.Addr().Network(), ls.Listener.Addr().String())
46 if err := <-errc; err != nil {
47 t.Fatalf("server error: %v", err)
50 // The socket should be non-blocking.
51 rawconn, err := client.(*UnixConn).SyscallConn()
55 err = rawconn.Control(func(fd uintptr) {
56 nonblock, err := unix.IsNonblock(int(fd))
61 t.Fatal("unix socket is in blocking mode")
68 file, err := client.(*UnixConn).File()
73 // At this point the descriptor should still be non-blocking.
74 rawconn, err = file.SyscallConn()
78 err = rawconn.Control(func(fd uintptr) {
79 nonblock, err := unix.IsNonblock(int(fd))
84 t.Fatal("unix socket as os.File is in blocking mode")
93 // Calling Fd should have put the descriptor into blocking mode.
94 nonblock, err := unix.IsNonblock(int(fd))
99 t.Error("unix socket through os.File.Fd is non-blocking")