mirror of
https://github.com/libp2p/go-eventbus.git
synced 2025-03-25 12:10:06 +08:00
Return error rather than panic in Emit
This commit is contained in:
parent
4afad1f620
commit
b1629519a0
5
basic.go
5
basic.go
@ -28,11 +28,12 @@ type emitter struct {
|
||||
dropper func(reflect.Type)
|
||||
}
|
||||
|
||||
func (e *emitter) Emit(evt interface{}) {
|
||||
func (e *emitter) Emit(evt interface{}) error {
|
||||
if atomic.LoadInt32(&e.closed) != 0 {
|
||||
panic("emitter is closed")
|
||||
return fmt.Errorf("emitter is closed")
|
||||
}
|
||||
e.n.emit(evt)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *emitter) Close() error {
|
||||
|
@ -109,18 +109,13 @@ func TestEmitOnClosed(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
em.Close()
|
||||
|
||||
defer func() {
|
||||
r := recover()
|
||||
if r == nil {
|
||||
t.Errorf("expected panic")
|
||||
}
|
||||
if r.(string) != "emitter is closed" {
|
||||
t.Error("unexpected message")
|
||||
}
|
||||
}()
|
||||
|
||||
em.Emit(EventA{})
|
||||
err = em.Emit(EventA{})
|
||||
if err == nil {
|
||||
t.Errorf("expected panic")
|
||||
}
|
||||
if err.Error() != "emitter is closed" {
|
||||
t.Error("unexpected message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClosingRaces(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user