Skip to content

Commit

Permalink
Merge branch 'review-base'
Browse files Browse the repository at this point in the history
  • Loading branch information
mfreeman451 committed Oct 2, 2024
2 parents 1daf981 + cf3c3d7 commit 7b8b313
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions wrapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package nats

import (
"fmt"
"net"

Check failure on line 5 in wrapper_test.go

View workflow job for this annotation

GitHub Actions / lint

"net" imported and not used (typecheck)

Check failure on line 5 in wrapper_test.go

View workflow job for this annotation

GitHub Actions / lint

"net" imported and not used (typecheck)
"testing"
"time"

"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
)

func TestNATSConnWrapper(t *testing.T) {
// Start an embedded NATS server
opts := &server.Options{
Host: "127.0.0.1",
Port: -1, // Random available port
}

ns, err := server.NewServer(opts)
if err != nil {
t.Fatalf("Error starting NATS server: %v", err)
}

go ns.Start()
defer ns.Shutdown()

if !ns.ReadyForConnections(10 * time.Second) {
t.Fatal("NATS server not ready for connections")
}

// Get the server's listen address
addr := ns.Addr().(*net.TCPAddr)
url := fmt.Sprintf("nats://%s:%d", addr.IP.String(), addr.Port)

t.Run("Status", func(t *testing.T) {
nc, err := nats.Connect(url)
if err != nil {
t.Fatal(err)
}
defer nc.Close()

wrapper := &natsConnWrapper{Conn: nc}
status := wrapper.Status()
expectedStatus := nats.CONNECTED

if status != expectedStatus {
t.Errorf("Expected status %v, got %v", expectedStatus, status)
}
})

t.Run("Close", func(t *testing.T) {
nc, err := nats.Connect(url)
if err != nil {
t.Fatal(err)
}

wrapper := &natsConnWrapper{Conn: nc}
wrapper.Close()

status := wrapper.Status()
expectedStatus := nats.CLOSED

if status != expectedStatus {
t.Errorf("Expected status %v, got %v", expectedStatus, status)
}
})
}

0 comments on commit 7b8b313

Please sign in to comment.