Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(netcore): add tests for WrapConn #60

Merged
merged 1 commit into from
Feb 2, 2025
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
2 changes: 1 addition & 1 deletion netcore/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type connWrapper struct {
closeonce sync.Once
conn net.Conn
laddr string
netx *Network
netx *Network // may contain nil logger!
protocol string
raddr string
}
Expand Down
48 changes: 43 additions & 5 deletions netcore/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestConnLocalAddr(t *testing.T) {
func Test_connLocalAddr(t *testing.T) {
t.Run("nil connection", func(t *testing.T) {
addr := connLocalAddr(nil)
assert.Equal(t, "", addr.Network())
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestConnLocalAddr(t *testing.T) {
})
}

func TestConnRemoteAddr(t *testing.T) {
func Test_connRemoteAddr(t *testing.T) {
t.Run("nil connection", func(t *testing.T) {
addr := connRemoteAddr(nil)
assert.Equal(t, "", addr.Network())
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestConnRemoteAddr(t *testing.T) {
})
}

func TestMaybeWrapConn(t *testing.T) {
func TestNetwork_maybeWrapConn(t *testing.T) {
t.Run("nil connection", func(t *testing.T) {
nx := &Network{}
assert.Nil(t, nx.maybeWrapConn(context.Background(), nil))
Expand Down Expand Up @@ -113,11 +113,49 @@ func TestMaybeWrapConn(t *testing.T) {
}
wrapped := nx.maybeWrapConn(context.Background(), conn)
assert.NotEqual(t, conn, wrapped) // should return wrapped
assert.IsType(t, &connWrapper{}, wrapped)
inner, ok := wrapped.(*connWrapper)
assert.True(t, ok)
assert.Equal(t, inner.netx, nx)
})
}

func TestConnWrapper(t *testing.T) {
func TestWrapConn(t *testing.T) {
t.Run("correctly initializes wrapper", func(t *testing.T) {
nx := &Network{
Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
WrapConn: WrapConn,
}
mock := &mocks.Conn{
MockLocalAddr: func() net.Addr {
return &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 1234}
},
MockRemoteAddr: func() net.Addr {
return &net.TCPAddr{IP: net.ParseIP("1.1.1.1"), Port: 443}
},
}

conn := WrapConn(context.Background(), nx, mock)
wrapped, ok := conn.(*connWrapper)
assert.True(t, ok)
assert.Equal(t, nx, wrapped.netx)
})

t.Run("handles nil addresses gracefully", func(t *testing.T) {
nx := &Network{}
mock := &mocks.Conn{
MockLocalAddr: func() net.Addr { return nil },
MockRemoteAddr: func() net.Addr { return nil },
}

conn := WrapConn(context.Background(), nx, mock)
wrapped, ok := conn.(*connWrapper)
assert.True(t, ok)
assert.Equal(t, "", wrapped.laddr)
assert.Equal(t, "", wrapped.raddr)
})
}

func Test_connWrapper(t *testing.T) {
t.Run("Close", func(t *testing.T) {
// Helper function to create a standard test environment
setup := func() (*bytes.Buffer, *mocks.Conn, *connWrapper, time.Time) {
Expand Down