Browse Source

optimize test

pull/43/head
yedf2 5 years ago
parent
commit
d385b5ef60
  1. 3
      dtmcli/barrier.go
  2. 7
      dtmcli/utils.go
  3. 17
      test/barrier_tcc_test.go

3
dtmcli/barrier.go

@ -57,9 +57,6 @@ func insertBarrier(tx Tx, transType string, gid string, branchID string, branchT
func (bb *BranchBarrier) Call(tx Tx, busiCall BusiFunc) (rerr error) {
bb.BarrierID = bb.BarrierID + 1
bid := fmt.Sprintf("%02d", bb.BarrierID)
if rerr != nil {
return
}
defer func() {
// Logf("barrier call error is %v", rerr)
if x := recover(); x != nil {

7
dtmcli/utils.go

@ -226,13 +226,6 @@ func DBExec(db DB, sql string, values ...interface{}) (affected int64, rerr erro
return
}
// DBQueryRow use raw tx to query row
func DBQueryRow(db DB, query string, args ...interface{}) *sql.Row {
query = makeSQLCompatible(query)
Logf("querying: "+query, args...)
return db.QueryRow(query, args...)
}
// GetDsn get dsn from map config
func GetDsn(conf map[string]string) string {
host := MayReplaceLocalhost(conf["host"])

17
test/barrier_tcc_test.go

@ -1,6 +1,8 @@
package test
import (
"context"
"database/sql"
"fmt"
"strings"
"testing"
@ -18,7 +20,7 @@ func TestBarrierTcc(t *testing.T) {
tccBarrierDisorder(t)
tccBarrierNormal(t)
tccBarrierRollback(t)
barrierPanic(t)
}
func tccBarrierRollback(t *testing.T) {
@ -115,3 +117,16 @@ func tccBarrierDisorder(t *testing.T) {
assert.Equal(t, []string{dtmcli.StatusSucceed, dtmcli.StatusPrepared, dtmcli.StatusPrepared}, getBranchesStatus(gid))
assert.Equal(t, dtmcli.StatusFailed, getTransStatus(gid))
}
func barrierPanic(t *testing.T) {
bb := &dtmcli.BranchBarrier{TransType: "saga", Gid: "gid1", BranchID: "bid1", BranchType: "action", BarrierID: 1}
var err error
func() {
defer dtmcli.P2E(&err)
tx, _ := dbGet().ToSQLDB().BeginTx(context.Background(), &sql.TxOptions{})
bb.Call(tx, func(db dtmcli.DB) error {
panic(fmt.Errorf("an error"))
})
}()
assert.Error(t, err, fmt.Errorf("an error"))
}

Loading…
Cancel
Save