Files

47 lines
1.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package emission
import "testing"
// 回归:func([]byte) 快速路径此前不写 ptr,Off/RemoveListener 永远删不掉,
// 并行 MCS 通道加入的持久监听器泄漏后每包数据重复进入(日志刷屏)。
func TestRemoveListenerFastPathFuncBytes(t *testing.T) {
e := NewEmitter()
calls := 0
fn := func(s []byte) { calls++ }
e.On("data", fn)
e.Emit("data", []byte("x"))
e.Emit("data", []byte("y"))
if calls != 2 {
t.Fatalf("calls=%d, want 2", calls)
}
e.Off("data", fn)
e.Emit("data", []byte("z"))
if calls != 2 {
t.Fatalf("Off 未生效: calls=%d, want 2", calls)
}
}
// func(error) 与 func() 快速路径同样必须可摘除。
func TestRemoveListenerFastPathMisc(t *testing.T) {
e := NewEmitter()
n := 0
fnErr := func(err error) { n++ }
e.On("error", fnErr)
e.Emit("error", nil)
e.Off("error", fnErr)
e.Emit("error", nil)
if n != 1 {
t.Fatalf("func(error) Off 未生效: n=%d", n)
}
m := 0
fn0 := func() { m++ }
e.On("tick", fn0)
e.Emit("tick")
e.Off("tick", fn0)
e.Emit("tick")
if m != 1 {
t.Fatalf("func() Off 未生效: m=%d", m)
}
}