93 lines
2.1 KiB
Go
93 lines
2.1 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/rabbitmq/amqp091-go"
|
|
"samba/pkg/log"
|
|
"samba/pkg/servername"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var hall IService
|
|
var chat IService
|
|
|
|
func testNewHall() IService {
|
|
opts := []Option{
|
|
SetOnInit(func(s IService) {
|
|
log.Info(fmt.Sprintf("service:%v init", s.Name()))
|
|
}),
|
|
SetOnStop(func(s IService) {
|
|
log.Info(fmt.Sprintf("service:%v stop", s.Name()))
|
|
}),
|
|
}
|
|
msgHandler := func(_ *amqp091.Delivery) {
|
|
//_ = hall.Send(msg, chat.Name())
|
|
}
|
|
s := NewService(servername.Truco, "", "", msgHandler, opts...)
|
|
return s
|
|
}
|
|
|
|
func testNewChat() IService {
|
|
opts := []Option{
|
|
SetCapacity(256),
|
|
SetOnInit(func(s IService) {
|
|
log.Info(fmt.Sprintf("service:%v init", s.Name()))
|
|
}),
|
|
SetOnStop(func(s IService) {
|
|
log.Info(fmt.Sprintf("service:%v stop", s.Name()))
|
|
}),
|
|
}
|
|
msgHandler := func(msg *amqp091.Delivery) {
|
|
log.Debug(fmt.Sprintf("chat service recv msg:%v", string(msg.Body)))
|
|
time.Sleep(100 * time.Millisecond)
|
|
//ss := FindServiceByType(servername.Truco)
|
|
//for _, s := range ss {
|
|
// //_ = Send(msg, chat.Name(), s.Name())
|
|
//}
|
|
}
|
|
s := NewService(servername.Truco, "", "", msgHandler, opts...)
|
|
return s
|
|
}
|
|
|
|
func testSend() {
|
|
//_ = chat.Send([]byte("hello world"), hall.Name())
|
|
}
|
|
|
|
func testCall() {
|
|
//tm := time.Now().Unix()
|
|
//count := 1
|
|
//for i := 0; i < count; i++ {
|
|
// ret, err := Call("Add", chat.Name(), hall.Name(), 1*time.Second, 1, 2)
|
|
// if err != nil {
|
|
// log.Error(err.Error())
|
|
// continue
|
|
// }
|
|
// if ret[0] != 3 {
|
|
// log.Error(fmt.Sprintf("1+2=%v", ret[0]))
|
|
// }
|
|
//}
|
|
//log.Debug(fmt.Sprintf("%v count cost %v second. rpc msg is empty:%v", count, time.Now().Unix()-tm, mgr.rpcMsg.IsEmpty()))
|
|
}
|
|
|
|
func testTimer() {
|
|
tm := time.Now().Unix()
|
|
chat.NewTimer(5*time.Second, func() {
|
|
log.Debug(fmt.Sprintf("timer cost %v second", time.Now().Unix()-tm))
|
|
})
|
|
}
|
|
|
|
func TestService(t *testing.T) {
|
|
log.Open("test.log", -1)
|
|
hall = testNewHall()
|
|
chat = testNewChat()
|
|
registerService(hall)
|
|
registerService(chat)
|
|
// testSend()
|
|
testCall()
|
|
// testTimer()
|
|
time.Sleep(15 * time.Second)
|
|
unregisterService(chat.Name())
|
|
unregisterService(hall.Name())
|
|
}
|