-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommitter_test.go
86 lines (63 loc) · 1.86 KB
/
committer_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
package nats
import (
"testing"
"github.com/nats-io/nats.go/jetstream"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)
// createTestCommitter is a helper function for tests to create a natsCommitter.
func createTestCommitter(msg jetstream.Msg) *natsCommitter {
return &natsCommitter{msg: msg}
}
func TestNATSCommitter_Commit(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockMsg := NewMockMsg(ctrl)
committer := createTestCommitter(mockMsg)
t.Run("Successful Commit", func(_ *testing.T) {
mockMsg.EXPECT().Ack().Return(nil)
committer.Commit()
})
t.Run("Failed Commit with Successful Nak", func(_ *testing.T) {
mockMsg.EXPECT().Ack().Return(assert.AnError)
mockMsg.EXPECT().Nak().Return(nil)
committer.Commit()
})
t.Run("Failed Commit with Failed Nak", func(_ *testing.T) {
mockMsg.EXPECT().Ack().Return(assert.AnError)
mockMsg.EXPECT().Nak().Return(assert.AnError)
committer.Commit()
})
}
func TestNATSCommitter_Nak(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockMsg := NewMockMsg(ctrl)
committer := createTestCommitter(mockMsg)
t.Run("Successful Nak", func(t *testing.T) {
mockMsg.EXPECT().Nak().Return(nil)
err := committer.Nak()
assert.NoError(t, err)
})
t.Run("Failed Nak", func(t *testing.T) {
mockMsg.EXPECT().Nak().Return(assert.AnError)
err := committer.Nak()
assert.Error(t, err)
})
}
func TestNATSCommitter_Rollback(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockMsg := NewMockMsg(ctrl)
committer := createTestCommitter(mockMsg)
t.Run("Successful Rollback", func(t *testing.T) {
mockMsg.EXPECT().Nak().Return(nil)
err := committer.Rollback()
assert.NoError(t, err)
})
t.Run("Failed Rollback", func(t *testing.T) {
mockMsg.EXPECT().Nak().Return(assert.AnError)
err := committer.Rollback()
assert.Error(t, err)
})
}