Repository: zeromicro/go-zero
Pull request #1604 (2022-03-02 13:03:16)

test: add more tests


Patch:
diff --git a/core/stores/mongoc/cachedcollection.go b/core/stores/mongoc/cachedcollection.go
index 49fdd203..3b4742fb 100644
--- a/core/stores/mongoc/cachedcollection.go
+++ b/core/stores/mongoc/cachedcollection.go
@@ -12,8 +12,8 @@ var (
 	ErrNotFound = mgo.ErrNotFound
 
 	// can't use one SingleFlight per conn, because multiple conns may share the same cache key.
-	sharedCalls = syncx.NewSingleFlight()
-	stats       = cache.NewStat("mongoc")
+	singleFlight = syncx.NewSingleFlight()
+	stats        = cache.NewStat("mongoc")
 )
 
 type (
diff --git a/core/stores/mongoc/cachedmodel.go b/core/stores/mongoc/cachedmodel.go
index c366cc3c..919afcea 100644
--- a/core/stores/mongoc/cachedmodel.go
+++ b/core/stores/mongoc/cachedmodel.go
@@ -38,7 +38,7 @@ func MustNewModel(url, collection string, c cache.CacheConf, opts ...cache.Optio
 
 // NewModel returns a Model with a cache cluster.
 func NewModel(url, collection string, conf cache.CacheConf, opts ...cache.Option) (*Model, error) {
-	c := cache.New(conf, sharedCalls, stats, mgo.ErrNotFound, opts...)
+	c := cache.New(conf, singleFlight, stats, mgo.ErrNotFound, opts...)
 	return NewModelWithCache(url, collection, c)
 }
 
@@ -51,7 +51,7 @@ func NewModelWithCache(url, collection string, c cache.Cache) (*Model, error) {
 
 // NewNodeModel returns a Model with a cache node.
 func NewNodeModel(url, collection string, rds *redis.Redis, opts ...cache.Option) (*Model, error) {
-	c := cache.NewNode(rds, sharedCalls, stats, mgo.ErrNotFound, opts...)
+	c := cache.NewNode(rds, singleFlight, stats, mgo.ErrNotFound, opts...)
 	return NewModelWithCache(url, collection, c)
 }
 
diff --git a/readme-cn.md b/readme-cn.md
index da868a2a..6b385602 100644
--- a/readme-cn.md
+++ b/readme-cn.md
@@ -252,6 +252,8 @@ go-zero 已被许多公司用于生产部署，接入场景如在线教育、电
 >56. 时代脉搏网络科技（云浮市）有限公司
 >57. 店有帮
 >58. 七牛云
+>59. 费芮网络
+>60. 51CTO
 
 如果贵公司也已使用 go-zero，欢迎在 [登记地址](https://github.com/zeromicro/go-zero/issues/602) 登记，仅仅为了推广，不做其它用途。
 
diff --git a/zrpc/proxy.go b/zrpc/proxy.go
index d02f7ff4..7bb997f8 100644
--- a/zrpc/proxy.go
+++ b/zrpc/proxy.go
@@ -12,20 +12,20 @@ import (
 
 // A RpcProxy is a rpc proxy.
 type RpcProxy struct {
-	backend     string
-	clients     map[string]Client
-	options     []internal.ClientOption
-	sharedCalls syncx.SingleFlight
-	lock        sync.Mutex
+	backend      string
+	clients      map[string]Client
+	options      []internal.ClientOption
+	singleFlight syncx.SingleFlight
+	lock         sync.Mutex
 }
 
 // NewProxy returns a RpcProxy.
 func NewProxy(backend string, opts ...internal.ClientOption) *RpcProxy {
 	return &RpcProxy{
-		backend:     backend,
-		clients:     make(map[string]Client),
-		options:     opts,
-		sharedCalls: syncx.NewSingleFlight(),
+		backend:      backend,
+		clients:      make(map[string]Client),
+		options:      opts,
+		singleFlight: syncx.NewSingleFlight(),
 	}
 }
 
@@ -33,7 +33,7 @@ func NewProxy(backend string, opts ...internal.ClientOption) *RpcProxy {
 func (p *RpcProxy) TakeConn(ctx context.Context) (*grpc.ClientConn, error) {
 	cred := auth.ParseCredential(ctx)
 	key := cred.App + "/" + cred.Token
-	val, err := p.sharedCalls.Do(key, func() (interface{}, error) {
+	val, err := p.singleFlight.Do(key, func() (interface{}, error) {
 		p.lock.Lock()
 		client, ok := p.clients[key]
 		p.lock.Unlock()


Test patch:
diff --git a/core/stores/mongoc/cachedcollection_test.go b/core/stores/mongoc/cachedcollection_test.go
index 6f839b13..f7eaa254 100644
--- a/core/stores/mongoc/cachedcollection_test.go
+++ b/core/stores/mongoc/cachedcollection_test.go
@@ -34,7 +34,7 @@ func TestCollection_Count(t *testing.T) {
 	assert.Nil(t, err)
 	defer clean()
 
-	cach := cache.NewNode(r, sharedCalls, stats, mgo.ErrNotFound)
+	cach := cache.NewNode(r, singleFlight, stats, mgo.ErrNotFound)
 	c := newCollection(dummyConn{}, cach)
 	val, err := c.Count("any")
 	assert.Nil(t, err)
@@ -98,7 +98,7 @@ func TestStat(t *testing.T) {
 	assert.Nil(t, err)
 	defer clean()
 
-	cach := cache.NewNode(r, sharedCalls, stats, mgo.ErrNotFound)
+	cach := cache.NewNode(r, singleFlight, stats, mgo.ErrNotFound)
 	c := newCollection(dummyConn{}, cach).(*cachedCollection)
 
 	for i := 0; i < 10; i++ {
@@ -121,7 +121,7 @@ func TestStatCacheFails(t *testing.T) {
 	defer log.SetOutput(os.Stdout)
 
 	r := redis.New("localhost:59999")
-	cach := cache.NewNode(r, sharedCalls, stats, mgo.ErrNotFound)
+	cach := cache.NewNode(r, singleFlight, stats, mgo.ErrNotFound)
 	c := newCollection(dummyConn{}, cach)
 
 	for i := 0; i < 20; i++ {
@@ -142,7 +142,7 @@ func TestStatDbFails(t *testing.T) {
 	assert.Nil(t, err)
 	defer clean()
 
-	cach := cache.NewNode(r, sharedCalls, stats, mgo.ErrNotFound)
+	cach := cache.NewNode(r, singleFlight, stats, mgo.ErrNotFound)
 	c := newCollection(dummyConn{}, cach).(*cachedCollection)
 
 	for i := 0; i < 20; i++ {
@@ -164,7 +164,7 @@ func TestStatFromMemory(t *testing.T) {
 	assert.Nil(t, err)
 	defer clean()
 
-	cach := cache.NewNode(r, sharedCalls, stats, mgo.ErrNotFound)
+	cach := cache.NewNode(r, singleFlight, stats, mgo.ErrNotFound)
 	c := newCollection(dummyConn{}, cach).(*cachedCollection)
 
 	var all sync.WaitGroup
diff --git a/zrpc/proxy_test.go b/zrpc/proxy_test.go
index 97a24fe4..935914a4 100644
--- a/zrpc/proxy_test.go
+++ b/zrpc/proxy_test.go
@@ -64,3 +64,9 @@ func TestProxy(t *testing.T) {
 		})
 	}
 }
+
+func TestRpcProxy_TakeConnNewClientFailed(t *testing.T) {
+	proxy := NewProxy("foo", WithDialOption(grpc.WithInsecure()), WithDialOption(grpc.WithBlock()))
+	_, err := proxy.TakeConn(context.Background())
+	assert.NotNil(t, err)
+}
diff --git a/zrpc/server_test.go b/zrpc/server_test.go
index 5dad1b99..14360ccc 100644
--- a/zrpc/server_test.go
+++ b/zrpc/server_test.go
@@ -101,6 +101,21 @@ func TestServer_HasEtcd(t *testing.T) {
 	srv.Stop()
 }
 
+func TestServer_StartFailed(t *testing.T) {
+	srv := MustNewServer(RpcServerConf{
+		ServiceConf: service.ServiceConf{
+			Log: logx.LogConf{
+				ServiceName: "foo",
+				Mode:        "console",
+			},
+		},
+		ListenOn: "localhost:aaa",
+	}, func(server *grpc.Server) {
+	})
+
+	assert.Panics(t, srv.Start)
+}
+
 type mockedServer struct {
 	unaryInterceptors  []grpc.UnaryServerInterceptor
 	streamInterceptors []grpc.StreamServerInterceptor