forked from lovoo/goka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocks.go
129 lines (101 loc) · 2.74 KB
/
mocks.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
package goka
import (
"fmt"
"hash"
"log"
"github.com/Shopify/sarama"
)
// constHasher implements a hasher that will always return the specified
// partition. Doesn't properly implement the Hash32 interface, use only in
// tests.
type constHasher struct {
partition uint32
}
func (ch *constHasher) Sum(b []byte) []byte {
return nil
}
func (ch *constHasher) Sum32() uint32 {
return ch.partition
}
func (ch *constHasher) BlockSize() int {
return 0
}
func (ch *constHasher) Reset() {}
func (ch *constHasher) Size() int { return 4 }
func (ch *constHasher) Write(p []byte) (n int, err error) {
return len(p), nil
}
func NewConstHasher(part uint32) hash.Hash32 {
return &constHasher{partition: part}
}
type clientMock struct {
topics []string
partitions []int32
newestOffsets map[string]int64
oldestOffsets map[string]int64
}
func newClientMock() *clientMock {
return &clientMock{
newestOffsets: make(map[string]int64),
oldestOffsets: make(map[string]int64),
}
}
// newest offset is the highwatermark
func (cm *clientMock) setNewestOffset(topic string, partitionID int32, offset int64) {
cm.newestOffsets[fmt.Sprintf("%s/%d", topic, partitionID)] = offset
}
func (cm *clientMock) setOldestOffset(topic string, partitionID int32, offset int64) {
cm.oldestOffsets[fmt.Sprintf("%s/%d", topic, partitionID)] = offset
}
func (cm *clientMock) Config() *sarama.Config {
return nil
}
func (cm *clientMock) Topics() ([]string, error) {
return cm.topics, nil
}
func (cm *clientMock) Partitions(topic string) ([]int32, error) {
return cm.partitions, nil
}
func (cm *clientMock) WritablePartitions(topic string) ([]int32, error) {
return cm.partitions, nil
}
func (cm *clientMock) Leader(topic string, partitionID int32) (*sarama.Broker, error) {
return nil, nil
}
func (cm *clientMock) Replicas(topic string, partitionID int32) ([]int32, error) {
return nil, nil
}
func (cm *clientMock) RefreshMetadata(topics ...string) error {
return nil
}
func (cm *clientMock) GetOffset(topic string, partitionID int32, time int64) (int64, error) {
var offset int64
var hasOffset bool
switch time {
case sarama.OffsetNewest:
offset, hasOffset = cm.newestOffsets[fmt.Sprintf("%s/%d", topic, partitionID)]
case sarama.OffsetOldest:
offset, hasOffset = cm.oldestOffsets[fmt.Sprintf("%s/%d", topic, partitionID)]
default:
log.Panic("we don't mock this case")
}
if hasOffset {
return offset, nil
}
if time == sarama.OffsetNewest {
return 0, nil
}
return 0, nil
}
func (cm *clientMock) Coordinator(consumerGroup string) (*sarama.Broker, error) {
return nil, nil
}
func (cm *clientMock) RefreshCoordinator(consumerGroup string) error {
return nil
}
func (cm *clientMock) Close() error {
return nil
}
func (cm *clientMock) Closed() bool {
return false
}