func NewJsonEncodedConn(tl test.TestLogger) *nats.EncodedConn { ec, err := nats.NewEncodedConn(test.NewDefaultConnection(tl), nats.JSON_ENCODER) if err != nil { tl.Fatalf("Failed to create an encoded connection: %v\n", err) } return ec }
func NewGobEncodedConn(t *testing.T) *nats.EncodedConn { ec, err := nats.NewEncodedConn(test.NewDefaultConnection(t), nats.GOB_ENCODER) if err != nil { t.Fatalf("Failed to create an encoded connection: %v\n", err) } return ec }
func NewProtoEncodedConn(tl test.TestLogger) *nats.EncodedConn { ec, err := nats.NewEncodedConn(test.NewDefaultConnection(tl), protobuf.PROTOBUF_ENCODER) if err != nil { tl.Fatalf("Failed to create an encoded connection: %v\n", err) } return ec }
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") } // Bail if we have a custom connection but not connected cnc := nats.Conn{Opts: nats.DefaultOptions} sc, err := Connect(clusterName, clientName, NatsConn(&cnc)) if err != ErrBadConnection { stackFatalf(t, "Expected to get an invalid connection error, got %v", err) } // Allow custom conn only if already connected opts := nats.DefaultOptions nc, err = opts.Connect() if err != nil { stackFatalf(t, "Expected to connect correctly, got err %v", err) } sc, err = Connect(clusterName, clientName, NatsConn(nc)) if err != nil { stackFatalf(t, "Expected to connect correctly, got err %v", err) } nc.Close() if nc.Status() != nats.CLOSED { t.Fatal("Should have status set to CLOSED") } // 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") } }
func TestConstructorErrs(t *testing.T) { s := test.RunDefaultServer() defer s.Shutdown() c := test.NewDefaultConnection(t) _, err := nats.NewEncodedConn(nil, "default") if err == nil { t.Fatal("Expected err for nil connection") } _, err = nats.NewEncodedConn(c, "foo22") if err == nil { t.Fatal("Expected err for bad encoder") } c.Close() _, err = nats.NewEncodedConn(c, "default") if err == nil { t.Fatal("Expected err for closed connection") } }