fox/processor/processor_test.go

82 lines
1.8 KiB
Go
Raw Normal View History

2025-05-27 00:18:59 +08:00
package processor
import (
"fmt"
2025-06-02 01:08:12 +08:00
"github.com/fox/fox/ipb"
2025-06-09 19:11:27 +08:00
"github.com/fox/fox/log"
"github.com/fox/fox/timer"
2025-05-27 00:18:59 +08:00
"github.com/golang/protobuf/proto"
"testing"
)
2025-06-09 19:11:27 +08:00
func initLog() {
log.Open("test.log", log.DebugL)
}
2025-06-02 01:08:12 +08:00
func onChat(userId int64, req *ipb.InternalMsg) {
2025-05-27 00:18:59 +08:00
_ = userId
2025-05-29 16:58:50 +08:00
fmt.Println("onChat.", string(req.Msg))
2025-05-27 00:18:59 +08:00
}
func TestProcessor(t *testing.T) {
p := NewProcessor()
p.RegisterMessages(RegisterMetas{
2025-06-02 01:08:12 +08:00
ipb.MsgId_Internal: {ipb.InternalMsg{}, onChat},
2025-05-27 00:18:59 +08:00
})
2025-06-02 01:08:12 +08:00
tmp := &ipb.InternalMsg{UserId: 1, ConnId: 1, MsgId: int32(ipb.MsgId_Internal), Msg: []byte("hello world")}
2025-05-27 00:18:59 +08:00
data, _ := proto.Marshal(tmp)
2025-06-02 01:08:12 +08:00
req, _ := p.Unmarshal(int32(ipb.MsgId_Internal), data)
if err := p.Dispatch(int32(ipb.MsgId_Internal), int64(1), req); err != nil {
2025-05-27 00:18:59 +08:00
t.Error(err)
}
}
2025-05-29 16:58:50 +08:00
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{
2025-06-02 01:08:12 +08:00
ipb.MsgId_Internal: {ipb.InternalMsg{}, onChat},
2025-05-29 16:58:50 +08:00
})
2025-06-02 01:08:12 +08:00
registerMessage(p, int32(ipb.MsgId_Internal), ipb.InternalMsg{}, onChat)
2025-05-29 16:58:50 +08:00
2025-06-02 01:08:12 +08:00
tmp := &ipb.InternalMsg{UserId: 1, ConnId: 1, MsgId: int32(ipb.MsgId_Internal), Msg: []byte("hello world")}
2025-05-29 16:58:50 +08:00
data, _ := proto.Marshal(tmp)
2025-06-02 01:08:12 +08:00
req, _ := p.Unmarshal(int32(ipb.MsgId_Internal), data)
if err := p.Dispatch(int32(ipb.MsgId_Internal), int64(1), req); err != nil {
2025-05-29 16:58:50 +08:00
t.Error(err)
}
}
2025-06-09 19:11:27 +08:00
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)
}