Merge pull request #84 from rakyll/more-ikeys
Take advantage of having interface{} chanotify keys
This commit is contained in:
commit
4764181eb5
1 changed files with 16 additions and 16 deletions
|
@ -1,7 +1,6 @@
|
||||||
package chanotify
|
package chanotify
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -11,29 +10,30 @@ func TestNotifier(t *testing.T) {
|
||||||
s := New()
|
s := New()
|
||||||
ch1 := make(chan struct{}, 1)
|
ch1 := make(chan struct{}, 1)
|
||||||
ch2 := make(chan struct{}, 1)
|
ch2 := make(chan struct{}, 1)
|
||||||
s.Add("1", ch1)
|
id1 := "1"
|
||||||
s.Add("2", ch2)
|
id2 := "2"
|
||||||
|
|
||||||
|
s.Add(id1, ch1)
|
||||||
|
s.Add(id2, ch2)
|
||||||
s.m.Lock()
|
s.m.Lock()
|
||||||
if len(s.doneCh) != 2 {
|
if len(s.doneCh) != 2 {
|
||||||
t.Fatalf("expected 2 channels, got %d", len(s.doneCh))
|
t.Fatalf("want 2 channels, got %d", len(s.doneCh))
|
||||||
}
|
}
|
||||||
s.m.Unlock()
|
s.m.Unlock()
|
||||||
ch1 <- struct{}{}
|
ch1 <- struct{}{}
|
||||||
id1 := <-s.Chan()
|
if got, want := <-s.Chan(), id1; got != want {
|
||||||
if id1.(string) != "1" {
|
t.Fatalf("got %v; want %v", got, want)
|
||||||
t.Fatalf("1 should be spawned, got %s", id1)
|
|
||||||
}
|
}
|
||||||
ch2 <- struct{}{}
|
ch2 <- struct{}{}
|
||||||
id2 := <-s.Chan()
|
if got, want := <-s.Chan(), id2; got != want {
|
||||||
if id2.(string) != "2" {
|
t.Fatalf("got %v; want %v", got, want)
|
||||||
t.Fatalf("2 should be spawned, got %s", id2)
|
|
||||||
}
|
}
|
||||||
close(ch1)
|
close(ch1)
|
||||||
close(ch2)
|
close(ch2)
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
s.m.Lock()
|
s.m.Lock()
|
||||||
if len(s.doneCh) != 0 {
|
if len(s.doneCh) != 0 {
|
||||||
t.Fatalf("expected 0 channels, got %d", len(s.doneCh))
|
t.Fatalf("want 0 channels, got %d", len(s.doneCh))
|
||||||
}
|
}
|
||||||
s.m.Unlock()
|
s.m.Unlock()
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ func TestConcurrentNotifier(t *testing.T) {
|
||||||
var chs []chan struct{}
|
var chs []chan struct{}
|
||||||
for i := 0; i < 8; i++ {
|
for i := 0; i < 8; i++ {
|
||||||
ch := make(chan struct{}, 2)
|
ch := make(chan struct{}, 2)
|
||||||
s.Add(strconv.Itoa(i), ch)
|
s.Add(i, ch)
|
||||||
chs = append(chs, ch)
|
chs = append(chs, ch)
|
||||||
}
|
}
|
||||||
testCounter := make(map[interface{}]int)
|
testCounter := make(map[interface{}]int)
|
||||||
|
@ -82,14 +82,14 @@ func TestConcurrentNotifier(t *testing.T) {
|
||||||
func TestAddToBlocked(t *testing.T) {
|
func TestAddToBlocked(t *testing.T) {
|
||||||
s := New()
|
s := New()
|
||||||
ch := make(chan struct{}, 1)
|
ch := make(chan struct{}, 1)
|
||||||
|
id := 1
|
||||||
go func() {
|
go func() {
|
||||||
// give some time to start first select
|
// give some time to start first select
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
s.Add("1", ch)
|
s.Add(id, ch)
|
||||||
ch <- struct{}{}
|
ch <- struct{}{}
|
||||||
}()
|
}()
|
||||||
val := <-s.Chan()
|
if got, want := <-s.Chan(), id; got != want {
|
||||||
if val != "1" {
|
t.Fatalf("got %v; want %v", got, want)
|
||||||
t.Fatalf("Expected 1, got %s", val)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue