-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconsumer_test.go
68 lines (53 loc) · 1.48 KB
/
consumer_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
package main
import (
"context"
"testing"
gomock "github.com/golang/mock/gomock"
assert "github.com/stretchr/testify/require"
)
func TestConsumerNext(t *testing.T) {
t.Run("next, ack, next", func(t *testing.T) {
assert := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
var (
topic = "test_topic"
msg1 = newValue([]byte("message1"))
msg2 = newValue([]byte("message2"))
)
mockStore := NewMockstorer(ctrl)
mockStore.EXPECT().GetNext(topic).Return(msg1, 0, nil)
mockStore.EXPECT().Ack(topic, 0).Return(nil)
mockStore.EXPECT().GetNext(topic).Return(msg2, 1, nil)
b := newBroker(mockStore)
c := b.Subscribe(topic)
msg, err := c.Next(context.Background())
assert.NoError(err)
assert.Equal(msg1, msg)
assert.Equal(c.ackOffset, 0)
assert.NoError(c.Ack())
msg, err = c.Next(context.Background())
assert.NoError(err)
assert.Equal(msg2, msg)
assert.Equal(c.ackOffset, 1)
})
t.Run("next next, fails due to outstanding ack", func(t *testing.T) {
assert := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
var (
topic = "test_topic"
msg1 = newValue([]byte("message1"))
)
mockStore := NewMockstorer(ctrl)
mockStore.EXPECT().GetNext(topic).Return(msg1, 0, nil)
b := newBroker(mockStore)
c := b.Subscribe(topic)
msg, err := c.Next(context.Background())
assert.NoError(err)
assert.Equal(msg1, msg)
assert.Equal(c.ackOffset, 0)
msg, err = c.Next(context.Background())
assert.Error(err)
})
}