Skip to content

Commit 0c627cb

Browse files
authored
Fix a corner case of back ack and panic. (#53)
1 parent dc5d2e2 commit 0c627cb

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

ack_buffer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (b *AckBuffer) Ack(seq uint64) error {
137137
indexAcked := seq - b.firstSeqNum
138138
if indexAcked >= b.nextIndexToDeliver {
139139
nextSeq := uint64(0)
140-
if b.buff[b.nextIndexToDeliver] != nil {
140+
if b.nextIndexToDeliver < uint64(len(b.buff)) && b.buff[b.nextIndexToDeliver] != nil {
141141
nextSeq = b.buff[b.nextIndexToDeliver].SeqNum
142142
}
143143
return fmt.Errorf("acked message (seq: %d) has not yet been delivered (next: %d)", seq, nextSeq)

ack_buffer_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,38 @@ func TestAckBufferBoundaries(t *testing.T) {
221221
if len(out) != 10 {
222222
t.Errorf("unexpected number of unacked events: %+v", out)
223223
}
224+
225+
// Case 4: ack overflow
226+
b, err = NewAckBuffer(AckBufferOptions{})
227+
b.UpdateCapacity(3)
228+
if err != nil {
229+
t.Errorf("failed creating ack buffer: %v", err)
230+
return
231+
}
232+
233+
if !b.Add(&protocol.DataMessage{}, 0) {
234+
t.Error("failed to add")
235+
}
236+
if !b.Add(&protocol.DataMessage{}, 0) {
237+
t.Error("failed to add")
238+
}
239+
if !b.Add(&protocol.DataMessage{}, 0) {
240+
t.Error("failed to add")
241+
}
242+
243+
if b.GetNextToDeliver(0) == nil {
244+
t.Error("should have been able to get a delivery")
245+
}
246+
if b.GetNextToDeliver(0) == nil {
247+
t.Error("should have been able to get a delivery")
248+
}
249+
if b.GetNextToDeliver(0) == nil {
250+
t.Error("should have been able to get a delivery")
251+
}
252+
253+
if err := b.Ack(4); err == nil {
254+
t.Errorf("invalid ack should have generated an error")
255+
}
224256
}
225257

226258
func TestAckAlways(t *testing.T) {

0 commit comments

Comments
 (0)