Skip to content

Commit

Permalink
Expose NATS Conn
Browse files Browse the repository at this point in the history
Add a NatsConn method to the Conn interface which allows retrieving the
underlying NATS Conn. Addresses nats-io#67.
  • Loading branch information
tylertreat committed Jul 2, 2016
1 parent 802c80a commit c4cc894
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
12 changes: 12 additions & 0 deletions stan.go
Expand Up @@ -50,6 +50,11 @@ type Conn interface {

// Close
Close() error

// NatsConn returns the underlying NATS conn. Use this with care. For
// example, closing the wrapped NATS conn will put the NATS Streaming Conn
// in an invalid state.
NatsConn() *nats.Conn
}

// Errors
Expand Down Expand Up @@ -271,6 +276,13 @@ func (sc *conn) Close() error {
return nil
}

// NatsConn returns the underlying NATS conn. Use this with care. For example,
// closing the wrapped NATS conn will put the NATS Streaming Conn in an invalid
// state.
func (sc *conn) NatsConn() *nats.Conn {
return sc.nc
}

// Process a heartbeat from the NATS Streaming cluster
func (sc *conn) processHeartBeat(m *nats.Msg) {
// No payload assumed, just reply.
Expand Down
35 changes: 35 additions & 0 deletions stan_test.go
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/nats-io/nats"
"github.com/nats-io/nats-streaming-server/server"
"github.com/nats-io/nats-streaming-server/test"
natstest "github.com/nats-io/nats/test"

natsd "github.com/nats-io/gnatsd/test"
)
Expand Down Expand Up @@ -1853,3 +1854,37 @@ func TestRaceAckOnClose(t *testing.T) {
time.Sleep(10 * time.Millisecond)
sc.Close()
}

func TestNatsConn(t *testing.T) {
s := RunServer(clusterName)
defer s.Shutdown()
sc := NewDefaultConnection(t)
defer sc.Close()

// Make sure we can get the STAN-created Conn.
nc := sc.NatsConn()

if nc.Status() != nats.CONNECTED {
t.Fatal("Should have status set to CONNECTED")
}
nc.Close()
if nc.Status() != nats.CLOSED {
t.Fatal("Should have status set to CLOSED")
}

sc.Close()
if sc.NatsConn() != nil {
t.Fatal("Wrapped conn should be nil after close")
}

// Make sure we can get the Conn we provide.
nc = natstest.NewDefaultConnection(t)
sc, err := Connect(clusterName, clientName, NatsConn(nc))
if err != nil {
stackFatalf(t, "Expected to connect correctly, got err %v", err)
}
defer sc.Close()
if sc.NatsConn() != nc {
t.Fatal("Unexpected wrapped conn")
}
}

0 comments on commit c4cc894

Please sign in to comment.