-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_test.go
148 lines (116 loc) · 3.35 KB
/
worker_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
package outbox_test
import (
"context"
"sync"
"testing"
"time"
"github.com/brianvoe/gofakeit/v6"
"github.com/stretchr/testify/require"
"github.com/tangelo-labs/go-outbox"
"github.com/tangelo-labs/go-outbox/provider/memory"
)
func TestWorker_Start(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
t.Run("GIVEN an event store with 3 unpublished events AND a worker instance with throughput=1, cleanupInterval=10ms, retention=1ms", func(t *testing.T) {
events := []outbox.Event{
{
ID: gofakeit.UUID(),
Type: gofakeit.AnimalType(),
Payload: []byte(gofakeit.Sentence(10)),
Metadata: map[string]string{
gofakeit.Color(): gofakeit.Color(),
},
},
{
ID: gofakeit.UUID(),
Type: gofakeit.AnimalType(),
Payload: []byte(gofakeit.Sentence(10)),
Metadata: map[string]string{
gofakeit.Color(): gofakeit.Color(),
},
},
{
ID: gofakeit.UUID(),
Type: gofakeit.AnimalType(),
Payload: []byte(gofakeit.Sentence(10)),
Metadata: map[string]string{
gofakeit.Color(): gofakeit.Color(),
},
},
}
store := &storeSpy{Store: memory.NewStore()}
sErr := store.SaveAllTx(ctx, nil, events...)
require.NoError(t, sErr)
w := outbox.NewWorker(
1*time.Second,
100*time.Millisecond,
10*time.Millisecond,
time.Millisecond,
1,
)
t.Run("WHEN the worker is started with a fake dispatcher", func(t *testing.T) {
dispatchedEvents := &queue{}
d := func(ctx context.Context, event outbox.Event) error {
dispatchedEvents.add(event)
return nil
}
require.NoError(t, w.Start(ctx, store, d))
t.Run("THEN eventually all events are dispatched AND store is invoked at least 3 times AND purge is invoked at least once AND event store is now empty", func(t *testing.T) {
require.Eventually(t, func() bool {
return len(dispatchedEvents.ids()) == len(events) && store.getDispatchPendingTxCalls() >= 3 && store.getPurgeCalls() > 0
}, 5*time.Second, 100*time.Millisecond)
require.Contains(t, dispatchedEvents.ids(), events[0].ID)
require.Contains(t, dispatchedEvents.ids(), events[1].ID)
require.Contains(t, dispatchedEvents.ids(), events[2].ID)
require.Zero(t, store.Size())
})
})
})
}
type queue struct {
events []outbox.Event
mu sync.Mutex
}
func (q *queue) add(event outbox.Event) {
q.mu.Lock()
defer q.mu.Unlock()
q.events = append(q.events, event)
}
func (q *queue) ids() []string {
q.mu.Lock()
defer q.mu.Unlock()
ids := make([]string, len(q.events))
for i, event := range q.events {
ids[i] = event.ID
}
return ids
}
type storeSpy struct {
*memory.Store
purgeCalls int
dispatchPendingTxCalls int
mu sync.Mutex
}
func (s *storeSpy) getDispatchPendingTxCalls() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.dispatchPendingTxCalls
}
func (s *storeSpy) getPurgeCalls() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.purgeCalls
}
func (s *storeSpy) DispatchPendingTx(ctx context.Context, batchSize uint16, fn outbox.DispatchFunc) error {
s.mu.Lock()
defer s.mu.Unlock()
s.dispatchPendingTxCalls++
return s.Store.DispatchPendingTx(ctx, batchSize, fn)
}
func (s *storeSpy) Purge(ctx context.Context, olderTan time.Duration) (int64, error) {
s.mu.Lock()
defer s.mu.Unlock()
s.purgeCalls++
return s.Store.Purge(ctx, olderTan)
}