-
Notifications
You must be signed in to change notification settings - Fork 9
/
record_iterator_test.go
152 lines (125 loc) · 3.71 KB
/
record_iterator_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
package rangedb_test
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/inklabs/rangedb"
"github.com/inklabs/rangedb/rangedbtest"
)
func TestRecordIterator(t *testing.T) {
event := &rangedbtest.ThingWasDone{ID: "A", Number: 1}
record := &rangedb.Record{
StreamName: "thing-" + event.AggregateID(),
AggregateType: event.AggregateType(),
AggregateID: event.AggregateID(),
GlobalSequenceNumber: 1,
StreamSequenceNumber: 1,
EventType: event.EventType(),
InsertTimestamp: 0,
Data: event,
Metadata: nil,
}
t.Run("only 1 record in the stream", func(t *testing.T) {
t.Run("first call to next", func(t *testing.T) {
// Given
iter := stubRecordIterator(rangedb.ResultRecord{Record: record, Err: nil})
// When
canContinue := iter.Next()
// Then
assert.True(t, canContinue)
assert.Equal(t, record, iter.Record())
assert.Nil(t, iter.Err())
})
t.Run("second call to Next", func(t *testing.T) {
// Given
iter := stubRecordIterator(rangedb.ResultRecord{Record: record, Err: nil})
iter.Next()
// When
canContinue := iter.Next()
// Then
assert.False(t, canContinue)
assert.Nil(t, iter.Record())
assert.Nil(t, iter.Err())
})
t.Run("first call to next context", func(t *testing.T) {
// Given
iter := stubRecordIterator(rangedb.ResultRecord{Record: record, Err: nil})
ctx := rangedbtest.TimeoutContext(t)
// When
canContinue := iter.NextContext(ctx)
// Then
assert.True(t, canContinue)
assert.Equal(t, record, iter.Record())
assert.Nil(t, iter.Err())
})
t.Run("second call to next context", func(t *testing.T) {
// Given
iter := stubRecordIterator(rangedb.ResultRecord{Record: record, Err: nil})
ctx := rangedbtest.TimeoutContext(t)
iter.NextContext(ctx)
// When
canContinue := iter.NextContext(ctx)
// Then
assert.False(t, canContinue)
assert.Nil(t, iter.Record())
assert.Nil(t, iter.Err())
})
t.Run("timeout from closed context", func(t *testing.T) {
// Given
iter := blockingRecordIterator(t)
canceledCtx, done := context.WithCancel(context.Background())
done()
// When
canContinue := iter.NextContext(canceledCtx)
// Then
assert.False(t, canContinue)
assert.Nil(t, iter.Record())
assert.Equal(t, context.Canceled, iter.Err())
})
})
t.Run("only 1 error in stream", func(t *testing.T) {
t.Run("first call to Next", func(t *testing.T) {
// Given
iter := stubRecordIterator(rangedb.ResultRecord{
Record: nil,
Err: fmt.Errorf("first error"),
})
// When
canContinue := iter.Next()
// Then
assert.False(t, canContinue)
assert.EqualError(t, iter.Err(), "first error")
assert.Nil(t, iter.Record())
})
t.Run("second call to Next retains error for use outside of a for loop", func(t *testing.T) {
// Given
iter := stubRecordIterator(rangedb.ResultRecord{
Record: nil,
Err: fmt.Errorf("first error"),
})
iter.Next()
// When
canContinue := iter.Next()
// Then
assert.False(t, canContinue)
assert.EqualError(t, iter.Err(), "first error")
assert.Nil(t, iter.Record())
})
})
}
func stubRecordIterator(resultRecords ...rangedb.ResultRecord) rangedb.RecordIterator {
resultRecordChan := make(chan rangedb.ResultRecord, len(resultRecords))
for _, resultRecord := range resultRecords {
resultRecordChan <- resultRecord
}
close(resultRecordChan)
return rangedb.NewRecordIterator(resultRecordChan)
}
func blockingRecordIterator(t *testing.T) rangedb.RecordIterator {
resultRecordChan := make(chan rangedb.ResultRecord, 1)
t.Cleanup(func() {
close(resultRecordChan)
})
return rangedb.NewRecordIterator(resultRecordChan)
}