2015-12-18 16:48:32 +00:00
|
|
|
package chanotify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNotifier(t *testing.T) {
|
|
|
|
s := New()
|
|
|
|
ch1 := make(chan struct{}, 1)
|
|
|
|
ch2 := make(chan struct{}, 1)
|
2016-01-22 19:32:51 +00:00
|
|
|
id1 := "1"
|
|
|
|
id2 := "2"
|
|
|
|
|
|
|
|
s.Add(id1, ch1)
|
|
|
|
s.Add(id2, ch2)
|
2015-12-18 16:48:32 +00:00
|
|
|
s.m.Lock()
|
2016-01-20 21:42:29 +00:00
|
|
|
if len(s.doneCh) != 2 {
|
2016-01-22 19:32:51 +00:00
|
|
|
t.Fatalf("want 2 channels, got %d", len(s.doneCh))
|
2015-12-18 16:48:32 +00:00
|
|
|
}
|
|
|
|
s.m.Unlock()
|
|
|
|
ch1 <- struct{}{}
|
2016-01-22 19:32:51 +00:00
|
|
|
if got, want := <-s.Chan(), id1; got != want {
|
|
|
|
t.Fatalf("got %v; want %v", got, want)
|
2015-12-18 16:48:32 +00:00
|
|
|
}
|
|
|
|
ch2 <- struct{}{}
|
2016-01-22 19:32:51 +00:00
|
|
|
if got, want := <-s.Chan(), id2; got != want {
|
|
|
|
t.Fatalf("got %v; want %v", got, want)
|
2015-12-18 16:48:32 +00:00
|
|
|
}
|
|
|
|
close(ch1)
|
|
|
|
close(ch2)
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
s.m.Lock()
|
2016-01-20 21:42:29 +00:00
|
|
|
if len(s.doneCh) != 0 {
|
2016-01-22 19:32:51 +00:00
|
|
|
t.Fatalf("want 0 channels, got %d", len(s.doneCh))
|
2015-12-18 16:48:32 +00:00
|
|
|
}
|
|
|
|
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)
|
2016-01-22 19:32:51 +00:00
|
|
|
s.Add(i, ch)
|
2015-12-18 16:48:32 +00:00
|
|
|
chs = append(chs, ch)
|
|
|
|
}
|
2016-01-22 16:15:13 +00:00
|
|
|
testCounter := make(map[interface{}]int)
|
2015-12-18 16:48:32 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-21 23:01:31 +00:00
|
|
|
|
|
|
|
func TestAddToBlocked(t *testing.T) {
|
|
|
|
s := New()
|
|
|
|
ch := make(chan struct{}, 1)
|
2016-01-22 19:32:51 +00:00
|
|
|
id := 1
|
2015-12-21 23:01:31 +00:00
|
|
|
go func() {
|
|
|
|
// give some time to start first select
|
|
|
|
time.Sleep(1 * time.Second)
|
2016-01-22 19:32:51 +00:00
|
|
|
s.Add(id, ch)
|
2015-12-21 23:01:31 +00:00
|
|
|
ch <- struct{}{}
|
|
|
|
}()
|
2016-01-22 19:32:51 +00:00
|
|
|
if got, want := <-s.Chan(), id; got != want {
|
|
|
|
t.Fatalf("got %v; want %v", got, want)
|
2015-12-21 23:01:31 +00:00
|
|
|
}
|
|
|
|
}
|