Skip to content
Snippets Groups Projects
Commit dcd25568 authored by zonghaishang's avatar zonghaishang
Browse files

refactor & unit test

parent 51651d68
No related branches found
No related tags found
No related merge requests found
package loadbalance
import (
"context"
"fmt"
"testing"
)
import (
"github.com/stretchr/testify/assert"
)
import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/protocol"
"github.com/apache/dubbo-go/protocol/invocation"
)
func TestLeastActiveSelect(t *testing.T) {
loadBalance := NewLeastActiveLoadBalance()
var invokers []protocol.Invoker
url, _ := common.NewURL(context.TODO(), "dubbo://192.168.1.0:20000/org.apache.demo.HelloService")
invokers = append(invokers, protocol.NewBaseInvoker(url))
i := loadBalance.Select(invokers, &invocation.RPCInvocation{})
assert.True(t, i.GetUrl().URLEqual(url))
for i := 1; i < 10; i++ {
url, _ := common.NewURL(context.TODO(), fmt.Sprintf("dubbo://192.168.1.%v:20000/org.apache.demo.HelloService", i))
invokers = append(invokers, protocol.NewBaseInvoker(url))
}
loadBalance.Select(invokers, &invocation.RPCInvocation{})
}
func TestLeastActiveByWeight(t *testing.T) {
loadBalance := NewLeastActiveLoadBalance()
var invokers []protocol.Invoker
loop := 3
for i := 1; i <= loop; i++ {
url, _ := common.NewURL(context.TODO(), fmt.Sprintf("test%v://192.168.1.%v:20000/org.apache.demo.HelloService?weight=%v", i, i, i))
invokers = append(invokers, protocol.NewBaseInvoker(url))
}
inv := new(invocation.RPCInvocation)
inv.SetMethod("test")
// test3 active count equals 1
protocol.BeginCount(invokers[2].GetUrl(), inv.MethodName())
loop = 10000
var (
firstCount int
secondCount int
)
for i := 1; i <= loop; i++ {
invoker := loadBalance.Select(invokers, inv)
if invoker.GetUrl().Protocol == "test1" {
firstCount++
} else if invoker.GetUrl().Protocol == "test2" {
secondCount++
}
}
assert.Equal(t, firstCount+secondCount, loop)
}
...@@ -14,13 +14,13 @@ ...@@ -14,13 +14,13 @@
package impl package impl
import ( import (
"github.com/dubbo/go-for-apache-dubbo/common/logger" "github.com/apache/dubbo-go/common/logger"
) )
import ( import (
"github.com/dubbo/go-for-apache-dubbo/common/extension" "github.com/apache/dubbo-go/common/extension"
"github.com/dubbo/go-for-apache-dubbo/filter" "github.com/apache/dubbo-go/filter"
"github.com/dubbo/go-for-apache-dubbo/protocol" "github.com/apache/dubbo-go/protocol"
) )
const active = "active" const active = "active"
......
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dubbo/go-for-apache-dubbo v1.0.0 h1:d1+EmiQNGjOZuEprh7ru/aa/5/pu3lvl8vBkdFEvLuw=
github.com/dubbo/go-for-apache-dubbo v1.0.0/go.mod h1:seatOl29ahGPWuMz4+VqqB6BSNYdHLXtnpPJ54U988Q=
github.com/dubbogo/getty v0.0.0-20190523180329-bdf5e640ea53/go.mod h1:cRMSuoCmwc5lULFFnYZTxyCfZhObmRTNbS7XRnPNHSo=
github.com/dubbogo/getty v1.0.7 h1:5Hg+JwXyCKm9Yr4yJkm98ahhnoa8c2h6br5QJxwQ+YU= github.com/dubbogo/getty v1.0.7 h1:5Hg+JwXyCKm9Yr4yJkm98ahhnoa8c2h6br5QJxwQ+YU=
github.com/dubbogo/getty v1.0.7/go.mod h1:cRMSuoCmwc5lULFFnYZTxyCfZhObmRTNbS7XRnPNHSo= github.com/dubbogo/getty v1.0.7/go.mod h1:cRMSuoCmwc5lULFFnYZTxyCfZhObmRTNbS7XRnPNHSo=
github.com/dubbogo/hessian2 v0.0.0-20190526221400-d5610bbd0a41/go.mod h1:XFGDn4oSZX26zkcfhkM/fCJrOqwQJxk/xgWW1KMJBKM=
github.com/dubbogo/hessian2 v1.0.1 h1:ztI7gJxR3Isxrrl2jE1IZKX61eNR93eRKGhn49vPEX8= github.com/dubbogo/hessian2 v1.0.1 h1:ztI7gJxR3Isxrrl2jE1IZKX61eNR93eRKGhn49vPEX8=
github.com/dubbogo/hessian2 v1.0.1/go.mod h1:XFGDn4oSZX26zkcfhkM/fCJrOqwQJxk/xgWW1KMJBKM= github.com/dubbogo/hessian2 v1.0.1/go.mod h1:XFGDn4oSZX26zkcfhkM/fCJrOqwQJxk/xgWW1KMJBKM=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
......
...@@ -19,7 +19,7 @@ import ( ...@@ -19,7 +19,7 @@ import (
) )
import ( import (
"github.com/dubbo/go-for-apache-dubbo/common" "github.com/apache/dubbo-go/common"
) )
var ( var (
...@@ -35,22 +35,22 @@ func (rpc *RpcStatus) GetActive() int32 { ...@@ -35,22 +35,22 @@ func (rpc *RpcStatus) GetActive() int32 {
} }
func GetStatus(url common.URL, methodName string) *RpcStatus { func GetStatus(url common.URL, methodName string) *RpcStatus {
identity := url.Key() identifier := url.Key()
methodMap, found := methodStatistics.Load(identity) methodMap, found := methodStatistics.Load(identifier)
if !found { if !found {
methodMap = sync.Map{} methodMap = sync.Map{}
methodStatistics.Store(identity, methodMap) methodStatistics.Store(identifier, methodMap)
} }
methodActive := methodMap.(sync.Map) methodActive := methodMap.(sync.Map)
rpcStatus, found := methodActive.Load(methodName) rpcStatus, found := methodActive.Load(methodName)
if !found { if !found {
rpcStatus = RpcStatus{} rpcStatus = &RpcStatus{}
methodActive.Store(methodName, rpcStatus) methodActive.Store(methodName, rpcStatus)
} }
status := rpcStatus.(RpcStatus) status := rpcStatus.(*RpcStatus)
return &status return status
} }
func BeginCount(url common.URL, methodName string) { func BeginCount(url common.URL, methodName string) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment