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 { type EtcdConf struct {
Hosts []string Hosts []string
Key string Key string
ServerID int64 `json:",optional"` ID int64 `json:",optional"`
User string `json:",optional"` User string `json:",optional"`
Pass string `json:",optional"` Pass string `json:",optional"`
CertFile 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 return len(c.User) > 0 && len(c.Pass) > 0
} }
// HasServerID returns if ServerID provided. // HasID returns if ID provided.
func (c EtcdConf) HasServerID() bool { func (c EtcdConf) HasID() bool {
return c.ServerID > 0 return c.ID > 0
} }
// HasTLS returns if TLS CertFile/CertKeyFile/CACertFile are provided. // HasTLS returns if TLS CertFile/CertKeyFile/CACertFile are provided.

@ -81,7 +81,7 @@ func TestEtcdConf_HasAccount(t *testing.T) {
} }
} }
func TestEtcdConf_HasServerID(t *testing.T) { func TestEtcdConf_HasID(t *testing.T) {
tests := []struct { tests := []struct {
EtcdConf EtcdConf
hasServerID bool hasServerID bool
@ -89,27 +89,81 @@ func TestEtcdConf_HasServerID(t *testing.T) {
{ {
EtcdConf: EtcdConf{ EtcdConf: EtcdConf{
Hosts: []string{"any"}, Hosts: []string{"any"},
ServerID: -1, ID: -1,
}, },
hasServerID: false, hasServerID: false,
}, },
{ {
EtcdConf: EtcdConf{ EtcdConf: EtcdConf{
Hosts: []string{"any"}, Hosts: []string{"any"},
ServerID: 0, ID: 0,
}, },
hasServerID: false, hasServerID: false,
}, },
{ {
EtcdConf: EtcdConf{ EtcdConf: EtcdConf{
Hosts: []string{"any"}, Hosts: []string{"any"},
ServerID: 10000, ID: 10000,
}, },
hasServerID: true, hasServerID: true,
}, },
} }
for _, test := range tests { 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:"`, VarExpression: `cacheGoZeroUserIdPrefix = "cache:goZero:user:id:"`,
KeyLeft: "goZeroUserIdKey", KeyLeft: "goZeroUserIdKey",
KeyRight: `fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, id)`, 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)`, 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"}, FieldNameJoin: []string{"id"},
}) })
}()) }())
@ -170,9 +170,9 @@ func TestGenCacheKeys(t *testing.T) {
VarExpression: `cacheUserIdPrefix = "cache:user:id:"`, VarExpression: `cacheUserIdPrefix = "cache:user:id:"`,
KeyLeft: "userIdKey", KeyLeft: "userIdKey",
KeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`, 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)`, 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"}, FieldNameJoin: []string{"id"},
}) })
}()) }())

@ -2,7 +2,6 @@ package clientinterceptors
import ( import (
"context" "context"
"github.com/zeromicro/go-zero/core/lang"
"io" "io"
ztrace "github.com/zeromicro/go-zero/core/trace" ztrace "github.com/zeromicro/go-zero/core/trace"
@ -95,7 +94,7 @@ type (
Finished chan error Finished chan error
desc *grpc.StreamDesc desc *grpc.StreamDesc
events chan streamEvent events chan streamEvent
eventsDone chan lang.PlaceholderType eventsDone chan struct{}
receivedMessageID int receivedMessageID int
sentMessageID 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, pubOpts = append(pubOpts, discov.WithPubEtcdTLS(etcd.CertFile, etcd.CertKeyFile,
etcd.CACertFile, etcd.InsecureSkipVerify)) etcd.CACertFile, etcd.InsecureSkipVerify))
} }
if etcd.HasServerID() { if etcd.HasID() {
pubOpts = append(pubOpts, discov.WithId(etcd.ServerID)) pubOpts = append(pubOpts, discov.WithId(etcd.ID))
} }
pubClient := discov.NewPublisher(etcd.Hosts, etcd.Key, pubListenOn, pubOpts...) pubClient := discov.NewPublisher(etcd.Hosts, etcd.Key, pubListenOn, pubOpts...)
return pubClient.KeepAlive() return pubClient.KeepAlive()

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

Loading…
Cancel
Save