fox/service/natsService_test.go
2025-05-29 11:56:53 +08:00

93 lines
2.0 KiB
Go

package service
import (
"fmt"
"github.com/fox/fox/ipb"
"github.com/fox/fox/log"
"testing"
"time"
)
const (
GameSrv = "game"
GameType = 1001
NatsAddress = "nats://192.168.232.128:4222"
EtcdAddress = "192.168.232.128:2379"
EtcdAddress2 = "114.132.124.145:2379"
NatsAddress2 = "nats://114.132.124.145:4222"
)
type gameService struct {
*NatsService
//etcdTopic *etcd.Registry[etcd.TopicNode]
//srvTopic string
}
func newGameService() *gameService {
_ = NatsAddress2
_ = NatsAddress
_ = EtcdAddress2
_ = EtcdAddress
var err error
s := new(gameService)
if s.NatsService, err = NewNatsService(&InitNatsServiceParams{
EtcdAddress: []string{EtcdAddress2},
EtcdUsername: "",
EtcdPassword: "",
NatsAddress: []string{NatsAddress2},
ServiceType: GameSrv,
ServiceName: fmt.Sprintf("%s-%d", GameSrv, 0),
OnFunc: nil,
TypeId: GameType,
Version: time.Now().Format("20060102150405"),
}); err != nil {
log.Fatal(err.Error())
return nil
}
//if s.etcdTopic, err = etcd.NewRegistry[etcd.TopicNode]([]string{EtcdAddress2}, "", ""); err != nil {
// log.Error(err.Error())
// s.NatsService.OnStop()
// return nil
//}
s.OnInit()
return s
}
func (s *gameService) OnInit() {
//s.etcdTopic.WatchServices()
if err := s.NatsService.QueueSubscribe(GroupTopic(s), GroupQueue(s)); err != nil {
log.Error(err.Error())
}
s.Run()
log.Debug("onInit")
}
func (s *gameService) OnStop() {
//s.etcdTopic.UnregisterService()
s.NatsService.OnStop()
log.Debug("OnStop")
}
func (s *gameService) OnMessage(msg []byte) error {
log.Debug(s.Log("on message:%v", string(msg)))
return nil
}
func TestGameService(t *testing.T) {
log.Open("test.log", log.DebugL)
s := newGameService()
msg := ipb.MakeMsg(GameSrv, 0, 1, 2, []byte("hello world"))
if err := s.Send(Topic(s), msg); err != nil {
log.Error(err.Error())
}
s.GetServiceNodes().Range(func(key, value interface{}) bool {
log.Debug(s.Log("发现有服务:%v", value))
return true
})
time.Sleep(1 * time.Second)
s.NotifyStop()
s.WaitStop()
log.Debug("exit")
}