Skip to content

Commit

Permalink
GODRIVER-141: Expose local address in conn package
Browse files Browse the repository at this point in the history
  • Loading branch information
MartyZhang authored and saghm committed Dec 6, 2017
1 parent b3a8ba6 commit fa5e1f1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mongo/internal/conntest/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package conntest
import (
"context"
"fmt"
"net"

"github.com/10gen/mongo-go-driver/mongo/model"
"github.com/10gen/mongo-go-driver/mongo/private/msg"
Expand Down Expand Up @@ -56,6 +57,11 @@ func (c *MockConnection) Expired() bool {
return c.Dead
}

// LocalAddr returns nil.
func (c *MockConnection) LocalAddr() net.Addr {
return nil
}

// Read reads a server response from the MockConnection.
func (c *MockConnection) Read(ctx context.Context, responseTo int32) (msg.Response, error) {
if c.ReadErr != nil {
Expand Down
5 changes: 5 additions & 0 deletions mongo/internal/servertest/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package servertest
import (
"context"
"fmt"
"net"
"sync"

"github.com/10gen/mongo-go-driver/bson"
Expand Down Expand Up @@ -90,6 +91,10 @@ func (c *fakeMonitorConn) CloseIgnoreError() {
_ = c.Close()
}

func (c *fakeMonitorConn) LocalAddr() net.Addr {
return nil
}

func (c *fakeMonitorConn) MarkDead() {
c.Lock()
defer c.Unlock()
Expand Down
5 changes: 5 additions & 0 deletions mongo/private/cluster/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package cluster_test
import (
"context"
"fmt"
"net"
"sync"
"testing"

Expand Down Expand Up @@ -77,6 +78,10 @@ func (c *fakeMonitorConn) Model() *model.Conn {
return &model.Conn{}
}

func (c *fakeMonitorConn) LocalAddr() net.Addr {
return nil
}

func (c *fakeMonitorConn) Expired() bool {
return c.dead
}
Expand Down
7 changes: 7 additions & 0 deletions mongo/private/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ type Connection interface {
Model() *model.Conn
// Expired indicates if the connection has expired.
Expired() bool
// LocalAddr returns the local address of the connection.
LocalAddr() net.Addr
// Read reads a message from the connection.
Read(context.Context, int32) (msg.Response, error)
// Write writes a number of messages to the connection.
Expand Down Expand Up @@ -176,6 +178,11 @@ func (c *connImpl) Expired() bool {
return c.dead
}

// LocalAddr returns the local address of a connection.
func (c *connImpl) LocalAddr() net.Addr {
return c.rw.LocalAddr()
}

func (c *connImpl) Read(ctx context.Context, responseTo int32) (msg.Response, error) {
if c.dead {
return nil, &Error{
Expand Down
9 changes: 9 additions & 0 deletions mongo/private/conn/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package conn_test
import (
"context"
"fmt"
"net"
"testing"
"time"

Expand Down Expand Up @@ -207,6 +208,14 @@ func TestConnection_Read_timeout(t *testing.T) {
require.False(t, subject.Alive())
}

func TestConnection_LocalAddr(t *testing.T) {
t.Parallel()
subject, err := createIntegrationTestConn()
require.NoError(t, err)
localAddr := subject.LocalAddr()
require.NotEqual(t, localAddr, &net.TCPAddr{})
}

func TestConnection_Read_after_connection_is_dead(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode")
Expand Down

0 comments on commit fa5e1f1

Please sign in to comment.