-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
requeue_test.go
214 lines (185 loc) · 5 KB
/
requeue_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
package requeue_test
import (
"context"
"fmt"
"io/ioutil"
"os"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
natsserver "github.com/nats-io/nats-server/v2/test"
"github.com/nats-io/nats.go"
requeue "github.com/nickpoorman/nats-requeue"
"github.com/nickpoorman/nats-requeue/internal/republisher"
"github.com/nickpoorman/nats-requeue/protocol"
"github.com/stretchr/testify/assert"
"golang.org/x/sync/errgroup"
)
func cleanUp(t *testing.T, path string) {
t.Cleanup(func() {
err := os.RemoveAll(path)
if err != nil {
fmt.Println(err)
}
})
}
func setup(t *testing.T) string {
dir, err := ioutil.TempDir("", fmt.Sprintf("%s-*", t.Name()))
if err != nil {
t.Fatal(err)
}
cleanUp(t, dir)
return dir
}
func Test_RequeueEndToEnd(t *testing.T) {
done := true
s := natsserver.RunRandClientPortServer()
t.Cleanup(func() {
s.Shutdown()
})
// Create a directory just for this test.
dataDir := setup(t)
// To not collide with other tests.
subject := nats.NewInbox()
clientURL := s.ClientURL()
t.Logf("running nats on: %s", clientURL)
// NATS connect Options.
natsOpts := []nats.Option{
nats.Name(fmt.Sprintf("test_requeue_%s", t.Name())),
nats.MaxReconnects(10),
}
ctx, cancel := context.WithCancel(context.Background())
rc, err := requeue.Connect(
requeue.ConnectContext(ctx),
requeue.DataDir(dataDir),
requeue.NATSOptions(natsOpts),
requeue.NATSServers(clientURL),
requeue.NATSSubject(subject),
requeue.NATSQueueName(requeue.DefaultNatsQueueName),
requeue.RepublisherOptions(
republisher.RepublishInterval(1*time.Second),
// republisher.MaxInFlight(1),
),
)
if err != nil {
t.Fatalf("Error on requeue connect: %v", err)
}
t.Cleanup(func() {
rc.Close()
})
nc, err := nats.Connect(
clientURL,
nats.DisconnectErrHandler(func(con *nats.Conn, err error) {
if !done {
t.Errorf("nats-producer: DisconnectErrHandler: %s", err.Error())
}
}),
nats.ReconnectHandler(func(con *nats.Conn) {
t.Logf("nats-producer: Got reconnected to %s!", con.ConnectedUrl())
}),
nats.ClosedHandler(func(con *nats.Conn) {
t.Log("nats-producer: ClosedHandler")
}),
nats.ErrorHandler(func(con *nats.Conn, sub *nats.Subscription, err error) {
t.Errorf("nats-producer: ErrorHandler: Got err: conn=%s sub=%s err=%w", con.Opts.Name, sub.Subject, err)
}),
)
if err != nil {
t.Fatalf("Error on connect: %v", err)
}
t.Cleanup(func() {
nc.Close()
})
var eventsMu sync.Mutex
events := make(map[string]struct{})
total := 100
pending := int64(total)
group, _ := errgroup.WithContext(context.Background())
ch := make(chan int)
var republishedWG sync.WaitGroup
republishedWG.Add(total)
originalSubject := "foo.bar.baz"
_, err = nc.Subscribe(originalSubject, func(msg *nats.Msg) {
t.Logf("got replayed message: %s", string(msg.Data))
if err := msg.Respond(nil); err != nil {
t.Fatal(fmt.Errorf("failed to respond to message: %w", err))
}
eventsMu.Lock()
m := string(msg.Data)
if _, ok := events[m]; ok {
delete(events, m)
republishedWG.Done()
t.Logf("replay events remaining: %d", len(events))
}
eventsMu.Unlock()
})
assert.Nil(t, err)
go func() {
// Generate all our events
for i := 0; i < total; i++ {
ch <- i
}
close(ch)
}()
// Send some events for requeue to persist
for i := 0; i < runtime.NumCPU(); i++ { // Throttle in flight requests
group.Go(func(i int) func() error {
return func() error {
for i := range ch {
err := func(i int) error {
defer func() {
left := atomic.AddInt64(&pending, -1)
t.Logf("number left: %d", left)
}()
payload := buildPayload(i, originalSubject)
eventsMu.Lock()
events[string(payload.OriginalPayload)] = struct{}{}
eventsMu.Unlock()
msg, err := requeue.RetryRequest(nc, subject, payload.Bytes(), 15*time.Second, 100000)
if err != nil {
if nc.LastError() != nil {
return fmt.Errorf("last error for request: %w", nc.LastError())
}
return fmt.Errorf("for request: %w", err)
}
if len(msg.Data) > 0 {
// For performace, we should be able to send ACK with an empty
// payload.
return fmt.Errorf("Expected the ACK to be empty but got %s", string(msg.Data))
}
return nil
}(i)
if err != nil {
return err
}
}
return nil
}
}(i))
}
if err := group.Wait(); err != nil {
t.Fatal(err)
}
t.Logf("pending: %d", pending)
// Wait for all the messages to have been republished
republishedWG.Wait()
t.Log("got all messages")
done = true
t.Log("canceling requeue context")
cancel()
<-rc.HasBeenClosed()
t.Log("requeue closed")
}
func buildPayload(i int, originalSubject string) protocol.RequeueMessage {
msg := protocol.DefaultRequeueMessage()
msg.Retries = 1
msg.TTL = uint64(5 * 24 * time.Hour)
msg.Delay = uint64(1 * time.Nanosecond)
msg.BackoffStrategy = protocol.BackoffStrategy_Exponential
// msg.QueueName = "default"
msg.OriginalSubject = originalSubject
msg.OriginalPayload = []byte(fmt.Sprintf("my awesome payload %d", i))
return msg
}