-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnats_integration_test.go
More file actions
223 lines (178 loc) · 5.66 KB
/
nats_integration_test.go
File metadata and controls
223 lines (178 loc) · 5.66 KB
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
//go:build integration
// +build integration
package eventbus
import (
"context"
"fmt"
"os"
"os/exec"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// setupNATS starts a NATS server using Docker for integration testing
func setupNATS(t *testing.T) (cleanup func()) {
t.Helper()
// Check if Docker is available
if _, err := exec.LookPath("docker"); err != nil {
t.Skip("Docker not available, skipping NATS integration tests")
}
// Start NATS container
containerName := fmt.Sprintf("nats-test-%d", time.Now().Unix())
cmd := exec.Command("docker", "run", "-d", "--name", containerName, "-p", "4222:4222", "nats:2.10-alpine")
if err := cmd.Run(); err != nil {
t.Skipf("Failed to start NATS container: %v", err)
}
// Wait for NATS to be ready
time.Sleep(3 * time.Second)
// Return cleanup function
return func() {
exec.Command("docker", "stop", containerName).Run()
exec.Command("docker", "rm", containerName).Run()
}
}
// TestNatsIntegrationPubSub tests NATS pub/sub with a real server
func TestNatsIntegrationPubSub(t *testing.T) {
if os.Getenv("INTEGRATION_TESTS") != "true" {
t.Skip("Skipping integration test. Set INTEGRATION_TESTS=true to run")
}
cleanup := setupNATS(t)
defer cleanup()
config := map[string]interface{}{
"url": "nats://localhost:4222",
}
bus, err := NewNatsEventBus(config)
require.NoError(t, err, "Failed to create NATS event bus")
require.NotNil(t, bus)
defer bus.Stop(context.Background())
ctx := context.Background()
err = bus.Start(ctx)
require.NoError(t, err, "Failed to start NATS event bus")
// Create a channel to receive events
eventReceived := make(chan Event, 1)
// Subscribe to a topic
handler := func(ctx context.Context, event Event) error {
eventReceived <- event
return nil
}
sub, err := bus.Subscribe(ctx, "test.topic", handler)
require.NoError(t, err)
require.NotNil(t, sub)
defer bus.Unsubscribe(ctx, sub)
// Give subscription time to be established
time.Sleep(100 * time.Millisecond)
// Publish an event
testPayload := map[string]string{"message": "hello"}
event := newTestCloudEvent("test.topic", testPayload)
err = bus.Publish(ctx, event)
require.NoError(t, err)
// Wait for event to be received
select {
case receivedEvent := <-eventReceived:
assert.Equal(t, "test.topic", receivedEvent.Type())
assert.NotNil(t, receivedEvent.Data())
case <-time.After(2 * time.Second):
t.Fatal("Timeout waiting for event")
}
}
// TestNatsIntegrationWildcards tests NATS wildcard subscriptions with a real server
func TestNatsIntegrationWildcards(t *testing.T) {
if os.Getenv("INTEGRATION_TESTS") != "true" {
t.Skip("Skipping integration test. Set INTEGRATION_TESTS=true to run")
}
cleanup := setupNATS(t)
defer cleanup()
config := map[string]interface{}{
"url": "nats://localhost:4222",
}
bus, err := NewNatsEventBus(config)
require.NoError(t, err)
require.NotNil(t, bus)
defer bus.Stop(context.Background())
ctx := context.Background()
err = bus.Start(ctx)
require.NoError(t, err)
// Create a channel to receive events
eventsReceived := make(chan Event, 10)
// Subscribe to wildcard topic
handler := func(ctx context.Context, event Event) error {
eventsReceived <- event
return nil
}
sub, err := bus.Subscribe(ctx, "user.*", handler)
require.NoError(t, err)
require.NotNil(t, sub)
defer bus.Unsubscribe(ctx, sub)
// Give subscription time to be established
time.Sleep(100 * time.Millisecond)
// Publish multiple events
events := []string{"user.created", "user.updated", "user.deleted"}
for _, topic := range events {
event := newTestCloudEvent(topic, map[string]string{"topic": topic})
err = bus.Publish(ctx, event)
require.NoError(t, err)
}
// Wait for events to be received
receivedCount := 0
timeout := time.After(2 * time.Second)
for receivedCount < len(events) {
select {
case <-eventsReceived:
receivedCount++
case <-timeout:
t.Fatalf("Timeout waiting for events, received %d/%d", receivedCount, len(events))
}
}
assert.Equal(t, len(events), receivedCount)
}
// TestNatsIntegrationAsync tests NATS async subscriptions with a real server
func TestNatsIntegrationAsync(t *testing.T) {
if os.Getenv("INTEGRATION_TESTS") != "true" {
t.Skip("Skipping integration test. Set INTEGRATION_TESTS=true to run")
}
cleanup := setupNATS(t)
defer cleanup()
config := map[string]interface{}{
"url": "nats://localhost:4222",
}
bus, err := NewNatsEventBus(config)
require.NoError(t, err)
require.NotNil(t, bus)
defer bus.Stop(context.Background())
ctx := context.Background()
err = bus.Start(ctx)
require.NoError(t, err)
// Create a channel to receive events
eventReceived := make(chan Event, 1)
// Subscribe asynchronously
handler := func(ctx context.Context, event Event) error {
// Simulate some processing time
time.Sleep(50 * time.Millisecond)
eventReceived <- event
return nil
}
sub, err := bus.SubscribeAsync(ctx, "async.test", handler)
require.NoError(t, err)
require.NotNil(t, sub)
assert.True(t, sub.IsAsync())
defer bus.Unsubscribe(ctx, sub)
// Give subscription time to be established
time.Sleep(100 * time.Millisecond)
// Publish an event
event := newTestCloudEvent("async.test", map[string]string{"message": "async test"})
startTime := time.Now()
err = bus.Publish(ctx, event)
publishDuration := time.Since(startTime)
require.NoError(t, err)
// Publishing should not block for async subscriptions
// Allow some overhead but it should be fast
assert.Less(t, publishDuration, 100*time.Millisecond)
// Wait for event to be received
select {
case <-eventReceived:
// Event received successfully
case <-time.After(2 * time.Second):
t.Fatal("Timeout waiting for async event")
}
}