-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathoutboxer_test.go
200 lines (166 loc) · 4.54 KB
/
outboxer_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
package outboxer_test
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/italolelis/outboxer"
amqpOut "github.com/italolelis/outboxer/es/amqp"
)
type inMemDS struct {
data []*outboxer.OutboxMessage
}
func (inmem *inMemDS) GetEvents(ctx context.Context, batchSize int32) ([]*outboxer.OutboxMessage, error) {
return inmem.data, nil
}
func (inmem *inMemDS) SetAsDispatched(ctx context.Context, id int64) error {
for _, m := range inmem.data {
if m.ID == id {
m.Dispatched = true
m.DispatchedAt.Time = time.Now()
return nil
}
}
return fmt.Errorf("message not found")
}
func (inmem *inMemDS) Add(ctx context.Context, m *outboxer.OutboxMessage) error {
inmem.data = append(inmem.data, m)
return nil
}
func (inmem *inMemDS) AddWithinTx(ctx context.Context, m *outboxer.OutboxMessage, fn func(outboxer.ExecerContext) error) error {
return inmem.Add(ctx, m)
}
func (inmem *inMemDS) Remove(ctx context.Context, cleanUpBefore time.Time, batchSize int32) error {
for i, m := range inmem.data {
if m.DispatchedAt.Time == cleanUpBefore {
inmem.data = append(inmem.data[:i], inmem.data[i+1:]...)
return nil
}
}
return errors.New("event not found")
}
type inMemES struct {
ok bool
}
// Send mocks the behavior of the event store
func (inmem *inMemES) Send(context.Context, *outboxer.OutboxMessage) error {
if inmem.ok {
return nil
}
return errors.New("mock returned an error")
}
func TestOutboxer_Send(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
o, err := outboxer.New(
outboxer.WithDataStore(&inMemDS{}),
outboxer.WithEventStream(&inMemES{true}),
outboxer.WithCheckInterval(1*time.Second),
outboxer.WithCleanupInterval(5*time.Second),
outboxer.WithCleanUpBefore(time.Now().AddDate(0, 0, -5)),
outboxer.WithCleanUpBatchSize(10),
outboxer.WithMessageBatchSize(10),
)
if err != nil {
t.Fatalf("could not create an outboxer instance: %s", err)
}
fmt.Println("started to listen for new messages")
o.Start(ctx)
defer o.Stop()
done := make(chan struct{})
go func(ob *outboxer.Outboxer) {
for {
select {
case err := <-ob.ErrChan():
t.Errorf("could not send message: %s", err)
return
case <-ob.OkChan():
fmt.Println("message received")
done <- struct{}{}
return
case <-ctx.Done():
return
}
}
}(o)
fmt.Println("sending message...")
if err = o.Send(ctx, &outboxer.OutboxMessage{
Payload: []byte("test payload"),
Options: map[string]interface{}{
amqpOut.ExchangeNameOption: "test",
amqpOut.ExchangeTypeOption: "topic",
amqpOut.RoutingKeyOption: "test.send",
},
}); err != nil {
t.Fatalf("could not send message: %s", err)
}
fmt.Println("waiting for successfully sent messages...")
<-done
}
func TestOutboxer_SendWithinTx(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
o, err := outboxer.New(
outboxer.WithDataStore(&inMemDS{}),
outboxer.WithEventStream(&inMemES{true}),
outboxer.WithCheckInterval(1*time.Second),
outboxer.WithCleanupInterval(5*time.Second),
outboxer.WithCleanUpBefore(time.Now().AddDate(0, 0, -5)),
outboxer.WithCleanUpBatchSize(10),
outboxer.WithMessageBatchSize(10),
)
if err != nil {
t.Fatalf("could not create an outboxer instance: %s", err)
}
fmt.Println("started to listen for new messages")
o.Start(ctx)
defer o.Stop()
done := make(chan struct{})
go func(ob *outboxer.Outboxer) {
for {
select {
case err := <-ob.ErrChan():
t.Errorf("could not send message: %s", err)
return
case <-ob.OkChan():
fmt.Println("message received")
done <- struct{}{}
return
case <-ctx.Done():
return
}
}
}(o)
if err = o.SendWithinTx(ctx, &outboxer.OutboxMessage{
Payload: []byte("test payload"),
Options: map[string]interface{}{
amqpOut.ExchangeNameOption: "test",
amqpOut.ExchangeTypeOption: "topic",
amqpOut.RoutingKeyOption: "test.send",
},
}, func(execer outboxer.ExecerContext) error {
if _, err := execer.ExecContext(ctx, "SELECT * FROM orders LIMIT 1"); err != nil {
return err
}
return nil
}); err != nil {
t.Fatalf("could not send message within transaction: %s", err)
}
fmt.Println("waiting for successfully sent messages...")
<-done
}
func TestOutboxer_WithWrongParams(t *testing.T) {
_, err := outboxer.New(
outboxer.WithEventStream(&inMemES{true}),
)
if err == nil {
t.Fatalf("this should return an error ErrMissingDataStore")
}
_, err = outboxer.New(
outboxer.WithDataStore(&inMemDS{}),
)
if err == nil {
t.Fatalf("this should return an error ErrMissingEventStream")
}
}