1 Star 0 Fork 4

jiaoyuedave/Service-Box-go

forked from Plato/Service-Box-go 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
service_layer_test.go 15.45 KB
一键复制 编辑 原始数据 按行查看 历史
CloudGuan 提交于 2022-11-01 11:10 . update: 更新访问控制权限
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
package sbox
import (
"gitee.com/dennis-kk/rpc-go-backend/idlrpc"
"gitee.com/dennis-kk/rpc-go-backend/idlrpc/pkg/transport"
"gitee.com/dennis-kk/service-box-go/common"
_ "gitee.com/dennis-kk/service-box-go/internal/zookeeper"
"gitee.com/dennis-kk/service-box-go/util/errors"
"gitee.com/dennis-kk/service-box-go/util/service_infra"
"gitee.com/dennis-kk/service-box-go/util/slog"
"gitee.com/dennis-kk/service-box-go/util/slog/zap"
"sync/atomic"
"testing"
"time"
)
var (
testLayer *serviceLayer
logger slog.BoxLogger
res = struct {
name string
value string
err error
}{}
testCh *BoxChannel
)
func init() {
logger, _ = zap.NewLogger(slog.WithCallerSkipCount(2))
slog.SetDefaultLog(logger)
testCh = &BoxChannel{
status: common.ChannelClose,
localHost: "127.0.0.1:4666",
peerHost: "127.0.0.1:2333",
conn: &testConn{},
}
}
func testChangedHandle(name string, eventType service_infra.ChangedType, changeInfo *service_infra.ChangedInfo, err error) {
if err != nil {
return
}
switch eventType {
case service_infra.ServiceChange:
res.name = name
res.err = err
for _, h := range changeInfo.Hosts {
res.value = h
}
case service_infra.ServiceDelete:
}
}
func testInitLayer() error {
res = struct {
name string
value string
err error
}{}
testLayer, _ = makeNewLayer("192.168.82.22:2181", testConnectToservice)
return nil
}
func testConnectToservice(name, network, host string) {
go func() {
testCh.status = common.ChannelRunning
res.err = testLayer.onConnect(host, testCh)
if res.err != nil {
return
}
}()
}
func testCallBack(name string, trans *BoxChannel, err error) {
res.name = name
res.value = trans.RemoteAddr()
res.err = err
logger.Info("CallBack Service Successful !")
}
func testProxyFinder(uuid uint64, trans transport.ITransport) (idlrpc.IProxy, error) {
return nil, nil
}
func makeNewLayer(host string, ch ConnectHandle) (*serviceLayer, error) {
layer := makeServiceLayer(ch, logger)
cfg := &serviceLayerConfig{
MiddlewareType: "zookeeper",
Prefix: "/",
Hosts: []string{
host,
},
}
if err := layer.init(cfg, testProxyFinder); err != nil {
return nil, err
}
err := layer.svs.AddListener(layer.onServiceChanged)
if err != nil {
return nil, err
}
if err := layer.start(); err != nil {
return nil, err
}
return layer, nil
}
func TestRegisterService(t *testing.T) {
if err := testInitLayer(); err != nil {
t.Fatalf("init service layer %v", err)
}
if err := testLayer.registerService("5871407834537456905", "127.0.0.1:2333"); err != nil {
t.Fatalf("register Service Error %v", err)
}
if err := testLayer.getTransport("5871407834537456905", testCallBack); err != nil {
t.Fatalf("get transport error %v !", err)
}
ticker := time.NewTimer(100 * time.Millisecond)
run := true
for run {
select {
case <-ticker.C:
run = false
default:
testLayer.tick()
}
}
if res.err != nil {
t.Errorf("get service error %v", res.err)
}
if res.name != "5871407834537456905" || res.value != "127.0.0.1:2333" {
t.Errorf("test error %s, %s", res.name, res.value)
}
}
func TestAddServiceWatcher(t *testing.T) {
if err := testInitLayer(); err != nil {
t.Fatalf("init service layer %v", err)
}
watcher := func(eType ServiceEvent, name string, host string, proxy idlrpc.IProxy) {}
err := testLayer.addWatcher(5871407834537456905, "5871407834537456905", watcher)
if err != nil {
t.Fatalf("add service watcher error !!!")
}
}
func TestRepeatedAddWatcher(t *testing.T) {
if err := testInitLayer(); err != nil {
t.Fatalf("init service layer %v", err)
}
watcher := func(eType ServiceEvent, name string, host string, proxy idlrpc.IProxy) {}
err := testLayer.addWatcher(5871407834537456905, "5871407834537456905", watcher)
if err != nil {
t.Fatalf("add service watcher error !!!")
}
err = testLayer.addWatcher(5871407834537456905, "5871407834537456905", watcher)
if err != errors.RepeatedWatcher {
t.Fatalf("unexcpetion error %v", err)
}
testLayer.stop()
}
func TestMultiAddWatcher(t *testing.T) {
if err := testInitLayer(); err != nil {
t.Fatalf("init service layer %v", err)
}
watcher1 := func(eType ServiceEvent, name string, host string, proxy idlrpc.IProxy) {}
watcher2 := func(eType ServiceEvent, name string, host string, proxy idlrpc.IProxy) {}
err := testLayer.addWatcher(5871407834537456905, "5871407834537456905", watcher1)
if err != nil {
t.Fatalf("add service watcher error !!!")
}
err = testLayer.addWatcher(5871407834537456905, "5871407834537456905", watcher2)
if err != nil {
t.Fatalf("unexcpetion error %v", err)
}
testLayer.stop()
}
func TestSingleWatcher(t *testing.T) {
if err := testInitLayer(); err != nil {
t.Fatalf("init service layer %v", err)
}
except := struct {
name string
host string
}{}
watcher := func(eType ServiceEvent, name string, host string, proxy idlrpc.IProxy) {
except.name = name
except.host = host
}
err := testLayer.addWatcher(5871407834537456905, "5871407834537456905", watcher)
if err != nil {
t.Fatalf("add service watcher error !!!")
}
if err := testLayer.registerService("5871407834537456905", "127.0.0.1:2333"); err != nil {
t.Fatalf("register Service Error %v", err)
}
if err := testLayer.getTransport("5871407834537456905", testCallBack); err != nil {
t.Fatalf("get transport error %v !", err)
}
ticker := time.NewTimer(100 * time.Millisecond)
run := true
for run {
select {
case <-ticker.C:
run = false
default:
testLayer.tick()
}
}
if except.name != "5871407834537456905" {
t.Errorf("unexception service name %s:5871407834537456905", except.name)
}
if except.host != "127.0.0.1:2333" {
t.Errorf("unexception service host %s:127.0.0.1:2333", except.host)
}
testLayer.stop()
}
//TestAddWatcherBeforeRegisterService 注册服务前就已然添加监听
func TestAddWatcherBeforeRegisterService(t *testing.T) {
if err := testInitLayer(); err != nil {
t.Fatalf("init service layer %v", err)
}
except := struct {
name string
host string
}{}
watcher := func(eType ServiceEvent, name string, host string, proxy idlrpc.IProxy) {
except.name = name
except.host = host
}
err := testLayer.addWatcher(5871407834537456905, "5871407834537456905", watcher)
if err != nil {
t.Fatalf("add service watcher error %v!!!", err)
}
if err := testLayer.registerService("5871407834537456905", "127.0.0.1:2333"); err != nil {
t.Fatalf("register Service Error %v", err)
}
ticker := time.NewTimer(100 * time.Millisecond)
run := true
for run {
select {
case <-ticker.C:
run = false
default:
testLayer.tick()
}
}
if except.name != "5871407834537456905" {
t.Errorf("unexception service name %s:5871407834537456905", except.name)
}
if except.host != "127.0.0.1:2333" {
t.Errorf("unexception service host %s:127.0.0.1:2333", except.host)
}
testLayer.stop()
}
//TestAddWatcherWithAlreadyDiscovered 添加监听时,服务已经进行过发现
func TestAddWatcherWithAlreadyDiscovered(t *testing.T) {
if err := testInitLayer(); err != nil {
t.Fatalf("init service layer %v", err)
}
except := struct {
name string
host string
}{}
watcher := func(eType ServiceEvent, name string, host string, proxy idlrpc.IProxy) {
except.name = name
except.host = host
}
if err := testLayer.registerService("5871407834537456905", "127.0.0.1:2333"); err != nil {
t.Fatalf("register Service Error %v", err)
}
if err := testLayer.getTransport("5871407834537456905", testCallBack); err != nil {
t.Fatalf("get transport error %v !", err)
}
//确定已经发现到服务
ticker := time.NewTimer(100 * time.Millisecond)
run := true
for run {
select {
case <-ticker.C:
run = false
default:
testLayer.tick()
}
}
if res.name != "5871407834537456905" {
t.Fatalf("discover Service Error %s", res.name)
}
err := testLayer.addWatcher(5871407834537456905, "5871407834537456905", watcher)
if err != nil {
t.Fatalf("add service watcher error %v!!!", err)
}
ticker = time.NewTimer(100 * time.Millisecond)
run = true
for run {
select {
case <-ticker.C:
run = false
default:
testLayer.tick()
}
}
if except.name != "5871407834537456905" {
t.Errorf("unexception service name %s:5871407834537456905", except.name)
}
if except.host != "127.0.0.1:2333" {
t.Errorf("unexception service host %s:127.0.0.1:2333", except.host)
}
testLayer.stop()
}
func TestTriggerMultiWatcher(t *testing.T) {
if err := testInitLayer(); err != nil {
t.Fatalf("init service layer %v", err)
}
except := struct {
name string
host string
time int32
}{}
watcher1 := func(eType ServiceEvent, name string, host string, proxy idlrpc.IProxy) {
except.name = name
except.host = host
atomic.AddInt32(&except.time, 1)
}
watcher2 := func(eType ServiceEvent, name string, host string, proxy idlrpc.IProxy) {
atomic.AddInt32(&except.time, 1)
}
err := testLayer.addWatcher(5871407834537456905, "5871407834537456905", watcher1)
if err != nil {
t.Fatalf("add service watcher error !!!")
}
err = testLayer.addWatcher(5871407834537456905, "5871407834537456905", watcher2)
if err != nil {
t.Fatalf("unexcpetion error %v", err)
}
if err := testLayer.registerService("5871407834537456905", "127.0.0.1:2333"); err != nil {
t.Fatalf("register Service Error %v", err)
}
if err := testLayer.getTransport("5871407834537456905", testCallBack); err != nil {
t.Fatalf("get transport error %v !", err)
}
ticker := time.NewTimer(100 * time.Millisecond)
run := true
for run {
select {
case <-ticker.C:
run = false
default:
testLayer.tick()
}
}
if except.name != "5871407834537456905" {
t.Errorf("unexception service name %s:5871407834537456905", except.name)
}
if except.host != "127.0.0.1:2333" {
t.Errorf("unexception service host %s:127.0.0.1:2333", except.host)
}
if except.time != 2 {
t.Errorf("unexception service time %d:2", except.time)
}
testLayer.stop()
}
func TestRemoveTrigger(t *testing.T) {
if err := testInitLayer(); err != nil {
t.Fatalf("init service layer %v", err)
}
except := struct {
name string
host string
}{}
watcher1 := func(eType ServiceEvent, name string, host string, proxy idlrpc.IProxy) {
except.name = name
except.host = host
}
err := testLayer.addWatcher(5871407834537456905, "5871407834537456905", watcher1)
if err != nil {
t.Fatalf("add service watcher error !!!")
}
err = testLayer.removeWatcher("5871407834537456905", watcher1)
if err != nil {
t.Fatalf("remove service watcher error !!!")
}
if _, ok := testLayer.watcher.watchersMap["5871407834537456905"]; ok {
t.Fatalf("remove service watcher failed !!!")
}
testLayer.stop()
}
func TestRemoveUnExistWatcher(t *testing.T) {
if err := testInitLayer(); err != nil {
t.Fatalf("init service layer %v", err)
}
except := struct {
name string
host string
}{}
watcher1 := func(eType ServiceEvent, name string, host string, proxy idlrpc.IProxy) {
except.name = name
except.host = host
}
err := testLayer.removeWatcher("5871407834537456905", watcher1)
if err != nil {
t.Fatalf("remove service watcher error !!!")
}
testLayer.stop()
}
//TestServiceRemove 启动服务,删除节点,其他集群能够正常收到删除消息并且剔除已
func TestServiceRemove(t *testing.T) {
if err := testInitLayer(); err != nil {
t.Fatalf("init service layer %v", err)
}
layer2, _ := makeNewLayer("192.168.82.22:2181", testConnectToservice)
if layer2 == nil {
t.Fatalf("invalid layer")
}
// layer2 注册服务
if err := layer2.registerService("5871407834537456905", "127.0.0.1:2333"); err != nil {
t.Fatalf("register Service Error %v", err)
}
// tick 1秒
ticker := time.NewTimer(1 * time.Second)
run := true
for run {
select {
case <-ticker.C:
run = false
default:
testLayer.tick()
layer2.tick()
}
}
res.name = ""
//layer2 反注册
layer2.stop()
ticker = time.NewTimer(1 * time.Second)
// 获取layer2
if err := testLayer.getTransport("5871407834537456905", testCallBack); err != nil {
t.Fatalf("get transport error %v !", err)
}
// tick 1秒 触发超时
ticker = time.NewTimer(1 * time.Second)
run = true
for run {
select {
case <-ticker.C:
run = false
default:
testLayer.tick()
layer2.tick()
}
}
if len(res.name) != 0 {
t.Fatalf("dirty data of nodes %s", res.name)
}
testLayer.stop()
}
func TestFindServiceAfterReboot(t *testing.T) {
if err := testInitLayer(); err != nil {
t.Fatalf("init service layer %v", err)
}
layer2, _ := makeNewLayer("192.168.82.22:2181", testConnectToservice)
if layer2 == nil {
t.Fatalf("invalid layer")
}
// layer2 注册服务
if err := layer2.registerService("5871407834537456905", "127.0.0.1:2333"); err != nil {
t.Fatalf("register Service Error %v", err)
}
// tick 1秒
ticker := time.NewTimer(1 * time.Second)
run := true
for run {
select {
case <-ticker.C:
run = false
default:
testLayer.tick()
layer2.tick()
}
}
//layer2 反注册
layer2.stop()
//重新注册
layer2, _ = makeNewLayer("192.168.82.22:2181", testConnectToservice)
if layer2 == nil {
t.Fatalf("invalid layer")
}
// layer2 注册服务
if err := layer2.registerService("5871407834537456905", "127.0.0.1:2333"); err != nil {
t.Fatalf("register Service Error %v", err)
}
// 获取layer2
if err := testLayer.getTransport("5871407834537456905", testCallBack); err != nil {
t.Fatalf("get transport error %v !", err)
}
// tick 1秒
ticker = time.NewTimer(2 * time.Second)
run = true
for run {
select {
case <-ticker.C:
run = false
default:
testLayer.tick()
layer2.tick()
}
}
if res.name != "5871407834537456905" || res.value != "127.0.0.1:2333" {
t.Errorf("test error %s, %s", res.name, res.value)
}
}
//TestRefinedServiceAfterReboot 测试一个已经发现过的服务重启之后,能否正常更新对应的connect
func TestRefinedServiceAfterReboot(t *testing.T) {
// 第一次初始化准备服务
if err := testInitLayer(); err != nil {
t.Fatalf("init service layer %v", err)
}
layer2, _ := makeNewLayer("192.168.82.22:2181", testConnectToservice)
if layer2 == nil {
t.Fatalf("invalid layer")
}
// layer2 注册服务
if err := layer2.registerService("5871407834537456905", "127.0.0.1:2333"); err != nil {
t.Fatalf("register Service Error %v", err)
}
// 第一次尝试发现
if err := testLayer.getTransport("5871407834537456905", testCallBack); err != nil {
t.Fatalf("get transport error %v !", err)
}
//tick 1秒
ticker := time.NewTimer(1 * time.Second)
run := true
for run {
select {
case <-ticker.C:
run = false
default:
testLayer.tick()
layer2.tick()
}
}
//layer2 反注册
layer2.stop()
//test layer close
testLayer.onClose("127.0.0.1:2333")
//ch 清理
testCh.status = common.ChannelClose
// tick 1秒
ticker = time.NewTimer(1 * time.Second)
run = true
for run {
select {
case <-ticker.C:
run = false
default:
testLayer.tick()
layer2.tick()
}
}
//重新注册
layer2, _ = makeNewLayer("192.168.82.22:2181", testConnectToservice)
if layer2 == nil {
t.Fatalf("invalid layer")
}
// layer2 注册服务
if err := layer2.registerService("5871407834537456905", "127.0.0.1:2333"); err != nil {
t.Fatalf("register Service Error %v", err)
}
// 第一次尝试发现
if err := testLayer.getTransport("5871407834537456905", testCallBack); err != nil {
t.Fatalf("get transport error %v !", err)
}
// tick 1秒
ticker = time.NewTimer(1 * time.Second)
run = true
for run {
select {
case <-ticker.C:
run = false
default:
testLayer.tick()
layer2.tick()
}
}
if testCh.status != common.ChannelRunning {
t.Fatalf("error for channel status %d ", testCh.status)
}
testLayer.stop()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jiaoyuedave/service-box-go.git
git@gitee.com:jiaoyuedave/service-box-go.git
jiaoyuedave
service-box-go
Service-Box-go
master

搜索帮助