pkg/wait: make id checking stricter

Do not allow register with same id.
This commit is contained in:
Xiang Li 2016-02-04 09:48:21 -08:00
parent 26f440be7c
commit d43bd48977
2 changed files with 23 additions and 6 deletions

View File

@ -17,6 +17,7 @@
package wait package wait
import ( import (
"log"
"sync" "sync"
"github.com/coreos/etcd/pkg/testutil" "github.com/coreos/etcd/pkg/testutil"
@ -43,6 +44,8 @@ func (w *List) Register(id uint64) <-chan interface{} {
if ch == nil { if ch == nil {
ch = make(chan interface{}, 1) ch = make(chan interface{}, 1)
w.m[id] = ch w.m[id] = ch
} else {
log.Panicf("dup id %x", id)
} }
return ch return ch
} }

View File

@ -17,6 +17,7 @@ package wait
import ( import (
"fmt" "fmt"
"testing" "testing"
"time"
) )
func TestWait(t *testing.T) { func TestWait(t *testing.T) {
@ -34,17 +35,30 @@ func TestWait(t *testing.T) {
} }
} }
func TestRegisterDupSuppression(t *testing.T) { func TestRegisterDupPanic(t *testing.T) {
const eid = 1 const eid = 1
wt := New() wt := New()
ch1 := wt.Register(eid) ch1 := wt.Register(eid)
ch2 := wt.Register(eid)
panicC := make(chan struct{}, 1)
func() {
defer func() {
if r := recover(); r != nil {
panicC <- struct{}{}
}
}()
wt.Register(eid)
}()
select {
case <-panicC:
case <-time.After(1 * time.Second):
t.Errorf("failed to receive panic")
}
wt.Trigger(eid, "foo") wt.Trigger(eid, "foo")
<-ch1 <-ch1
g := <-ch2
if g != nil {
t.Errorf("unexpected non-nil value: %v (%T)", g, g)
}
} }
func TestTriggerDupSuppression(t *testing.T) { func TestTriggerDupSuppression(t *testing.T) {