Browse Source

Merge pull request #77 from yedf/alpha

add more test
pull/80/head
yedf2 4 years ago
committed by GitHub
parent
commit
f6fab55eaf
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  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
      examples/base_http.go
  8. 2
      test/main_test.go
  9. 30
      test/tcc_cover_test.go
  10. 48
      test/tcc_grpc_cover_test.go
  11. 54
      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
examples/base_http.go

@ -112,7 +112,7 @@ func error2Resp(err error) (interface{}, error) {
return gin.H{"dtm_result": s}, nil
}
}
return nil, nil
return nil, err
}
// BaseAddRoute add base route handler

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")
}

48
test/tcc_grpc_cover_test.go

@ -0,0 +1,48 @@
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"
"google.golang.org/protobuf/types/known/emptypb"
)
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.DtmGrpcServer, gid, func(tcc *dtmgrpc.TccGrpc) error {
panic("user panic")
})
assert.FailNow(t, "not executed")
})
assert.Contains(t, err.Error(), "user panic")
}
func TestTccGrpcCoverCallBranch(t *testing.T) {
req := examples.GenBusiReq(30, false, false)
gid := dtmimp.GetFuncName()
err := dtmgrpc.TccGlobalTransaction(examples.DtmGrpcServer, gid, func(tcc *dtmgrpc.TccGrpc) error {
r := &emptypb.Empty{}
err := tcc.CallBranch(req, "not_exists://abc", examples.BusiGrpc+"/examples.Busi/TransOutConfirm", examples.BusiGrpc+"/examples.Busi/TransOutRevert", r)
assert.Error(t, err)
tcc.Dtm = "localhost:01"
err = tcc.CallBranch(req, examples.BusiGrpc+"/examples.Busi/TransOut", examples.BusiGrpc+"/examples.Busi/TransOutConfirm", examples.BusiGrpc+"/examples.Busi/TransOutRevert", r)
assert.Error(t, err)
return err
})
assert.Error(t, err)
}

54
test/xa_cover_test.go

@ -0,0 +1,54 @@
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)
getXc().Conf["driver"] = oldDriver // make abort succeed
return nil, err
})
assert.Error(t, err)
getXc().Conf["driver"] = "no-driver" // make xa rollback failed
waitTransProcessed(gid)
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
}
func TestXaCoverGidError(t *testing.T) {
gid := "errgid-' '"
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.Error(t, err)
return nil, err
})
assert.Error(t, err)
}
Loading…
Cancel
Save