forked from hanguofeng/taiji
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsliding_window_arbiter_test.go
184 lines (158 loc) · 5.61 KB
/
sliding_window_arbiter_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"testing"
"time"
"github.com/Shopify/sarama"
"github.com/stretchr/testify/assert"
)
type SlidingWindowArbiterPartitionConsumerMock struct {
stopper chan struct{}
messages chan *sarama.ConsumerMessage
errors chan *sarama.ConsumerError
}
func (pcm *SlidingWindowArbiterPartitionConsumerMock) Init() {
pcm.messages = make(chan *sarama.ConsumerMessage, 5)
pcm.errors = make(chan *sarama.ConsumerError)
go func() {
i := int64(1)
partitionConsumerMockLoop:
for {
newMsg := &sarama.ConsumerMessage{
Topic: "test_topic",
Partition: 1,
Offset: i,
}
select {
case pcm.messages <- newMsg:
i++
case <-pcm.stopper:
break partitionConsumerMockLoop
}
}
}()
}
func (pcm *SlidingWindowArbiterPartitionConsumerMock) Errors() <-chan *sarama.ConsumerError {
return pcm.errors
}
func (pcm *SlidingWindowArbiterPartitionConsumerMock) Messages() <-chan *sarama.ConsumerMessage {
return pcm.messages
}
func (pcm *SlidingWindowArbiterPartitionConsumerMock) Close() error {
close(pcm.stopper)
return nil
}
func (*SlidingWindowArbiterPartitionConsumerMock) AsyncClose() {
}
func (*SlidingWindowArbiterPartitionConsumerMock) HighWaterMarkOffset() int64 {
return 0
}
func TestSlidingWindowArbiter(t *testing.T) {
t.Log("Allocated new SlidingWindowArbiter")
arbiter, err := NewArbiter("SlidingWindow")
assert.Nil(t, err, "Create SlidingWindowArbiter failed")
// config/callbackManager init
callbackConfig := &CallbackItemConfig{
Url: "http://invalid_url_that_should_never_deliver",
}
callbackConfig.OffsetConfig.StorageName = "null"
callbackConfig.ArbiterConfig = make(ArbiterConfig)
callbackConfig.ArbiterConfig["window_size"] = 10
offsetManager := NewOffsetManager()
callbackManager := &CallbackManager{
offsetManager: offsetManager,
}
offsetManager.Init(callbackConfig.OffsetConfig, callbackManager)
partitionConsumerMock := &SlidingWindowArbiterPartitionConsumerMock{
stopper: make(chan struct{}),
}
partitionConsumerMock.Init()
manager := &PartitionManager{
partitionConsumer: &PartitionConsumer{
kafkaPartitionConsumer: partitionConsumerMock,
},
manager: callbackManager,
}
// arbiter init
t.Log("Initializing SlidingWindowArbiter")
err = arbiter.Init(callbackConfig, callbackConfig.ArbiterConfig, manager)
assert.Nil(t, err, "Init SlidingWindowArbiter failed")
t.Log("Started SlidingWindowArbiter")
go arbiter.Run()
defer arbiter.Close()
defer manager.GetKafkaPartitionConsumer().Close()
if err := arbiter.Ready(); err != nil {
t.Fatalf("Arbiter start failed [err:%s]", err)
}
messages := arbiter.MessageChannel()
offsets := arbiter.OffsetChannel()
assert.NotNil(t, messages, "Arbiter MessageChannel should not be nil")
t.Logf("PartitionConsumerMock channel length [length:%d][capacity:%d]",
len(manager.GetKafkaPartitionConsumer().Messages()), cap(manager.GetKafkaPartitionConsumer().Messages()))
t.Logf("Arbiter message channel length and capacity [length:%d][capacity:%d]", len(messages), cap(messages))
t.Logf("Current arbiter offsetBase [offsetBase:%d]", arbiter.(*SlidingWindowArbiter).offsetBase)
t.Logf("Arbiter stats [stat:%v]", arbiter.GetStat())
// test all nocommit case
for i := 0; i != 10; i++ {
select {
case message := <-messages:
t.Logf("Message received [offset:%d]", message.Offset)
case <-time.After(time.Second):
// force yield, wait for arbiter goroutine to process
t.Fatalf("Ready message not received when window is not full")
}
}
select {
case <-messages:
t.Fatalf("Ready message count exceeded window size")
case <-time.After(time.Second):
// force yield, wait for arbiter goroutine to process
}
// commit a non-base offset
offsets <- 5
select {
case <-messages:
t.Fatalf("Message offset exceeded window size + offsetBase")
t.Logf("Current arbiter offsetBase [offsetBase:%d]", arbiter.(*SlidingWindowArbiter).offsetBase)
t.Logf("Arbiter stats [stat:%v]", arbiter.GetStat())
case <-time.After(time.Second):
// force yield, wait for arbiter goroutine to process
}
// commit base offset
offsets <- 1
select {
case message := <-messages:
t.Logf("Message received [offset:%d]", message.Offset)
case <-time.After(time.Second):
// force yield, wait for arbiter goroutine to process
t.Fatalf("No available message even offsetBase is received")
t.Logf("Current arbiter offsetBase [offsetBase:%d]", arbiter.(*SlidingWindowArbiter).offsetBase)
t.Logf("Arbiter stats [stat:%v]", arbiter.GetStat())
}
select {
case <-messages:
t.Fatalf("Message offset exceeded window size + offsetBase")
t.Logf("Current arbiter offsetBase [offsetBase:%d]", arbiter.(*SlidingWindowArbiter).offsetBase)
t.Logf("Arbiter stats [stat:%v]", arbiter.GetStat())
case <-time.After(time.Second):
// force yield, wait for arbiter goroutine to process
}
// commit several offset, including previous offset 5
offsets <- 3
offsets <- 4
offsets <- 2
for i := 0; i != 4; i++ {
select {
case message := <-messages:
t.Logf("Message received [offset:%d]", message.Offset)
case <-time.After(time.Second):
// force yield, wait for arbiter goroutine to process
t.Fatalf("Ready message not received when window is not full")
}
}
t.Logf("PartitionConsumerMock channel length [length:%d][capacity:%d]",
len(manager.GetKafkaPartitionConsumer().Messages()), cap(manager.GetKafkaPartitionConsumer().Messages()))
t.Logf("Arbiter message channel length and capacity [length:%d][capacity:%d]", len(messages), cap(messages))
t.Logf("Current arbiter offsetBase [offsetBase:%d]", arbiter.(*SlidingWindowArbiter).offsetBase)
t.Logf("Arbiter stats [stat:%v]", arbiter.GetStat())
t.Logf("SlidingWindowArbiter working normally")
}