-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectors_test.go
74 lines (57 loc) · 1.71 KB
/
connectors_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
package nats
import (
"testing"
"github.com/nats-io/nats.go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
func TestDefaultNATSConnector_Connect(t *testing.T) {
// Start a NATS server
ns, url := startNATSServer(t)
defer ns.Shutdown()
connector := &DefaultNATSConnector{}
// Test successful connection
conn, err := connector.Connect(url)
require.NoError(t, err)
assert.NotNil(t, conn)
assert.Implements(t, (*ConnInterface)(nil), conn)
// Close the connection
conn.Close()
// Test connection failure
_, err = connector.Connect("nats://invalid-url:4222")
assert.Error(t, err)
}
func TestDefaultJetStreamCreator_New(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
t.Run("Successful JetStream creation", func(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)
defer nc.Close()
// Wrap the real connection
wrapper := &natsConnWrapper{conn: nc}
creator := &DefaultJetStreamCreator{}
// Test successful JetStream creation
js, err := creator.New(wrapper)
require.NoError(t, err)
assert.NotNil(t, js)
})
t.Run("JetStream creation failure", func(t *testing.T) {
// Create a mock NATS connection
mockConn := NewMockConnInterface(ctrl)
// Mock the JetStream method to return an error
expectedError := errJetStreamCreationFailed
mockConn.EXPECT().JetStream().Return(nil, expectedError)
creator := &DefaultJetStreamCreator{}
// Test JetStream creation failure
js, err := creator.New(mockConn)
require.Error(t, err)
assert.Nil(t, js)
assert.Equal(t, expectedError, err)
})
}