-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper_test.go
83 lines (66 loc) · 1.62 KB
/
wrapper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package nats
import (
"fmt"
"net"
"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)
}
})
t.Run("NATSConn", func(t *testing.T) {
nc, err := nats.Connect(url)
if err != nil {
t.Fatal(err)
}
defer nc.Close()
wrapper := &natsConnWrapper{conn: nc}
returnedConn := wrapper.NATSConn()
if returnedConn != nc {
t.Errorf("Expected NATSConn to return the original connection")
}
})
}