Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions net_dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ func DialConnection(network, address string, timeout time.Duration) (connection
return defaultDialer.DialConnection(network, address, timeout)
}

// NewFDConnection create a Connection initialed by any fd
// It's useful for write unit test for functions have args with the type of netpoll.Connection
// The typical usage like:
//
// rfd, wfd := netpoll.GetSysFdPairs()
// rconn, _ = netpoll.NewFDConnection(rfd)
// wconn, _ = netpoll.NewFDConnection(wfd)
func NewFDConnection(fd int) (Connection, error) {
conn := new(connection)
err := conn.init(&netFD{fd: fd}, nil)
if err != nil {
return nil, err
}
return conn, nil
}

// NewDialer only support TCP and unix socket now.
func NewDialer() Dialer {
return &dialer{}
Expand Down
15 changes: 15 additions & 0 deletions net_dialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ func TestDialerThenClose(t *testing.T) {
wg.Wait()
}

func TestNewFDConnection(t *testing.T) {
r, w := GetSysFdPairs()
rconn, err := NewFDConnection(r)
MustNil(t, err)
wconn, err := NewFDConnection(w)
MustNil(t, err)
_, err = rconn.Writer().WriteString("hello")
MustNil(t, err)
err = rconn.Writer().Flush()
MustNil(t, err)
buf, err := wconn.Reader().Next(5)
MustNil(t, err)
Equal(t, string(buf), "hello")
}

func mockDialerEventLoop(idx int) EventLoop {
el, _ := NewEventLoop(func(ctx context.Context, conn Connection) (err error) {
defer func() {
Expand Down