-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_manager_test.go
234 lines (179 loc) · 5.75 KB
/
connection_manager_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package nats
import (
"context"
"testing"
"time"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"gofr.dev/pkg/gofr/datasource"
"gofr.dev/pkg/gofr/logging"
)
func TestNewConnectionManager(t *testing.T) {
cfg := &Config{Server: "nats://localhost:4222"}
logger := logging.NewMockLogger(logging.DEBUG)
natsConnector := &MockNATSConnector{}
jsCreator := &MockJetStreamCreator{}
cm := NewConnectionManager(cfg, logger, natsConnector, jsCreator)
assert.NotNil(t, cm)
assert.Equal(t, cfg, cm.config)
assert.Equal(t, logger, cm.logger)
assert.Equal(t, natsConnector, cm.natsConnector)
assert.Equal(t, jsCreator, cm.jetStreamCreator)
}
func TestConnectionManager_Connect(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := context.Background()
mockConn := NewMockConnInterface(ctrl)
mockJS := NewMockJetStream(ctrl)
mockNATSConnector := NewMockNATSConnector(ctrl)
mockJSCreator := NewMockJetStreamCreator(ctrl)
cm := NewConnectionManager(
&Config{Server: "nats://localhost:4222"},
logging.NewMockLogger(logging.DEBUG),
mockNATSConnector,
mockJSCreator,
)
mockNATSConnector.EXPECT().
Connect(gomock.Any(), gomock.Any()).
Return(mockConn, nil)
// We don't need to expect NATSConn() call anymore, as we're passing mockConn directly to New()
mockJSCreator.EXPECT().
New(mockConn).
Return(mockJS, nil)
err := cm.Connect(ctx)
require.NoError(t, err)
assert.Equal(t, mockConn, cm.conn)
assert.Equal(t, mockJS, cm.jetStream)
}
func TestConnectionManager_Close(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockConn := NewMockConnInterface(ctrl)
cm := &ConnectionManager{
conn: mockConn,
}
mockConn.EXPECT().Close()
ctx := context.Background()
cm.Close(ctx)
}
func TestConnectionManager_Publish(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockJS := NewMockJetStream(ctrl)
mockMetrics := NewMockMetrics(ctrl)
cm := &ConnectionManager{
jetStream: mockJS,
logger: logging.NewMockLogger(logging.DEBUG),
}
ctx := context.Background()
subject := "test.subject"
message := []byte("test message")
mockMetrics.EXPECT().IncrementCounter(ctx, "app_pubsub_publish_total_count", "subject", subject)
mockJS.EXPECT().Publish(ctx, subject, message).Return(&jetstream.PubAck{}, nil)
mockMetrics.EXPECT().IncrementCounter(ctx, "app_pubsub_publish_success_count", "subject", subject)
err := cm.Publish(ctx, subject, message, mockMetrics)
require.NoError(t, err)
}
func TestConnectionManager_validateJetStream(t *testing.T) {
cm := &ConnectionManager{
jetStream: NewMockJetStream(gomock.NewController(t)),
logger: logging.NewMockLogger(logging.DEBUG),
}
err := cm.validateJetStream("test.subject")
require.NoError(t, err)
cm.jetStream = nil
err = cm.validateJetStream("test.subject")
assert.Equal(t, errJetStreamNotConfigured, err)
cm.jetStream = NewMockJetStream(gomock.NewController(t))
err = cm.validateJetStream("")
assert.Equal(t, errJetStreamNotConfigured, err)
}
func TestConnectionManager_Health(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockConn := NewMockConnInterface(ctrl)
cm := &ConnectionManager{
conn: mockConn,
config: &Config{
Server: "nats://localhost:4222",
},
}
mockConn.EXPECT().Status().Return(nats.CONNECTED)
health := cm.Health()
assert.Equal(t, datasource.StatusUp, health.Status)
assert.Equal(t, "nats://localhost:4222", health.Details["server"])
mockConn.EXPECT().Status().Return(nats.CLOSED)
health = cm.Health()
assert.Equal(t, datasource.StatusDown, health.Status)
assert.Equal(t, "nats://localhost:4222", health.Details["server"])
cm.conn = nil
health = cm.Health()
assert.Equal(t, datasource.StatusDown, health.Status)
}
func TestConnectionManager_JetStream(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockJS := NewMockJetStream(ctrl)
cm := &ConnectionManager{
jetStream: mockJS,
}
js, err := cm.JetStream()
require.NoError(t, err)
assert.Equal(t, mockJS, js)
}
func TestConnectionManager_JetStream_Nil(t *testing.T) {
cm := &ConnectionManager{
jetStream: nil,
}
js, err := cm.JetStream()
require.Error(t, err)
assert.Nil(t, js)
assert.EqualError(t, err, "JetStream is not configured")
}
func TestNatsConnWrapper_Status(t *testing.T) {
mockConn := &nats.Conn{}
wrapper := &natsConnWrapper{mockConn}
assert.Equal(t, mockConn.Status(), wrapper.Status())
}
func TestNatsConnWrapper_Close(t *testing.T) {
// Start a NATS server
ns, url := startNATSServer(t)
defer ns.Shutdown()
// Create a real NATS connection
nc, err := nats.Connect(url)
require.NoError(t, err, "Failed to connect to NATS")
// Create the wrapper with the real connection
wrapper := &natsConnWrapper{conn: nc}
// Check initial status
assert.Equal(t, nats.CONNECTED, wrapper.Status(), "Initial status should be CONNECTED")
// Close the connection
wrapper.Close()
// Check final status
assert.Equal(t, nats.CLOSED, wrapper.Status(), "Final status should be CLOSED")
}
// startNATSServer starts a NATS server and returns the server instance and the client URL.
func startNATSServer(t *testing.T) (s *server.Server, u string) {
t.Helper()
opts := &server.Options{
Host: "127.0.0.1",
Port: -1, // Random available port
}
ns, err := server.NewServer(opts)
require.NoError(t, err, "Failed to create NATS server")
go ns.Start()
if !ns.ReadyForConnections(10 * time.Second) {
t.Fatal("NATS server not ready for connections")
}
u = ns.ClientURL()
return ns, u
}
func TestNatsConnWrapper_NatsConn(t *testing.T) {
mockConn := &nats.Conn{}
wrapper := &natsConnWrapper{mockConn}
assert.Equal(t, mockConn, wrapper.NATSConn())
}