-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_projectors_test.go
190 lines (160 loc) · 4.94 KB
/
stream_projectors_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
package po
import (
"context"
"fmt"
"testing"
"github.com/go-po/po/internal/record"
"github.com/go-po/po/streams"
"github.com/stretchr/testify/assert"
)
func TestOptimisticLockingStream_Project(t *testing.T) {
ctx := context.Background()
streamId := streams.ParseId("teststream-1")
// setup
newTestFixture := func(messageCount int64) projector {
store := &stubAppenderStore{messageCount: 0}
appender := newAppenderFunc(store, stubNotifier{}, testRegistry)
var i int64
for i = 0; i < messageCount; i++ {
_, err := appender(ctx, streamId, i, Msg{Name: fmt.Sprintf("Message %d", i)})
assert.NoError(t, err)
}
return newProjectorFunc(store, testRegistry)
}
t.Run("empty stream", func(t *testing.T) {
// setup
sut := newTestFixture(0)
count := 0
// execute
pos, err := sut.Project(ctx, streamId, -1, HandlerFunc(func(ctx context.Context, msg streams.Message) error {
count = count + 1
return nil
}))
// verify
assert.NoError(t, err)
assert.Equal(t, 0, count)
assert.Equal(t, -1, int(pos))
})
t.Run("with data", func(t *testing.T) {
// setup
sut := newTestFixture(10)
count := 0
// execute
pos, err := sut.Project(ctx, streamId, -1, HandlerFunc(func(ctx context.Context, msg streams.Message) error {
count = count + 1
return nil
}))
// verify
assert.NoError(t, err)
assert.Equal(t, 10, count, "messages received")
assert.Equal(t, 10, int(pos))
})
}
func newProjectionHandler(handler HandlerFunc) *stubProjectionHandler {
return &stubProjectionHandler{
handler: handler,
}
}
type stubProjectionHandler struct {
handler Handler
}
func (stub *stubProjectionHandler) SnapshotName() string {
return "stub-snapshot"
}
func (stub *stubProjectionHandler) Handle(ctx context.Context, msg streams.Message) error {
return stub.handler.Handle(ctx, msg)
}
var _ snapshotStore = &stubSnapshotStore{}
type stubSnapshotStore struct {
reads int
writes int
snapshot record.Snapshot
}
func (stub *stubSnapshotStore) ReadSnapshot(ctx context.Context, id streams.Id, snapshotId string) (record.Snapshot, error) {
stub.reads = stub.reads + 1
return stub.snapshot, nil
}
func (stub *stubSnapshotStore) UpdateSnapshot(ctx context.Context, id streams.Id, snapshotId string, snapshot record.Snapshot) error {
stub.writes = stub.writes + 1
stub.snapshot = snapshot
return nil
}
func TestOptimisticLockingStream_Project_Snapshot(t *testing.T) {
// setup
ctx := context.Background()
streamId := streams.ParseId("snapshot-1")
newTestFixture := func(snapshotPosition int64, inner projector) (projector, *stubSnapshotStore) {
store := &stubSnapshotStore{snapshot: record.Snapshot{
Data: []byte(`{}`),
Position: snapshotPosition,
ContentType: "application/json",
}}
return newSnapshots(store, inner), store
}
t.Run("empty", func(t *testing.T) {
// setup
inner := projectorFunc(func(ctx context.Context, id streams.Id, lockPosition int64, projection Handler) (int64, error) {
return -1, nil
})
sut, _ := newTestFixture(0, inner)
// execute
pos, err := sut.Project(ctx, streamId, -1, newProjectionHandler(func(ctx context.Context, msg streams.Message) error {
return nil
}))
// verify
assert.NoError(t, err)
assert.Equal(t, -1, int(pos))
})
t.Run("inner progress", func(t *testing.T) {
// setup
inner := projectorFunc(func(ctx context.Context, id streams.Id, lockPosition int64, projection Handler) (int64, error) {
return 10, nil
})
sut, store := newTestFixture(0, inner)
// execute
pos, err := sut.Project(ctx, streamId, -1, newProjectionHandler(func(ctx context.Context, msg streams.Message) error {
return nil
}))
// verify
assert.NoError(t, err)
assert.Equal(t, 10, int(pos))
assert.Equal(t, 10, int(store.snapshot.Position))
assert.Equal(t, 1, store.reads)
assert.Equal(t, 1, store.writes)
})
t.Run("halfway projected", func(t *testing.T) {
// setup
var gotLockPosition int64 = -10
inner := projectorFunc(func(ctx context.Context, id streams.Id, lockPosition int64, projection Handler) (int64, error) {
gotLockPosition = lockPosition
return 10, nil
})
sut, store := newTestFixture(4, inner)
// execute
pos, err := sut.Project(ctx, streamId, -1, newProjectionHandler(func(ctx context.Context, msg streams.Message) error {
return nil
}))
// verify
assert.NoError(t, err)
assert.Equal(t, 4, int(gotLockPosition))
assert.Equal(t, 10, int(pos))
assert.Equal(t, 10, int(store.snapshot.Position))
})
t.Run("not projector", func(t *testing.T) {
// setup
inner := projectorFunc(func(ctx context.Context, id streams.Id, lockPosition int64, projection Handler) (int64, error) {
return 10, nil
})
sut, store := newTestFixture(0, inner)
count := 0
// execute
_, err := sut.Project(ctx, streamId, -1, HandlerFunc(func(ctx context.Context, msg streams.Message) error {
count = count + 1
return nil
}))
// verify
assert.NoError(t, err)
assert.Equal(t, 0, store.reads)
assert.Equal(t, 0, store.writes)
})
}