Use a ring channel to avoid blocking write of events (#2082)

* Use a ring channel to avoid blocking write of events

* Add eapache/channels dependency
This commit is contained in:
Manuel Alejandro de Brito Fontes 2018-02-13 17:46:18 -08:00 committed by GitHub
parent 33475b7184
commit 9bcb5b08ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
35 changed files with 2833 additions and 78 deletions

View file

@ -0,0 +1,61 @@
package channels
import (
"math/rand"
"testing"
)
func TestResizableChannel(t *testing.T) {
var ch *ResizableChannel
ch = NewResizableChannel()
testChannel(t, "default resizable channel", ch)
ch = NewResizableChannel()
testChannelPair(t, "default resizable channel", ch, ch)
ch = NewResizableChannel()
ch.Resize(Infinity)
testChannel(t, "infinite resizable channel", ch)
ch = NewResizableChannel()
ch.Resize(Infinity)
testChannelPair(t, "infinite resizable channel", ch, ch)
ch = NewResizableChannel()
ch.Resize(5)
testChannel(t, "5-buffer resizable channel", ch)
ch = NewResizableChannel()
ch.Resize(5)
testChannelPair(t, "5-buffer resizable channel", ch, ch)
ch = NewResizableChannel()
testChannelConcurrentAccessors(t, "resizable channel", ch)
}
func TestResizableChannelOnline(t *testing.T) {
stopper := make(chan bool)
ch := NewResizableChannel()
go func() {
for i := 0; i < 1000; i++ {
ch.In() <- i
}
<-stopper
ch.Close()
}()
go func() {
for i := 0; i < 1000; i++ {
ch.Resize(BufferCap(rand.Intn(50) + 1))
}
close(stopper)
}()
for i := 0; i < 1000; i++ {
val := <-ch.Out()
if i != val.(int) {
t.Fatal("resizable channel expected", i, "but got", val.(int))
}
}
}