Skip to content

Commit

Permalink
feature: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stone-afk committed Apr 3, 2023
1 parent fe22dba commit 49f77a3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
10 changes: 5 additions & 5 deletions internal/datasource/cluster/cluster_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func (c *clusterDB) Exec(ctx context.Context, query datasource.Query) (sql.Resul
}

func (c *clusterDB) Close() error {
var resErrs []error
var err error
for name, inst := range c.masterSlavesDBs {
err := inst.Close()
if err != nil {
resErrs = append(resErrs, fmt.Errorf("DB name [%s] error: %w", name, err))
if er := inst.Close(); er != nil {
err = multierr.Combine(
err, fmt.Errorf("masterslave DB name [%s] error: %w", name, er))
}
}
return multierr.Combine(resErrs...)
return err
}

func NewClusterDB(ms map[string]*masterslave.MasterSlavesDB) datasource.DataSource {
Expand Down
13 changes: 7 additions & 6 deletions internal/datasource/masterslave/master_slave_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,17 @@ func NewMasterSlavesDB(master *sql.DB, opts ...MasterSlavesDBOption) *MasterSlav
}

func (m *MasterSlavesDB) Close() error {
var resErrs []error
if err := m.master.Close(); err != nil {
resErrs = append(resErrs, fmt.Errorf("master error: %w", err))
var err error
if er := m.master.Close(); er != nil {
err = multierr.Combine(
err, fmt.Errorf("master error: %w", er))
}
if m.slaves != nil {
if err := m.slaves.Close(); err != nil {
resErrs = append(resErrs, err)
if er := m.slaves.Close(); er != nil {
err = multierr.Combine(err, er)
}
}
return multierr.Combine(resErrs...)
return err
}

type MasterSlavesDBOption func(db *MasterSlavesDB)
Expand Down
10 changes: 5 additions & 5 deletions internal/datasource/masterslave/slaves/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ func (s *Slaves) Close() error {
}

func (s *Slaves) closeDB() error {
var resErrs []error
var err error
for _, inst := range s.slaves {
err := inst.Close()
if err != nil {
resErrs = append(resErrs, fmt.Errorf("slave DB name [%s] error: %w", inst.SlaveName, err))
if er := inst.Close(); er != nil {
err = multierr.Combine(
err, fmt.Errorf("slave DB name [%s] error: %w", inst.SlaveName, er))
}
}
return multierr.Combine(resErrs...)
return err
}
10 changes: 5 additions & 5 deletions internal/datasource/masterslave/slaves/roundrobin/roundrobin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ func (r *Slaves) Next(ctx context.Context) (slaves.Slave, error) {
}

func (r *Slaves) Close() error {
var resErrs []error
var err error
for _, inst := range r.slaves {
err := inst.Close()
if err != nil {
resErrs = append(resErrs, fmt.Errorf("slave DB name [%s] error: %w", inst.SlaveName, err))
if er := inst.Close(); er != nil {
err = multierr.Combine(
err, fmt.Errorf("slave DB name [%s] error: %w", inst.SlaveName, er))
}
}
return multierr.Combine(resErrs...)
return err
}

func NewSlaves(dbs ...*sql.DB) (*Slaves, error) {
Expand Down
10 changes: 5 additions & 5 deletions internal/datasource/shardingsource/sharding_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func NewShardingDataSource(m map[string]datasource.DataSource) datasource.DataSo
}

func (s *ShardingDataSource) Close() error {
var resErrs []error
var err error
for name, inst := range s.sources {
err := inst.Close()
if err != nil {
resErrs = append(resErrs, fmt.Errorf("source name [%s] error: %w", name, err))
if er := inst.Close(); er != nil {
err = multierr.Combine(
err, fmt.Errorf("source name [%s] error: %w", name, er))
}
}
return multierr.Combine(resErrs...)
return err
}

0 comments on commit 49f77a3

Please sign in to comment.