fix: improve fuzzing coverage for jsn package
This commit is contained in:
@ -18,6 +18,7 @@ import (
|
||||
"github.com/dosco/super-graph/core"
|
||||
"github.com/gosimple/slug"
|
||||
"github.com/jackc/pgx/v4"
|
||||
"github.com/jackc/pgx/v4/stdlib"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -200,12 +201,12 @@ func importCSV(table, filename string) int64 {
|
||||
cols = append(cols, c.(string))
|
||||
}
|
||||
|
||||
conn, err := acquireConn(db)
|
||||
conn, err := stdlib.AcquireConn(db)
|
||||
if err != nil {
|
||||
log.Fatalf("ERR %v", err)
|
||||
}
|
||||
//nolint: errcheck
|
||||
defer releaseConn(db, conn)
|
||||
defer stdlib.ReleaseConn(db, conn)
|
||||
|
||||
n, err := conn.CopyFrom(
|
||||
context.Background(),
|
||||
|
@ -1,67 +0,0 @@
|
||||
package serv
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
errors "golang.org/x/xerrors"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
type ctxKey int
|
||||
|
||||
var ctxKeyFakeTx ctxKey = 0
|
||||
|
||||
var errNotPgx = errors.New("not pgx *sql.DB")
|
||||
|
||||
var (
|
||||
fakeTxMutex sync.Mutex
|
||||
fakeTxConns map[*pgx.Conn]*sql.Tx
|
||||
)
|
||||
|
||||
func acquireConn(db *sql.DB) (*pgx.Conn, error) {
|
||||
var conn *pgx.Conn
|
||||
ctx := context.WithValue(context.Background(), ctxKeyFakeTx, &conn)
|
||||
tx, err := db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if conn == nil {
|
||||
if err := tx.Rollback(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, errNotPgx
|
||||
}
|
||||
|
||||
fakeTxMutex.Lock()
|
||||
fakeTxConns[conn] = tx
|
||||
fakeTxMutex.Unlock()
|
||||
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func releaseConn(db *sql.DB, conn *pgx.Conn) error {
|
||||
var tx *sql.Tx
|
||||
var ok bool
|
||||
|
||||
if conn.PgConn().IsBusy() || conn.PgConn().TxStatus() != 'I' {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
conn.Close(ctx)
|
||||
}
|
||||
|
||||
fakeTxMutex.Lock()
|
||||
tx, ok = fakeTxConns[conn]
|
||||
if ok {
|
||||
delete(fakeTxConns, conn)
|
||||
fakeTxMutex.Unlock()
|
||||
} else {
|
||||
fakeTxMutex.Unlock()
|
||||
return errors.Errorf("can't release conn that is not acquired")
|
||||
}
|
||||
|
||||
return tx.Rollback()
|
||||
}
|
Reference in New Issue
Block a user