Add chanotify package
It allows to listen for notifications on bunch of channels. Signed-off-by: Alexander Morozov <lk4d4@docker.com>
This commit is contained in:
parent
5c4b7a66c0
commit
fed10a2c22
2 changed files with 177 additions and 0 deletions
80
chanotify/chanotify_test.go
Normal file
80
chanotify/chanotify_test.go
Normal file
|
@ -0,0 +1,80 @@
|
|||
package chanotify
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNotifier(t *testing.T) {
|
||||
s := New()
|
||||
ch1 := make(chan struct{}, 1)
|
||||
ch2 := make(chan struct{}, 1)
|
||||
s.Add(ch1, "1")
|
||||
s.Add(ch2, "2")
|
||||
s.m.Lock()
|
||||
if len(s.chMap) != 2 {
|
||||
t.Fatalf("expected 2 channels, got %d", len(s.chMap))
|
||||
}
|
||||
s.m.Unlock()
|
||||
ch1 <- struct{}{}
|
||||
id1 := <-s.Chan()
|
||||
if id1 != "1" {
|
||||
t.Fatalf("1 should be spawned, got %s", id1)
|
||||
}
|
||||
ch2 <- struct{}{}
|
||||
id2 := <-s.Chan()
|
||||
if id2 != "2" {
|
||||
t.Fatalf("2 should be spawned, got %s", id2)
|
||||
}
|
||||
close(ch1)
|
||||
close(ch2)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
s.m.Lock()
|
||||
if len(s.chMap) != 0 {
|
||||
t.Fatalf("expected 0 channels, got %d", len(s.chMap))
|
||||
}
|
||||
s.m.Unlock()
|
||||
}
|
||||
|
||||
func TestConcurrentNotifier(t *testing.T) {
|
||||
s := New()
|
||||
var chs []chan struct{}
|
||||
for i := 0; i < 8; i++ {
|
||||
ch := make(chan struct{}, 2)
|
||||
s.Add(ch, strconv.Itoa(i))
|
||||
chs = append(chs, ch)
|
||||
}
|
||||
testCounter := make(map[string]int)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
for id := range s.Chan() {
|
||||
testCounter[id]++
|
||||
}
|
||||
close(done)
|
||||
}()
|
||||
var wg sync.WaitGroup
|
||||
for _, ch := range chs {
|
||||
wg.Add(1)
|
||||
go func(ch chan struct{}) {
|
||||
ch <- struct{}{}
|
||||
ch <- struct{}{}
|
||||
close(ch)
|
||||
wg.Done()
|
||||
}(ch)
|
||||
}
|
||||
wg.Wait()
|
||||
// wait for notifications
|
||||
time.Sleep(1 * time.Second)
|
||||
s.Close()
|
||||
<-done
|
||||
if len(testCounter) != 8 {
|
||||
t.Fatalf("expect to find exactly 8 distinct ids, got %d", len(testCounter))
|
||||
}
|
||||
for id, c := range testCounter {
|
||||
if c != 2 {
|
||||
t.Fatalf("Expected to find exactly 2 id %s, but got %d", id, c)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue