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,49 @@
package channels
import "testing"
func TestOverflowingChannel(t *testing.T) {
var ch Channel
ch = NewOverflowingChannel(Infinity) // yes this is rather silly, but it should work
testChannel(t, "infinite overflowing channel", ch)
ch = NewOverflowingChannel(None)
go func() {
for i := 0; i < 1000; i++ {
ch.In() <- i
}
ch.Close()
}()
prev := -1
for i := range ch.Out() {
if prev >= i.(int) {
t.Fatal("overflowing channel prev", prev, "but got", i.(int))
}
}
ch = NewOverflowingChannel(10)
for i := 0; i < 1000; i++ {
ch.In() <- i
}
ch.Close()
for i := 0; i < 10; i++ {
val := <-ch.Out()
if i != val.(int) {
t.Fatal("overflowing channel expected", i, "but got", val.(int))
}
}
if val, open := <-ch.Out(); open == true {
t.Fatal("overflowing channel expected closed but got", val)
}
ch = NewOverflowingChannel(None)
ch.In() <- 0
ch.Close()
if val, open := <-ch.Out(); open == true {
t.Fatal("overflowing channel expected closed but got", val)
}
ch = NewOverflowingChannel(2)
testChannelConcurrentAccessors(t, "overflowing channel", ch)
}