-
Notifications
You must be signed in to change notification settings - Fork 173
/
mockbuilder.go
89 lines (75 loc) · 2.25 KB
/
mockbuilder.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
package goka
import (
"errors"
"fmt"
"hash"
"testing"
"github.com/golang/mock/gomock"
"github.com/lovoo/goka/storage"
)
var (
errProducerBuilder error = errors.New("building producer failed on purpose")
)
type builderMock struct {
ctrl *gomock.Controller
st storage.Storage
mst *MockStorage
tmgr *MockTopicManager
consumerGroup *MockConsumerGroup
producer *MockProducer
client *MockClient
admin *MockClusterAdmin
}
func newBuilderMock(ctrl *gomock.Controller) *builderMock {
return &builderMock{
ctrl: ctrl,
mst: NewMockStorage(ctrl),
tmgr: NewMockTopicManager(ctrl),
producer: NewMockProducer(ctrl),
client: NewMockClient(ctrl),
admin: NewMockClusterAdmin(ctrl),
}
}
func (bm *builderMock) createProcessorOptions(consBuilder SaramaConsumerBuilder, groupBuilder ConsumerGroupBuilder) []ProcessorOption {
return []ProcessorOption{
WithStorageBuilder(bm.getStorageBuilder()),
WithTopicManagerBuilder(bm.getTopicManagerBuilder()),
WithProducerBuilder(bm.getProducerBuilder()),
WithConsumerGroupBuilder(groupBuilder),
WithConsumerSaramaBuilder(consBuilder),
}
}
func (bm *builderMock) getStorageBuilder() storage.Builder {
return func(topic string, partition int32) (storage.Storage, error) {
if bm.st != nil {
return bm.st, nil
}
return bm.mst, nil
}
}
func (bm *builderMock) getTopicManagerBuilder() TopicManagerBuilder {
return func([]string) (TopicManager, error) {
return bm.tmgr, nil
}
}
func (bm *builderMock) getProducerBuilder() ProducerBuilder {
return func(brokers []string, clientID string, hasher func() hash.Hash32) (Producer, error) {
return bm.producer, nil
}
}
func (bm *builderMock) getErrorProducerBuilder() ProducerBuilder {
return func(brokers []string, clientID string, hasher func() hash.Hash32) (Producer, error) {
return nil, errProducerBuilder
}
}
func (bm *builderMock) useMemoryStorage() {
bm.st = storage.NewMemory()
}
func errStorageBuilder() storage.Builder {
return func(topic string, partition int32) (storage.Storage, error) {
return nil, fmt.Errorf("error returned by errStorageBuilder")
}
}
func defaultSaramaAutoConsumerMock(t *testing.T) *MockAutoConsumer {
return NewMockAutoConsumer(t, DefaultConfig())
}