Browse Source

higher coverage

pull/77/head
yedf2 4 years ago
parent
commit
50ad4c6e32
  1. 2
      dtmcli/dtmimp/db_special.go
  2. 2
      dtmcli/dtmimp/db_special_test.go
  3. 4
      dtmcli/dtmimp/utils.go
  4. 5
      dtmcli/dtmimp/utils_test.go
  5. 2
      dtmcli/types.go
  6. 5
      dtmcli/types_test.go
  7. 2
      test/main_test.go
  8. 30
      test/tcc_cover_test.go
  9. 29
      test/tcc_grpc_cover.go
  10. 40
      test/xa_cover_test.go

2
dtmcli/dtmimp/db_special.go

@ -77,7 +77,7 @@ func (*postgresDBSpecial) GetPlaceHoldSQL(sql string) string {
}
func (*postgresDBSpecial) GetInsertIgnoreTemplate(tableAndValues string, pgConstraint string) string {
return fmt.Sprintf("insert into %s on conflict ON CONSTRAINT %s do nothing", tableAndValues, pgConstraint)
return fmt.Sprintf("insert into %s on conflict ON CONSTRAINT %s do nothing", tableAndValues, pgConstraint)
}
func init() {
dbSpecials[DBTypePostgres] = &postgresDBSpecial{}

2
dtmcli/dtmimp/db_special_test.go

@ -23,10 +23,12 @@ func TestDBSpecial(t *testing.T) {
assert.Equal(t, "? ?", sp.GetPlaceHoldSQL("? ?"))
assert.Equal(t, "xa start 'xa1'", sp.GetXaSQL("start", "xa1"))
assert.Equal(t, "date_add(now(), interval 1000 second)", sp.TimestampAdd(1000))
assert.Equal(t, "insert ignore into a(f) values(?)", sp.GetInsertIgnoreTemplate("a(f) values(?)", "c"))
SetCurrentDBType(DBTypePostgres)
sp = GetDBSpecial()
assert.Equal(t, "$1 $2", sp.GetPlaceHoldSQL("? ?"))
assert.Equal(t, "begin", sp.GetXaSQL("start", "xa1"))
assert.Equal(t, "current_timestamp + interval '1000 second'", sp.TimestampAdd(1000))
assert.Equal(t, "insert into a(f) values(?) on conflict ON CONSTRAINT c do nothing", sp.GetInsertIgnoreTemplate("a(f) values(?)", "c"))
SetCurrentDBType(old)
}

4
dtmcli/dtmimp/utils.go

@ -122,6 +122,10 @@ func MustRemarshal(from interface{}, to interface{}) {
var logger *zap.SugaredLogger = nil
func init() {
InitLog()
}
func InitLog() {
config := zap.NewProductionConfig()
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
if os.Getenv("DTM_DEBUG") != "" {

5
dtmcli/dtmimp/utils_test.go

@ -92,3 +92,8 @@ func TestFatal(t *testing.T) {
})
assert.Error(t, err, fmt.Errorf("fatal"))
}
func TestInitLog(t *testing.T) {
os.Setenv("DTM_DEBUG", "1")
InitLog()
}

2
dtmcli/types.go

@ -39,7 +39,7 @@ func GetCurrentDBType() string {
}
// SetXaSqlTimeoutMs set XaSqlTimeoutMs
func XaSqlTimeoutMs(ms int) {
func SetXaSqlTimeoutMs(ms int) {
dtmimp.XaSqlTimeoutMs = ms
}

5
dtmcli/types_test.go

@ -24,3 +24,8 @@ func TestTypes(t *testing.T) {
assert.Error(t, err)
}
func TestXaSqlTimeout(t *testing.T) {
old := GetXaSqlTimeoutMs()
SetXaSqlTimeoutMs(old)
}

2
test/main_test.go

@ -7,7 +7,6 @@
package test
import (
"os"
"testing"
"time"
@ -20,7 +19,6 @@ import (
func TestMain(m *testing.M) {
common.MustLoadConfig()
dtmcli.SetCurrentDBType(common.DtmConfig.DB["driver"])
os.Setenv("DTM_DEBUG", "1")
dtmsvr.TransProcessedTestChan = make(chan string, 1)
dtmsvr.NowForwardDuration = 0 * time.Second
dtmsvr.CronForwardDuration = 180 * time.Second

30
test/tcc_cover_test.go

@ -0,0 +1,30 @@
package test
import (
"testing"
"github.com/go-resty/resty/v2"
"github.com/stretchr/testify/assert"
"github.com/yedf/dtm/dtmcli"
"github.com/yedf/dtm/dtmcli/dtmimp"
"github.com/yedf/dtm/examples"
)
func TestTccCoverNotConnected(t *testing.T) {
gid := dtmimp.GetFuncName()
err := dtmcli.TccGlobalTransaction("localhost:01", gid, func(tcc *dtmcli.Tcc) (*resty.Response, error) {
return nil, nil
})
assert.Error(t, err)
}
func TestTccCoverPanic(t *testing.T) {
gid := dtmimp.GetFuncName()
err := dtmimp.CatchP(func() {
_ = dtmcli.TccGlobalTransaction(examples.DtmHttpServer, gid, func(tcc *dtmcli.Tcc) (*resty.Response, error) {
panic("user panic")
})
assert.FailNow(t, "not executed")
})
assert.Contains(t, err.Error(), "user panic")
}

29
test/tcc_grpc_cover.go

@ -0,0 +1,29 @@
package test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/yedf/dtm/dtmcli/dtmimp"
"github.com/yedf/dtm/dtmgrpc"
"github.com/yedf/dtm/examples"
)
func TestTccGrpcCoverNotConnected(t *testing.T) {
gid := dtmimp.GetFuncName()
err := dtmgrpc.TccGlobalTransaction("localhost:01", gid, func(tcc *dtmgrpc.TccGrpc) error {
return nil
})
assert.Error(t, err)
}
func TestTccGrpcCoverPanic(t *testing.T) {
gid := dtmimp.GetFuncName()
err := dtmimp.CatchP(func() {
_ = dtmgrpc.TccGlobalTransaction(examples.DtmHttpServer, gid, func(tcc *dtmgrpc.TccGrpc) error {
panic("user panic")
})
assert.FailNow(t, "not executed")
})
assert.Contains(t, err.Error(), "user panic")
}

40
test/xa_cover_test.go

@ -0,0 +1,40 @@
package test
import (
"testing"
"github.com/go-resty/resty/v2"
"github.com/stretchr/testify/assert"
"github.com/yedf/dtm/dtmcli"
"github.com/yedf/dtm/dtmcli/dtmimp"
"github.com/yedf/dtm/examples"
)
func TestXaCoverDBError(t *testing.T) {
oldDriver := getXc().Conf["driver"]
gid := dtmimp.GetFuncName()
err := getXc().XaGlobalTransaction(gid, func(xa *dtmcli.Xa) (*resty.Response, error) {
req := examples.GenTransReq(30, false, false)
_, err := xa.CallBranch(req, examples.Busi+"/TransOutXa")
assert.Nil(t, err)
getXc().Conf["driver"] = "no-driver"
_, err = xa.CallBranch(req, examples.Busi+"/TransInXa")
assert.Error(t, err)
return nil, err
})
assert.Error(t, err)
getXc().Conf["driver"] = oldDriver
cronTransOnceForwardNow(500) // rollback succeeded here
assert.Equal(t, StatusFailed, getTransStatus(gid))
}
func TestXaCoverDTMError(t *testing.T) {
oldServer := getXc().Server
getXc().Server = "localhost:01"
gid := dtmimp.GetFuncName()
err := getXc().XaGlobalTransaction(gid, func(xa *dtmcli.Xa) (*resty.Response, error) {
return nil, nil
})
assert.Error(t, err)
getXc().Server = oldServer
}
Loading…
Cancel
Save