package processor import ( "fmt" "github.com/fox/fox/ipb" "github.com/fox/fox/log" "github.com/fox/fox/timer" "github.com/golang/protobuf/proto" "testing" ) func initLog() { log.Open("test.log", log.DebugL) } func onChat(userId int64, req *ipb.InternalMsg) { _ = userId fmt.Println("onChat.", string(req.Msg)) } func TestProcessor(t *testing.T) { p := NewProcessor() p.RegisterMessages(RegisterMetas{ ipb.MsgId_Internal: {ipb.InternalMsg{}, onChat}, }) tmp := &ipb.InternalMsg{UserId: 1, ConnId: 1, MsgId: int32(ipb.MsgId_Internal), Msg: []byte("hello world")} data, _ := proto.Marshal(tmp) req, _ := p.Unmarshal(int32(ipb.MsgId_Internal), data) if err := p.Dispatch(int32(ipb.MsgId_Internal), int64(1), req); err != nil { t.Error(err) } } func registerMessage(p *Processor, cmd int32, msg any, cb any) { p.RegisterMessage(cmd, msg, cb) } func TestProcessorAny(t *testing.T) { p := NewProcessor() p.RegisterMessages(RegisterMetas{ ipb.MsgId_Internal: {ipb.InternalMsg{}, onChat}, }) registerMessage(p, int32(ipb.MsgId_Internal), ipb.InternalMsg{}, onChat) tmp := &ipb.InternalMsg{UserId: 1, ConnId: 1, MsgId: int32(ipb.MsgId_Internal), Msg: []byte("hello world")} data, _ := proto.Marshal(tmp) req, _ := p.Unmarshal(int32(ipb.MsgId_Internal), data) if err := p.Dispatch(int32(ipb.MsgId_Internal), int64(1), req); err != nil { t.Error(err) } } type TimerType int const ( TtSecond TimerType = 5 // 每秒定时器 ) func (t TimerType) Number() int32 { return int32(t) } func (t TimerType) String() string { switch t { case TtSecond: return "TtSecond" } return "unknown" } func TestTimerProcessor(t *testing.T) { initLog() p := NewTimerProcessor() p.RegisterMessages(map[timer.ITimeType]any{ TtSecond: func(t *testing.T) { t.Log(TtSecond) }, }) _ = p.Dispatch(TimerType(5), t) }