change server name register to server and process duplicate flag values
This commit is contained in:
parent
3e5f0e1a81
commit
f96a73f219
@ -35,20 +35,23 @@ var profileCmd = &cobra.Command{
|
|||||||
# Get coverage counter from default register center http://127.0.0.1:7777, the result output to stdout.
|
# Get coverage counter from default register center http://127.0.0.1:7777, the result output to stdout.
|
||||||
goc profile
|
goc profile
|
||||||
|
|
||||||
# Get coverage counter from default register center, the result output to specified file.
|
|
||||||
goc profile -o ./coverage.cov
|
|
||||||
|
|
||||||
# Get coverage counter from specified register center, the result output to specified file.
|
|
||||||
goc profile --center=http://192.168.1.1:8080 -o ./coverage.cov
|
|
||||||
|
|
||||||
# Get coverage counter from specified register center, the result output to specified file.
|
# Get coverage counter from specified register center, the result output to specified file.
|
||||||
goc profile --center=http://192.168.1.1:8080 --output=./coverage.cov
|
goc profile --center=http://192.168.1.1:8080 --output=./coverage.cov
|
||||||
|
|
||||||
|
# Get coverage counter of several specified services
|
||||||
|
goc profile --service=service1,service2 --service=service3
|
||||||
|
|
||||||
|
# Get coverage counter of several specified address
|
||||||
|
goc profile --address=address1,address2 --address=address3
|
||||||
|
|
||||||
|
# Force to get coverage counter, ignore any internal error
|
||||||
|
goc profile --force
|
||||||
`,
|
`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
p := cover.ProfileParam{
|
p := cover.ProfileParam{
|
||||||
Force: force,
|
Force: force,
|
||||||
Name: name,
|
Service: svrList,
|
||||||
Address: address,
|
Address: addrList,
|
||||||
}
|
}
|
||||||
res, err := cover.NewWorker(center).Profile(p)
|
res, err := cover.NewWorker(center).Profile(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -73,12 +76,14 @@ goc profile --center=http://192.168.1.1:8080 --output=./coverage.cov
|
|||||||
|
|
||||||
var output string
|
var output string
|
||||||
var force bool
|
var force bool
|
||||||
|
var svrList []string
|
||||||
|
var addrList []string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
profileCmd.Flags().StringVarP(&output, "output", "o", "", "download cover profile")
|
profileCmd.Flags().StringVarP(&output, "output", "o", "", "download cover profile")
|
||||||
profileCmd.Flags().StringVarP(&name, "name", "n", "", "name list to get cover profile")
|
profileCmd.Flags().StringSliceVarP(&svrList, "service", "", nil, "service to get cover profile")
|
||||||
profileCmd.Flags().StringVarP(&address, "address", "a", "", "address list to get cover proflie")
|
profileCmd.Flags().StringSliceVarP(&addrList, "address", "", nil, "address to get cover profile")
|
||||||
profileCmd.Flags().BoolVarP(&force, "force", "f", false, "force")
|
profileCmd.Flags().BoolVarP(&force, "force", "f", false, "force to get cover profile, ignore any internal error")
|
||||||
addBasicFlags(profileCmd.Flags())
|
addBasicFlags(profileCmd.Flags())
|
||||||
rootCmd.AddCommand(profileCmd)
|
rootCmd.AddCommand(profileCmd)
|
||||||
}
|
}
|
||||||
|
2
go.mod
2
go.mod
@ -19,6 +19,6 @@ require (
|
|||||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a
|
golang.org/x/net v0.0.0-20200301022130-244492dfa37a
|
||||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
|
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
|
||||||
golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65
|
golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65
|
||||||
k8s.io/kubernetes v1.13.0 // indirect
|
k8s.io/kubernetes v1.13.0
|
||||||
k8s.io/test-infra v0.0.0-20200511080351-8ac9dbfab055
|
k8s.io/test-infra v0.0.0-20200511080351-8ac9dbfab055
|
||||||
)
|
)
|
||||||
|
1
go.sum
1
go.sum
@ -464,7 +464,6 @@ github.com/klauspost/pgzip v1.2.1/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQ
|
|||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
|
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
|
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
|
||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
|
@ -17,8 +17,7 @@
|
|||||||
package cover
|
package cover
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"errors"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -26,6 +25,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -75,8 +75,6 @@ func (c *client) RegisterService(srv Service) ([]byte, error) {
|
|||||||
if strings.TrimSpace(srv.Name) == "" {
|
if strings.TrimSpace(srv.Name) == "" {
|
||||||
return nil, fmt.Errorf("invalid service name")
|
return nil, fmt.Errorf("invalid service name")
|
||||||
}
|
}
|
||||||
srvPath := strings.Split(srv.Name, "/")
|
|
||||||
srv.Name = srvPath[len(srvPath)-1]
|
|
||||||
u := fmt.Sprintf("%s%s?name=%s&address=%s", c.Host, CoverRegisterServiceAPI, srv.Name, srv.Address)
|
u := fmt.Sprintf("%s%s?name=%s&address=%s", c.Host, CoverRegisterServiceAPI, srv.Name, srv.Address)
|
||||||
res, err := c.do("POST", u, nil)
|
res, err := c.do("POST", u, nil)
|
||||||
return res, err
|
return res, err
|
||||||
@ -93,17 +91,17 @@ func (c *client) ListServices() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) Profile(param ProfileParam) ([]byte, error) {
|
func (c *client) Profile(param ProfileParam) ([]byte, error) {
|
||||||
log.Printf("param:%+v", param)
|
u := fmt.Sprintf("%s%s?force=%s", c.Host, CoverProfileAPI, strconv.FormatBool(param.Force))
|
||||||
u := fmt.Sprintf("%s%s", c.Host, CoverProfileAPI)
|
for _, svr := range param.Service {
|
||||||
args, err := json.Marshal(param)
|
u = u + "&service=" + svr
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
profile, err := c.do("POST", u, bytes.NewReader(args))
|
for _, addr := range param.Address {
|
||||||
|
u = u + "&address=" + addr
|
||||||
|
}
|
||||||
|
profile, err := c.do("GET", u, nil)
|
||||||
if err != nil && isNetworkError(err) {
|
if err != nil && isNetworkError(err) {
|
||||||
profile, err = c.do("POST", u, bytes.NewReader(args))
|
profile, err = c.do("GET", u, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
return profile, err
|
return profile, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,7 +133,10 @@ func (c *client) do(method, url string, body io.Reader) ([]byte, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
err = errors.New(string(responseBody))
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return responseBody, nil
|
return responseBody, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestClientAction(t *testing.T) {
|
func TestClientAction(t *testing.T) {
|
||||||
@ -32,14 +31,59 @@ func TestClientAction(t *testing.T) {
|
|||||||
|
|
||||||
// regsiter service into goc server
|
// regsiter service into goc server
|
||||||
var src Service
|
var src Service
|
||||||
src.Name = "/home/goctest123/_package/newest/goc"
|
src.Name = "goc"
|
||||||
src.Address = "http://127.0.0.1:7777"
|
src.Address = "http://127.0.0.1:7777"
|
||||||
res, err := client.RegisterService(src)
|
res, err := client.RegisterService(src)
|
||||||
srvPath := strings.Split(src.Name, "/")
|
|
||||||
src.Name = srvPath[len(srvPath)-1]
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Contains(t, string(res), "success")
|
assert.Contains(t, string(res), "success")
|
||||||
|
|
||||||
|
// get porfile from goc server
|
||||||
|
profileItems := []struct {
|
||||||
|
param ProfileParam
|
||||||
|
err string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
param: ProfileParam{Force: false, Service: []string{src.Name}, Address: []string{src.Address}},
|
||||||
|
err: "invalid param",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
param: ProfileParam{Force: false, Address: []string{src.Address, "http://unknown.com"}},
|
||||||
|
err: "not found",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
param: ProfileParam{Force: true, Address: []string{src.Address, "http://unknown.com"}},
|
||||||
|
err: "no profile",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
param: ProfileParam{Force: true, Service: []string{src.Name, "unknownSvr"}},
|
||||||
|
err: "no profile",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
param: ProfileParam{Force: false, Service: []string{src.Name, "unknownSvr"}},
|
||||||
|
err: "not found",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
param: ProfileParam{},
|
||||||
|
err: "connection refused",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
param: ProfileParam{Service: []string{src.Name, src.Name}},
|
||||||
|
err: "connection refused",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
param: ProfileParam{Address: []string{src.Address, src.Address}},
|
||||||
|
err: "connection refused",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, item := range profileItems {
|
||||||
|
res, err = client.Profile(item.param)
|
||||||
|
if err != nil {
|
||||||
|
assert.Contains(t, err.Error(), item.err)
|
||||||
|
} else {
|
||||||
|
assert.Contains(t, string(res), item.err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// do list and check service
|
// do list and check service
|
||||||
res, err = client.ListServices()
|
res, err = client.ListServices()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
@ -41,6 +41,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
ErrCoverPkgFailed = errors.New("fail to inject code to project")
|
ErrCoverPkgFailed = errors.New("fail to inject code to project")
|
||||||
ErrCoverListFailed = errors.New("fail to list package dependencies")
|
ErrCoverListFailed = errors.New("fail to list package dependencies")
|
||||||
|
ErrStoreDuplicated = errors.New("storage duplicated")
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestCover is a collection of all counters
|
// TestCover is a collection of all counters
|
||||||
|
@ -54,6 +54,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
{{range $i, $pkgCover := .DepsCover}}
|
{{range $i, $pkgCover := .DepsCover}}
|
||||||
_cover{{$i}} {{$pkgCover.Package.ImportPath | printf "%q"}}
|
_cover{{$i}} {{$pkgCover.Package.ImportPath | printf "%q"}}
|
||||||
@ -212,7 +213,8 @@ func registerHandlers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func registerSelf(address string) ([]byte, error) {
|
func registerSelf(address string) ([]byte, error) {
|
||||||
req, err := http.NewRequest("POST", fmt.Sprintf("%s/v1/cover/register?name=%s&address=%s", {{.Center | printf "%q"}}, os.Args[0], address), nil)
|
selfName := filepath.Base(os.Args[0])
|
||||||
|
req, err := http.NewRequest("POST", fmt.Sprintf("%s/v1/cover/register?name=%s&address=%s", {{.Center | printf "%q"}}, selfName, address), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("http.NewRequest failed: %v", err)
|
log.Fatalf("http.NewRequest failed: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -26,12 +26,11 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"encoding/json"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.org/x/tools/cover"
|
"golang.org/x/tools/cover"
|
||||||
"k8s.io/test-infra/gopherage/pkg/cov"
|
"k8s.io/test-infra/gopherage/pkg/cov"
|
||||||
"strings"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultStore implements the IPersistence interface
|
// DefaultStore implements the IPersistence interface
|
||||||
@ -69,7 +68,7 @@ func GocServer(w io.Writer) *gin.Engine {
|
|||||||
v1 := r.Group("/v1")
|
v1 := r.Group("/v1")
|
||||||
{
|
{
|
||||||
v1.POST("/cover/register", registerService)
|
v1.POST("/cover/register", registerService)
|
||||||
v1.POST("/cover/profile", profile)
|
v1.GET("/cover/profile", profile)
|
||||||
v1.POST("/cover/clear", clear)
|
v1.POST("/cover/clear", clear)
|
||||||
v1.POST("/cover/init", initSystem)
|
v1.POST("/cover/init", initSystem)
|
||||||
v1.GET("/cover/list", listServices)
|
v1.GET("/cover/list", listServices)
|
||||||
@ -87,8 +86,8 @@ type Service struct {
|
|||||||
// ProfileParam is param of profile API (TODO)
|
// ProfileParam is param of profile API (TODO)
|
||||||
type ProfileParam struct {
|
type ProfileParam struct {
|
||||||
Force bool `form:"force"`
|
Force bool `form:"force"`
|
||||||
Name string `form:"name" json:"name"`
|
Service []string `form:"service" json:"service"`
|
||||||
Address string `form:"address" json:"address"`
|
Address []string `form:"address" json:"address"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//listServices list all the registered services
|
//listServices list all the registered services
|
||||||
@ -129,69 +128,76 @@ func registerService(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func profile(c *gin.Context) {
|
func profile(c *gin.Context) {
|
||||||
respByte, err := ioutil.ReadAll(c.Request.Body)
|
force, err := strconv.ParseBool(c.Query("force"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var param ProfileParam
|
|
||||||
json.Unmarshal(respByte, ¶m)
|
|
||||||
if param.Name != "" && param.Address != "" {
|
|
||||||
c.JSON(http.StatusExpectationFailed, gin.H{"error": "invalid param"})
|
c.JSON(http.StatusExpectationFailed, gin.H{"error": "invalid param"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
nameList := strings.Split(param.Name, "&")
|
svrList := c.QueryArray("service")
|
||||||
addrList := strings.Split(param.Address, "&")
|
addrList := c.QueryArray("address")
|
||||||
svrsAll := DefaultStore.GetAll()
|
svrsAll := DefaultStore.GetAll()
|
||||||
svrsUnderTest := make(map[string][]string)
|
svrsUnderTest := make(map[string][]string)
|
||||||
if param.Name == "" && param.Address == "" {
|
if len(svrList) != 0 && len(addrList) != 0 {
|
||||||
|
c.JSON(http.StatusExpectationFailed, gin.H{"error": "invalid param"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(svrList) == 0 && len(addrList) == 0 {
|
||||||
svrsUnderTest = svrsAll
|
svrsUnderTest = svrsAll
|
||||||
} else {
|
} else {
|
||||||
if param.Name != "" {
|
if len(svrList) != 0 {
|
||||||
for _, name := range nameList {
|
for _, name := range svrList {
|
||||||
miss := true
|
if addr, ok := svrsAll[name]; ok {
|
||||||
for svr, addrs := range svrsAll {
|
svrsUnderTest[name] = addr
|
||||||
if svr == name {
|
continue
|
||||||
svrsUnderTest[svr] = addrs
|
|
||||||
miss = false
|
|
||||||
}
|
}
|
||||||
}
|
if !force {
|
||||||
if miss && !param.Force {
|
|
||||||
c.JSON(http.StatusNotFound, fmt.Sprintf("service [%s] not found!", name))
|
c.JSON(http.StatusNotFound, fmt.Sprintf("service [%s] not found!", name))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if param.Address != "" {
|
if len(addrList) != 0 {
|
||||||
|
I:
|
||||||
for _, addr := range addrList {
|
for _, addr := range addrList {
|
||||||
miss := true
|
|
||||||
for svr, addrs := range svrsAll {
|
for svr, addrs := range svrsAll {
|
||||||
|
if contains(svrsUnderTest[svr], addr) {
|
||||||
|
continue I
|
||||||
|
}
|
||||||
for _, a := range addrs {
|
for _, a := range addrs {
|
||||||
if a == addr {
|
if a == addr {
|
||||||
svrsUnderTest[svr] = append(svrsUnderTest[svr], a)
|
svrsUnderTest[svr] = append(svrsUnderTest[svr], a)
|
||||||
miss = false
|
continue I
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if miss && !param.Force {
|
if !force {
|
||||||
c.JSON(http.StatusNotFound, fmt.Sprintf("address [%s] not found!", addr))
|
c.JSON(http.StatusNotFound, fmt.Sprintf("address [%s] not found!", addr))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var mergedProfiles = make([][]*cover.Profile, len(svrsUnderTest))
|
var mergedProfiles = make([][]*cover.Profile, 0)
|
||||||
for _, svrs := range svrsUnderTest {
|
for _, svrs := range svrsUnderTest {
|
||||||
for _, addr := range svrs {
|
for _, addr := range svrs {
|
||||||
pp, err := NewWorker(addr).Profile(ProfileParam{})
|
pp, err := NewWorker(addr).Profile(ProfileParam{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if !force {
|
||||||
|
|
||||||
c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()})
|
c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
profile, err := convertProfile(pp)
|
profile, err := convertProfile(pp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if !force {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
mergedProfiles = append(mergedProfiles, profile)
|
mergedProfiles = append(mergedProfiles, profile)
|
||||||
}
|
}
|
||||||
@ -253,3 +259,12 @@ func convertProfile(p []byte) ([]*cover.Profile, error) {
|
|||||||
|
|
||||||
return cover.ParseProfiles(tf.Name())
|
return cover.ParseProfiles(tf.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func contains(arr []string, str string) bool {
|
||||||
|
for _, element := range arr {
|
||||||
|
if str == element {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -71,8 +72,11 @@ func NewFileStore() Store {
|
|||||||
|
|
||||||
// Add adds the given service to file Store
|
// Add adds the given service to file Store
|
||||||
func (l *fileStore) Add(s Service) error {
|
func (l *fileStore) Add(s Service) error {
|
||||||
l.memoryStore.Add(s)
|
err := l.memoryStore.Add(s)
|
||||||
|
log.Println(errors.Is(err, ErrStoreDuplicated))
|
||||||
|
if errors.Is(err, ErrStoreDuplicated) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
// persistent to local store
|
// persistent to local store
|
||||||
l.mu.Lock()
|
l.mu.Lock()
|
||||||
defer l.mu.Unlock()
|
defer l.mu.Unlock()
|
||||||
@ -193,7 +197,7 @@ func (l *memoryStore) Add(s Service) error {
|
|||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
if addr == s.Address {
|
if addr == s.Address {
|
||||||
log.Printf("service registered already, name: %s, address: %s", s.Name, s.Address)
|
log.Printf("service registered already, name: %s, address: %s", s.Name, s.Address)
|
||||||
return nil
|
return ErrStoreDuplicated
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
addrs = append(addrs, s.Address)
|
addrs = append(addrs, s.Address)
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
package cover
|
package cover
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -42,6 +43,8 @@ func TestLocalStore(t *testing.T) {
|
|||||||
localStore.Add(tc2)
|
localStore.Add(tc2)
|
||||||
localStore.Add(tc3)
|
localStore.Add(tc3)
|
||||||
localStore.Add(tc4)
|
localStore.Add(tc4)
|
||||||
|
err := localStore.Add(tc1)
|
||||||
|
assert.NoError(t, err)
|
||||||
addrs := localStore.Get(tc1.Name)
|
addrs := localStore.Get(tc1.Name)
|
||||||
if len(addrs) != 2 {
|
if len(addrs) != 2 {
|
||||||
t.Error("unexpected result")
|
t.Error("unexpected result")
|
||||||
@ -62,3 +65,21 @@ func TestLocalStore(t *testing.T) {
|
|||||||
t.Error("local store init failed")
|
t.Error("local store init failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMemoryStore(t *testing.T) {
|
||||||
|
memoryStore := NewMemoryStore()
|
||||||
|
var tc1 = Service{
|
||||||
|
Name: "a",
|
||||||
|
Address: "http://127.0.0.1",
|
||||||
|
}
|
||||||
|
err := memoryStore.Add(tc1)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("add server to memory store failed")
|
||||||
|
}
|
||||||
|
addrs := memoryStore.Get(tc1.Name)
|
||||||
|
if len(addrs) != 1 {
|
||||||
|
t.Error("memory store check failed")
|
||||||
|
}
|
||||||
|
err = memoryStore.Add(tc1)
|
||||||
|
assert.Equal(t, err, ErrStoreDuplicated)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user