-
Notifications
You must be signed in to change notification settings - Fork 17
/
eventstore_test.go
90 lines (71 loc) · 2.27 KB
/
eventstore_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
//go:build unit
package goengine_test
import (
"errors"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/hellofresh/goengine/v2"
"github.com/hellofresh/goengine/v2/mocks"
)
func TestReadEventStream(t *testing.T) {
mockedMessages := make([]goengine.Message, 4)
for i := range mockedMessages {
mockedMessages[i] = mocks.NewDummyMessage(goengine.UUID{}, i, nil, time.Now())
}
t.Run("Get the message of the stream", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
stream := mocks.NewEventStream(ctrl)
streamIndex := -1
stream.EXPECT().Next().DoAndReturn(func() bool {
streamIndex++
return streamIndex < 4
}).Times(5)
stream.EXPECT().Message().DoAndReturn(func() (goengine.Message, int64, error) {
return mockedMessages[streamIndex], int64(streamIndex + 1), nil
}).Times(4)
stream.EXPECT().Err().Return(nil).MinTimes(1)
messages, numbers, err := goengine.ReadEventStream(stream)
assert.NoError(t, err)
assert.Equal(t, mockedMessages, messages)
assert.Equal(t, []int64{1, 2, 3, 4}, numbers)
})
t.Run("Error while iterating", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
expectedError := errors.New("I failed")
stream := mocks.NewEventStream(ctrl)
stream.EXPECT().Next().Return(false)
stream.EXPECT().Err().Return(expectedError)
messages, numbers, err := goengine.ReadEventStream(stream)
asserts := assert.New(t)
asserts.Equal(expectedError, err)
asserts.Empty(messages)
asserts.Empty(numbers)
})
t.Run("Error while fetching a Message", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
expectedError := errors.New("I failed")
stream := mocks.NewEventStream(ctrl)
streamIndex := -1
stream.EXPECT().Next().DoAndReturn(func() bool {
streamIndex++
return streamIndex < 4
}).Times(3)
stream.EXPECT().Message().DoAndReturn(func() (goengine.Message, int64, error) {
var err error
if streamIndex == 2 {
err = expectedError
}
return mockedMessages[streamIndex], int64(streamIndex + 1), err
}).Times(3)
messages, numbers, err := goengine.ReadEventStream(stream)
asserts := assert.New(t)
asserts.Equal(expectedError, err)
asserts.Empty(numbers)
asserts.Empty(messages)
})
}