-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / lint
|
||
"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) | ||
} | ||
}) | ||
} |