|
| 1 | +package nats |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/meshery/meshkit/broker" |
| 9 | + natsio "github.com/nats-io/nats.go" |
| 10 | +) |
| 11 | + |
| 12 | +type mockNatsConn struct { |
| 13 | + servers []string |
| 14 | + name string |
| 15 | + publishErr error |
| 16 | + queueSubErr error |
| 17 | + closed bool |
| 18 | + drainErr error |
| 19 | + publishMessages [][]byte |
| 20 | +} |
| 21 | + |
| 22 | +func (m *mockNatsConn) Servers() []string { return m.servers } |
| 23 | +func (m *mockNatsConn) Publish(subject string, data []byte) error { |
| 24 | + m.publishMessages = append(m.publishMessages, data) |
| 25 | + return m.publishErr |
| 26 | +} |
| 27 | +func (m *mockNatsConn) QueueSubscribe(subject, queue string, cb func(msg *natsio.Msg)) (*natsio.Subscription, error) { |
| 28 | + if m.queueSubErr != nil { |
| 29 | + return nil, m.queueSubErr |
| 30 | + } |
| 31 | + // Simulate a message |
| 32 | + msg := &natsio.Msg{Data: []byte(`{"ObjectType":"request-payload","EventType":"ADDED"}`)} |
| 33 | + cb(msg) |
| 34 | + return nil, nil |
| 35 | +} |
| 36 | +func (m *mockNatsConn) Drain() error { return m.drainErr } |
| 37 | +func (m *mockNatsConn) Close() { m.closed = true } |
| 38 | +func (m *mockNatsConn) Opts() natsio.Options { return natsio.Options{Name: m.name} } |
| 39 | + |
| 40 | +// Ensure mockNatsConn implements NatsConn |
| 41 | +var _ NatsConn = (*mockNatsConn)(nil) |
| 42 | + |
| 43 | +func newTestNats(mock NatsConn) *Nats { |
| 44 | + return &Nats{conn: mock, wg: &sync.WaitGroup{}} |
| 45 | +} |
| 46 | + |
| 47 | +func TestConnectedEndpoints(t *testing.T) { |
| 48 | + mock := &mockNatsConn{servers: []string{"nats://localhost:4222", "nats://other:4222"}} |
| 49 | + n := newTestNats(mock) |
| 50 | + endpoints := n.ConnectedEndpoints() |
| 51 | + if len(endpoints) != 2 || endpoints[0] != "localhost:4222" { |
| 52 | + t.Errorf("unexpected endpoints: %v", endpoints) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestInfo(t *testing.T) { |
| 57 | + n := &Nats{conn: nil} |
| 58 | + if n.Info() != broker.NotConnected { |
| 59 | + t.Error("expected NotConnected when conn is nil") |
| 60 | + } |
| 61 | + mock := &mockNatsConn{name: "test-conn"} |
| 62 | + n = newTestNats(mock) |
| 63 | + if n.Info() != "test-conn" { |
| 64 | + t.Errorf("expected 'test-conn', got %s", n.Info()) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestCloseConnection(t *testing.T) { |
| 69 | + mock := &mockNatsConn{} |
| 70 | + n := newTestNats(mock) |
| 71 | + n.CloseConnection() |
| 72 | + if !mock.closed { |
| 73 | + t.Error("expected connection to be closed") |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestPublish(t *testing.T) { |
| 78 | + mock := &mockNatsConn{} |
| 79 | + n := newTestNats(mock) |
| 80 | + msg := &broker.Message{ObjectType: broker.Request, EventType: broker.Add} |
| 81 | + err := n.Publish("subject", msg) |
| 82 | + if err != nil { |
| 83 | + t.Errorf("unexpected error: %v", err) |
| 84 | + } |
| 85 | + // Test error on marshal |
| 86 | + err = n.Publish("subject", nil) |
| 87 | + if err == nil { |
| 88 | + t.Error("expected error for nil message") |
| 89 | + } |
| 90 | + // Test error on publish |
| 91 | + mock.publishErr = errors.New("publish error") |
| 92 | + msg = &broker.Message{ObjectType: broker.Request, EventType: broker.Add} |
| 93 | + err = n.Publish("subject", msg) |
| 94 | + if err == nil { |
| 95 | + t.Error("expected error from publish") |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestPublishWithChannel(t *testing.T) { |
| 100 | + mock := &mockNatsConn{} |
| 101 | + n := newTestNats(mock) |
| 102 | + ch := make(chan *broker.Message, 1) |
| 103 | + ch <- &broker.Message{ObjectType: broker.Request, EventType: broker.Add} |
| 104 | + close(ch) |
| 105 | + err := n.PublishWithChannel("subject", ch) |
| 106 | + if err != nil { |
| 107 | + t.Errorf("unexpected error: %v", err) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestSubscribe(t *testing.T) { |
| 112 | + mock := &mockNatsConn{} |
| 113 | + n := newTestNats(mock) |
| 114 | + n.wg = &sync.WaitGroup{} // ensure wg is set |
| 115 | + var msg []byte |
| 116 | + err := n.Subscribe("subject", "queue", msg) |
| 117 | + if err != nil { |
| 118 | + t.Errorf("unexpected error: %v", err) |
| 119 | + } |
| 120 | + // Test error on subscribe |
| 121 | + mock.queueSubErr = errors.New("subscribe error") |
| 122 | + err = n.Subscribe("subject", "queue", msg) |
| 123 | + if err == nil { |
| 124 | + t.Error("expected error from subscribe") |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestSubscribeWithChannel(t *testing.T) { |
| 129 | + mock := &mockNatsConn{} |
| 130 | + n := newTestNats(mock) |
| 131 | + ch := make(chan *broker.Message, 1) |
| 132 | + err := n.SubscribeWithChannel("subject", "queue", ch) |
| 133 | + if err != nil { |
| 134 | + t.Errorf("unexpected error: %v", err) |
| 135 | + } |
| 136 | + // Test error on subscribe |
| 137 | + mock.queueSubErr = errors.New("subscribe error") |
| 138 | + err = n.SubscribeWithChannel("subject", "queue", ch) |
| 139 | + if err == nil { |
| 140 | + t.Error("expected error from subscribe") |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func TestDeepCopy(t *testing.T) { |
| 145 | + n := &Nats{conn: nil, wg: &sync.WaitGroup{}} |
| 146 | + copy := n.DeepCopy() |
| 147 | + if copy == nil || copy == n { |
| 148 | + t.Error("DeepCopy did not create a new instance") |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func TestDeepCopyObject(t *testing.T) { |
| 153 | + n := &Nats{conn: nil, wg: &sync.WaitGroup{}} |
| 154 | + obj := n.DeepCopyObject() |
| 155 | + if obj == nil { |
| 156 | + t.Error("DeepCopyObject returned nil") |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func TestIsEmpty(t *testing.T) { |
| 161 | + var n *Nats |
| 162 | + if !n.IsEmpty() { |
| 163 | + t.Error("expected true for nil receiver") |
| 164 | + } |
| 165 | + n = &Nats{} |
| 166 | + if !n.IsEmpty() { |
| 167 | + t.Error("expected true for empty Nats struct") |
| 168 | + } |
| 169 | + mock := &mockNatsConn{} |
| 170 | + n = &Nats{conn: mock} |
| 171 | + if n.IsEmpty() { |
| 172 | + t.Error("expected false for non-empty Nats struct") |
| 173 | + } |
| 174 | +} |
0 commit comments