fox/service/service_test.go
2025-05-29 11:54:33 +08:00

72 lines
1.2 KiB
Go

package service
import (
"github.com/fox/fox/log"
"testing"
"time"
)
const (
TestSrv = "test"
)
type EchoService struct {
*BaseService
}
func (s *EchoService) OnInit() {
log.Debug("onInit")
}
func (s *EchoService) OnStop() {
log.Debug("OnStop")
}
func (s *EchoService) CanStop() bool {
log.Debug("CanStop")
return true
}
func (s *EchoService) Send(topic string, msg []byte) error {
log.Debug(s.Log("send %v to topic:%v", string(msg), topic))
return nil
}
func (s *EchoService) Call(topic string, timeout time.Duration, msg []byte) ([]byte, error) {
_ = timeout
_ = msg
log.Debug(s.Log("call topic:%v", topic))
return nil, nil
}
func (s *EchoService) OnMessage(msg []byte) error {
log.Debug(s.Log("on message:%v", string(msg)))
return nil
}
func NewTestService() *EchoService {
s := new(EchoService)
s.BaseService = NewBaseService(TestSrv, "1", s, s)
s.OnInit()
return s
}
func TestService(t *testing.T) {
log.Open("test.log", log.DebugL)
s := NewTestService()
msg := "hello world"
_ = s.Write([]byte(msg))
s.RunOnce(func() {
time.Sleep(2 * time.Second)
})
s.NotifyStop()
s.WaitStop()
log.Debug("exit")
}
func TestWipeWarn(t *testing.T) {
_ = TopicEx(TestSrv)
_ = RpcTopicEx(TestSrv)
}