only increase round-robin index when lambda returns true

This commit is contained in:
Marten Seemann
2016-08-06 17:00:33 +07:00
parent 1a30313ace
commit 8e7215aaeb
2 changed files with 14 additions and 6 deletions

View File

@@ -80,13 +80,13 @@ func (m *streamsMap) RoundRobinIterate(fn streamLambda) error {
return fmt.Errorf("BUG: Stream %d is closed, but still in openStreams map", streamID)
}
cont, err := fn(str)
m.roundRobinIndex = (m.roundRobinIndex + 1) % numStreams
if err != nil {
return err
}
if !cont {
break
}
m.roundRobinIndex = (m.roundRobinIndex + 1) % numStreams
}
return nil
}

View File

@@ -212,7 +212,7 @@ var _ = Describe("Streams Map", func() {
fn := func(str *stream) (bool, error) {
lambdaCalledForStream = append(lambdaCalledForStream, str.StreamID())
numIterations++
if str.StreamID() == 2 || str.StreamID() == 4 {
if str.StreamID() == 2 {
return false, nil
}
return true, nil
@@ -221,13 +221,21 @@ var _ = Describe("Streams Map", func() {
Expect(err).ToNot(HaveOccurred())
Expect(numIterations).To(Equal(2))
Expect(lambdaCalledForStream).To(Equal([]protocol.StreamID{1, 2}))
Expect(m.roundRobinIndex).To(Equal(2))
Expect(m.roundRobinIndex).To(Equal(1))
numIterations = 0
lambdaCalledForStream = lambdaCalledForStream[:0]
err = m.RoundRobinIterate(fn)
fn2 := func(str *stream) (bool, error) {
lambdaCalledForStream = append(lambdaCalledForStream, str.StreamID())
numIterations++
if str.StreamID() == 4 {
return false, nil
}
return true, nil
}
err = m.RoundRobinIterate(fn2)
Expect(err).ToNot(HaveOccurred())
Expect(numIterations).To(Equal(2))
Expect(lambdaCalledForStream).To(Equal([]protocol.StreamID{3, 4}))
Expect(numIterations).To(Equal(3))
Expect(lambdaCalledForStream).To(Equal([]protocol.StreamID{2, 3, 4}))
})
It("adjust the RoundRobinIndex when deleting an element in front", func() {