Repository: zeromicro/go-zero
Pull request #2897 (2023-02-18 03:55:08)

refactor: simplify sqlx fail fast ping and  simplify miniredis setup in test

codecov[bot]: # [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/2897?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#2897](https://codecov.io/gh/zeromicro/go-zero/pull/2897?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (f637b27) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/1dd18e23292dbba66dcf64c070d354352f571a8c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (1dd18e2) will **increase** coverage by `0.00%`.
> The diff coverage is `44.44%`.


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/2897?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/stores/sqlx/sqlmanager.go](https://codecov.io/gh/zeromicro/go-zero/pull/2897?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdG9yZXMvc3FseC9zcWxtYW5hZ2VyLmdv) | `67.74% <44.44%> (-0.83%)` | :arrow_down: |


Patch:
diff --git a/core/stores/sqlx/sqlmanager.go b/core/stores/sqlx/sqlmanager.go
index b2c1e169..db652edc 100644
--- a/core/stores/sqlx/sqlmanager.go
+++ b/core/stores/sqlx/sqlmanager.go
@@ -3,7 +3,6 @@ package sqlx
 import (
 	"database/sql"
 	"io"
-	"sync"
 	"time"
 
 	"github.com/zeromicro/go-zero/core/syncx"
@@ -17,43 +16,29 @@ const (
 
 var connManager = syncx.NewResourceManager()
 
-type pingedDB struct {
-	*sql.DB
-	once sync.Once
-}
-
-func getCachedSqlConn(driverName, server string) (*pingedDB, error) {
+func getCachedSqlConn(driverName, server string) (*sql.DB, error) {
 	val, err := connManager.GetResource(server, func() (io.Closer, error) {
 		conn, err := newDBConnection(driverName, server)
 		if err != nil {
 			return nil, err
 		}
 
-		return &pingedDB{
-			DB: conn,
-		}, nil
+		return conn, nil
 	})
 	if err != nil {
 		return nil, err
 	}
 
-	return val.(*pingedDB), nil
+	return val.(*sql.DB), nil
 }
 
 func getSqlConn(driverName, server string) (*sql.DB, error) {
-	pdb, err := getCachedSqlConn(driverName, server)
+	conn, err := getCachedSqlConn(driverName, server)
 	if err != nil {
 		return nil, err
 	}
 
-	pdb.once.Do(func() {
-		err = pdb.Ping()
-	})
-	if err != nil {
-		return nil, err
-	}
-
-	return pdb.DB, nil
+	return conn, nil
 }
 
 func newDBConnection(driverName, datasource string) (*sql.DB, error) {
@@ -70,5 +55,10 @@ func newDBConnection(driverName, datasource string) (*sql.DB, error) {
 	conn.SetMaxOpenConns(maxOpenConns)
 	conn.SetConnMaxLifetime(maxLifetime)
 
+	if err := conn.Ping(); err != nil {
+		_ = conn.Close()
+		return nil, err
+	}
+
 	return conn, nil
 }


Test patch:
diff --git a/core/stores/redis/redis_test.go b/core/stores/redis/redis_test.go
index 7a83b6f4..88ddd48b 100644
--- a/core/stores/redis/redis_test.go
+++ b/core/stores/redis/redis_test.go
@@ -1761,21 +1761,7 @@ func TestRedis_WithPass(t *testing.T) {
 func runOnRedis(t *testing.T, fn func(client *Redis)) {
 	logx.Disable()
 
-	s, err := miniredis.Run()
-	assert.Nil(t, err)
-	defer func() {
-		client, err := clientManager.GetResource(s.Addr(), func() (io.Closer, error) {
-			return nil, errors.New("should already exist")
-		})
-		if err != nil {
-			t.Error(err)
-		}
-
-		if client != nil {
-			_ = client.Close()
-		}
-	}()
-
+	s := miniredis.RunT(t)
 	fn(MustNewRedis(RedisConf{
 		Host: s.Addr(),
 		Type: NodeType,
@@ -1785,21 +1771,7 @@ func runOnRedis(t *testing.T, fn func(client *Redis)) {
 func runOnRedisWithError(t *testing.T, fn func(client *Redis)) {
 	logx.Disable()
 
-	s, err := miniredis.Run()
-	assert.NoError(t, err)
-	defer func() {
-		client, err := clientManager.GetResource(s.Addr(), func() (io.Closer, error) {
-			return nil, errors.New("should already exist")
-		})
-		if err != nil {
-			t.Error(err)
-		}
-
-		if client != nil {
-			_ = client.Close()
-		}
-	}()
-
+	s := miniredis.RunT(t)
 	s.SetError("mock error")
 	fn(New(s.Addr()))
 }
diff --git a/core/stores/sqlx/sqlconn_test.go b/core/stores/sqlx/sqlconn_test.go
index 8c68c5e6..50dd23c8 100644
--- a/core/stores/sqlx/sqlconn_test.go
+++ b/core/stores/sqlx/sqlconn_test.go
@@ -52,14 +52,11 @@ func TestSqlConn(t *testing.T) {
 }
 
 func buildConn() (mock sqlmock.Sqlmock, err error) {
-	_, err = connManager.GetResource(mockedDatasource, func() (io.Closer, error) {
+	connManager.GetResource(mockedDatasource, func() (io.Closer, error) {
 		var db *sql.DB
 		var err error
 		db, mock, err = sqlmock.New()
-		return &pingedDB{
-			DB: db,
-		}, err
+		return db, err
 	})
-
 	return
 }