From 50ad4c6e32da496acec8b08a7a8019f03e494c85 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Wed, 8 Dec 2021 13:25:28 +0800 Subject: [PATCH] higher coverage --- dtmcli/dtmimp/db_special.go | 2 +- dtmcli/dtmimp/db_special_test.go | 2 ++ dtmcli/dtmimp/utils.go | 4 ++++ dtmcli/dtmimp/utils_test.go | 5 ++++ dtmcli/types.go | 2 +- dtmcli/types_test.go | 5 ++++ test/main_test.go | 2 -- test/tcc_cover_test.go | 30 ++++++++++++++++++++++++ test/tcc_grpc_cover.go | 29 +++++++++++++++++++++++ test/xa_cover_test.go | 40 ++++++++++++++++++++++++++++++++ 10 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 test/tcc_cover_test.go create mode 100644 test/tcc_grpc_cover.go create mode 100644 test/xa_cover_test.go diff --git a/dtmcli/dtmimp/db_special.go b/dtmcli/dtmimp/db_special.go index 7306742..3eb3071 100644 --- a/dtmcli/dtmimp/db_special.go +++ b/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{} diff --git a/dtmcli/dtmimp/db_special_test.go b/dtmcli/dtmimp/db_special_test.go index d43daf5..3bf7012 100644 --- a/dtmcli/dtmimp/db_special_test.go +++ b/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) } diff --git a/dtmcli/dtmimp/utils.go b/dtmcli/dtmimp/utils.go index 3b01261..f2d77dd 100644 --- a/dtmcli/dtmimp/utils.go +++ b/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") != "" { diff --git a/dtmcli/dtmimp/utils_test.go b/dtmcli/dtmimp/utils_test.go index a58acdb..1625ced 100644 --- a/dtmcli/dtmimp/utils_test.go +++ b/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() +} diff --git a/dtmcli/types.go b/dtmcli/types.go index 64b4ab6..c478334 100644 --- a/dtmcli/types.go +++ b/dtmcli/types.go @@ -39,7 +39,7 @@ func GetCurrentDBType() string { } // SetXaSqlTimeoutMs set XaSqlTimeoutMs -func XaSqlTimeoutMs(ms int) { +func SetXaSqlTimeoutMs(ms int) { dtmimp.XaSqlTimeoutMs = ms } diff --git a/dtmcli/types_test.go b/dtmcli/types_test.go index 451c911..849fe57 100644 --- a/dtmcli/types_test.go +++ b/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) +} diff --git a/test/main_test.go b/test/main_test.go index 658831e..251d4e5 100644 --- a/test/main_test.go +++ b/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 diff --git a/test/tcc_cover_test.go b/test/tcc_cover_test.go new file mode 100644 index 0000000..12e5a32 --- /dev/null +++ b/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") +} diff --git a/test/tcc_grpc_cover.go b/test/tcc_grpc_cover.go new file mode 100644 index 0000000..3cecef2 --- /dev/null +++ b/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") +} diff --git a/test/xa_cover_test.go b/test/xa_cover_test.go new file mode 100644 index 0000000..65ad5c9 --- /dev/null +++ b/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 +}