chore: add more tests (#3014)

master
Kevin Wan 2 years ago committed by GitHub
parent 544aa7c432
commit f77e2c9cfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -13,7 +13,7 @@ var (
type EtcdConf struct {
Hosts []string
Key string
ServerID int64 `json:",optional"`
ID int64 `json:",optional"`
User string `json:",optional"`
Pass string `json:",optional"`
CertFile string `json:",optional"`
@ -27,9 +27,9 @@ func (c EtcdConf) HasAccount() bool {
return len(c.User) > 0 && len(c.Pass) > 0
}
// HasServerID returns if ServerID provided.
func (c EtcdConf) HasServerID() bool {
return c.ServerID > 0
// HasID returns if ID provided.
func (c EtcdConf) HasID() bool {
return c.ID > 0
}
// HasTLS returns if TLS CertFile/CertKeyFile/CACertFile are provided.

@ -81,35 +81,89 @@ func TestEtcdConf_HasAccount(t *testing.T) {
}
}
func TestEtcdConf_HasServerID(t *testing.T) {
func TestEtcdConf_HasID(t *testing.T) {
tests := []struct {
EtcdConf
hasServerID bool
}{
{
EtcdConf: EtcdConf{
Hosts: []string{"any"},
ServerID: -1,
Hosts: []string{"any"},
ID: -1,
},
hasServerID: false,
},
{
EtcdConf: EtcdConf{
Hosts: []string{"any"},
ServerID: 0,
Hosts: []string{"any"},
ID: 0,
},
hasServerID: false,
},
{
EtcdConf: EtcdConf{
Hosts: []string{"any"},
ServerID: 10000,
Hosts: []string{"any"},
ID: 10000,
},
hasServerID: true,
},
}
for _, test := range tests {
assert.Equal(t, test.hasServerID, test.EtcdConf.HasServerID())
assert.Equal(t, test.hasServerID, test.EtcdConf.HasID())
}
}
func TestEtcdConf_HasTLS(t *testing.T) {
tests := []struct {
name string
conf EtcdConf
want bool
}{
{
name: "empty config",
conf: EtcdConf{},
want: false,
},
{
name: "missing CertFile",
conf: EtcdConf{
CertKeyFile: "key",
CACertFile: "ca",
},
want: false,
},
{
name: "missing CertKeyFile",
conf: EtcdConf{
CertFile: "cert",
CACertFile: "ca",
},
want: false,
},
{
name: "missing CACertFile",
conf: EtcdConf{
CertFile: "cert",
CertKeyFile: "key",
},
want: false,
},
{
name: "valid config",
conf: EtcdConf{
CertFile: "cert",
CertKeyFile: "key",
CACertFile: "ca",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.conf.HasTLS()
assert.Equal(t, tt.want, got)
})
}
}

@ -76,9 +76,9 @@ func TestGenCacheKeys(t *testing.T) {
VarExpression: `cacheGoZeroUserIdPrefix = "cache:goZero:user:id:"`,
KeyLeft: "goZeroUserIdKey",
KeyRight: `fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, id)`,
DataKeyRight: `fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, data.Id)`,
DataKeyRight: `fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, data.ID)`,
KeyExpression: `goZeroUserIdKey := fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, id)`,
DataKeyExpression: `goZeroUserIdKey := fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, data.Id)`,
DataKeyExpression: `goZeroUserIdKey := fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, data.ID)`,
FieldNameJoin: []string{"id"},
})
}())
@ -170,9 +170,9 @@ func TestGenCacheKeys(t *testing.T) {
VarExpression: `cacheUserIdPrefix = "cache:user:id:"`,
KeyLeft: "userIdKey",
KeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`,
DataKeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)`,
DataKeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, data.ID)`,
KeyExpression: `userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`,
DataKeyExpression: `userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)`,
DataKeyExpression: `userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, data.ID)`,
FieldNameJoin: []string{"id"},
})
}())

@ -2,7 +2,6 @@ package clientinterceptors
import (
"context"
"github.com/zeromicro/go-zero/core/lang"
"io"
ztrace "github.com/zeromicro/go-zero/core/trace"
@ -95,7 +94,7 @@ type (
Finished chan error
desc *grpc.StreamDesc
events chan streamEvent
eventsDone chan lang.PlaceholderType
eventsDone chan struct{}
receivedMessageID int
sentMessageID int
}

@ -26,8 +26,8 @@ func NewRpcPubServer(etcd discov.EtcdConf, listenOn string, middlewares ServerMi
pubOpts = append(pubOpts, discov.WithPubEtcdTLS(etcd.CertFile, etcd.CertKeyFile,
etcd.CACertFile, etcd.InsecureSkipVerify))
}
if etcd.HasServerID() {
pubOpts = append(pubOpts, discov.WithId(etcd.ServerID))
if etcd.HasID() {
pubOpts = append(pubOpts, discov.WithId(etcd.ID))
}
pubClient := discov.NewPublisher(etcd.Hosts, etcd.Key, pubListenOn, pubOpts...)
return pubClient.KeepAlive()

@ -12,6 +12,7 @@ func TestNewRpcPubServer(t *testing.T) {
s, err := NewRpcPubServer(discov.EtcdConf{
User: "user",
Pass: "pass",
ID: 10,
}, "", ServerMiddlewaresConf{})
assert.NoError(t, err)
assert.NotPanics(t, func() {

Loading…
Cancel
Save