fox/processor/processor_test.go

49 lines
1.2 KiB
Go
Raw Normal View History

2025-05-27 00:18:59 +08:00
package processor
import (
"fmt"
2025-05-27 00:23:46 +08:00
"github.com/fox/fox/processor/pb"
2025-05-27 00:18:59 +08:00
"github.com/golang/protobuf/proto"
"testing"
)
2025-05-27 00:23:46 +08:00
func onChat(userId int64, req *pb.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-05-27 00:23:46 +08:00
pb.MsgId_Internal: {pb.InternalMsg{}, onChat},
2025-05-27 00:18:59 +08:00
})
2025-05-27 00:23:46 +08:00
tmp := &pb.InternalMsg{UserId: 1, ConnId: 1, MsgId: int32(pb.MsgId_Internal), Msg: []byte("hello world")}
2025-05-27 00:18:59 +08:00
data, _ := proto.Marshal(tmp)
2025-05-27 00:23:46 +08:00
req, _ := p.Unmarshal(int32(pb.MsgId_Internal), data)
if err := p.Dispatch(int32(pb.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{
pb.MsgId_Internal: {pb.InternalMsg{}, onChat},
})
registerMessage(p, int32(pb.MsgId_Internal), pb.InternalMsg{}, onChat)
tmp := &pb.InternalMsg{UserId: 1, ConnId: 1, MsgId: int32(pb.MsgId_Internal), Msg: []byte("hello world")}
data, _ := proto.Marshal(tmp)
req, _ := p.Unmarshal(int32(pb.MsgId_Internal), data)
if err := p.Dispatch(int32(pb.MsgId_Internal), int64(1), req); err != nil {
t.Error(err)
}
}