forked from bsm/sarama-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partitions_test.go
137 lines (110 loc) · 4.03 KB
/
partitions_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
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
130
131
132
133
134
135
136
137
package cluster
import (
"github.com/Shopify/sarama"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("partitionConsumer", func() {
var subject *partitionConsumer
BeforeEach(func() {
var err error
subject, err = newPartitionConsumer(&mockConsumer{}, "topic", 0, offsetInfo{2000, "m3ta"}, sarama.OffsetOldest)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
close(subject.dead)
Expect(subject.Close()).NotTo(HaveOccurred())
})
It("should set state", func() {
Expect(subject.getState()).To(Equal(partitionState{
Info: offsetInfo{2000, "m3ta"},
}))
})
It("should recover from default offset if requested offset is out of bounds", func() {
pc, err := newPartitionConsumer(&mockConsumer{}, "topic", 0, offsetInfo{200, "m3ta"}, sarama.OffsetOldest)
Expect(err).NotTo(HaveOccurred())
defer pc.Close()
close(pc.dead)
state := pc.getState()
Expect(state.Info.Offset).To(Equal(int64(-1)))
Expect(state.Info.Metadata).To(Equal("m3ta"))
})
It("should update state", func() {
subject.MarkOffset(2001, "met@") // should set state
Expect(subject.getState()).To(Equal(partitionState{
Info: offsetInfo{2002, "met@"},
Dirty: true,
}))
subject.markCommitted(2002) // should reset dirty status
Expect(subject.getState()).To(Equal(partitionState{
Info: offsetInfo{2002, "met@"},
}))
subject.MarkOffset(2001, "me7a") // should not update state
Expect(subject.getState()).To(Equal(partitionState{
Info: offsetInfo{2002, "met@"},
}))
subject.MarkOffset(2002, "me7a") // should bump state
Expect(subject.getState()).To(Equal(partitionState{
Info: offsetInfo{2003, "me7a"},
Dirty: true,
}))
// After committing a later offset, try rewinding back to earlier offset with new metadata.
subject.ResetOffset(2001, "met@")
Expect(subject.getState()).To(Equal(partitionState{
Info: offsetInfo{2002, "met@"},
Dirty: true,
}))
subject.markCommitted(2002) // should not unset state
Expect(subject.getState()).To(Equal(partitionState{
Info: offsetInfo{2002, "met@"},
}))
subject.MarkOffset(2002, "me7a") // should bump state
Expect(subject.getState()).To(Equal(partitionState{
Info: offsetInfo{2003, "me7a"},
Dirty: true,
}))
subject.markCommitted(2003)
Expect(subject.getState()).To(Equal(partitionState{
Info: offsetInfo{2003, "me7a"},
}))
})
})
var _ = Describe("partitionMap", func() {
var subject *partitionMap
BeforeEach(func() {
subject = newPartitionMap()
})
It("should fetch/store", func() {
Expect(subject.Fetch("topic", 0)).To(BeNil())
pc, err := newPartitionConsumer(&mockConsumer{}, "topic", 0, offsetInfo{2000, "m3ta"}, sarama.OffsetNewest)
Expect(err).NotTo(HaveOccurred())
subject.Store("topic", 0, pc)
Expect(subject.Fetch("topic", 0)).To(Equal(pc))
Expect(subject.Fetch("topic", 1)).To(BeNil())
Expect(subject.Fetch("other", 0)).To(BeNil())
})
It("should return info", func() {
pc0, err := newPartitionConsumer(&mockConsumer{}, "topic", 0, offsetInfo{2000, "m3ta"}, sarama.OffsetNewest)
Expect(err).NotTo(HaveOccurred())
pc1, err := newPartitionConsumer(&mockConsumer{}, "topic", 1, offsetInfo{2000, "m3ta"}, sarama.OffsetNewest)
Expect(err).NotTo(HaveOccurred())
subject.Store("topic", 0, pc0)
subject.Store("topic", 1, pc1)
info := subject.Info()
Expect(info).To(HaveLen(1))
Expect(info).To(HaveKeyWithValue("topic", []int32{0, 1}))
})
It("should create snapshots", func() {
pc0, err := newPartitionConsumer(&mockConsumer{}, "topic", 0, offsetInfo{2000, "m3ta"}, sarama.OffsetNewest)
Expect(err).NotTo(HaveOccurred())
pc1, err := newPartitionConsumer(&mockConsumer{}, "topic", 1, offsetInfo{2000, "m3ta"}, sarama.OffsetNewest)
Expect(err).NotTo(HaveOccurred())
subject.Store("topic", 0, pc0)
subject.Store("topic", 1, pc1)
subject.Fetch("topic", 1).MarkOffset(2000, "met@")
Expect(subject.Snapshot()).To(Equal(map[topicPartition]partitionState{
{"topic", 0}: {Info: offsetInfo{2000, "m3ta"}, Dirty: false},
{"topic", 1}: {Info: offsetInfo{2001, "met@"}, Dirty: true},
}))
})
})