test cover more
This commit is contained in:
parent
0f78db2b84
commit
17c8a3813e
@ -33,6 +33,8 @@ func TestDtmSvr(t *testing.T) {
|
|||||||
examples.TccSetup(app)
|
examples.TccSetup(app)
|
||||||
examples.XaSetup(app)
|
examples.XaSetup(app)
|
||||||
examples.MsgSetup(app)
|
examples.MsgSetup(app)
|
||||||
|
examples.TccBarrierAddRoute(app)
|
||||||
|
examples.SagaBarrierAddRoute(app)
|
||||||
|
|
||||||
// 清理数据
|
// 清理数据
|
||||||
e2p(dbGet().Exec("truncate trans_global").Error)
|
e2p(dbGet().Exec("truncate trans_global").Error)
|
||||||
@ -40,6 +42,8 @@ func TestDtmSvr(t *testing.T) {
|
|||||||
e2p(dbGet().Exec("truncate trans_log").Error)
|
e2p(dbGet().Exec("truncate trans_log").Error)
|
||||||
examples.ResetXaData()
|
examples.ResetXaData()
|
||||||
|
|
||||||
|
tccBarrierNormal(t)
|
||||||
|
sagaBarrierNormal(t)
|
||||||
msgNormal(t)
|
msgNormal(t)
|
||||||
msgPending(t)
|
msgPending(t)
|
||||||
tccNormal(t)
|
tccNormal(t)
|
||||||
@ -123,6 +127,24 @@ func tccNormal(t *testing.T) {
|
|||||||
})
|
})
|
||||||
e2p(err)
|
e2p(err)
|
||||||
}
|
}
|
||||||
|
func tccBarrierNormal(t *testing.T) {
|
||||||
|
_, err := dtmcli.TccGlobalTransaction(DtmServer, func(tcc *dtmcli.Tcc) (rerr error) {
|
||||||
|
res1, rerr := tcc.CallBranch(&examples.TransReq{Amount: 30}, Busi+"/TccBTransOutTry", Busi+"/TccBTransOutConfirm", Busi+"/TccBTransOutRevert")
|
||||||
|
e2p(rerr)
|
||||||
|
if res1.StatusCode() != 200 {
|
||||||
|
return fmt.Errorf("bad status code: %d", res1.StatusCode())
|
||||||
|
}
|
||||||
|
res2, rerr := tcc.CallBranch(&examples.TransReq{Amount: 30}, Busi+"/TccBTransInTry", Busi+"/TccBTransInConfirm", Busi+"/TccBTransInRevert")
|
||||||
|
e2p(rerr)
|
||||||
|
if res2.StatusCode() != 200 {
|
||||||
|
return fmt.Errorf("bad status code: %d", res2.StatusCode())
|
||||||
|
}
|
||||||
|
logrus.Printf("tcc returns: %s, %s", res1.String(), res2.String())
|
||||||
|
return
|
||||||
|
})
|
||||||
|
e2p(err)
|
||||||
|
}
|
||||||
|
|
||||||
func tccRollback(t *testing.T) {
|
func tccRollback(t *testing.T) {
|
||||||
data := &examples.TransReq{Amount: 30, TransInResult: "FAIL"}
|
data := &examples.TransReq{Amount: 30, TransInResult: "FAIL"}
|
||||||
_, err := dtmcli.TccGlobalTransaction(examples.DtmServer, func(tcc *dtmcli.Tcc) (rerr error) {
|
_, err := dtmcli.TccGlobalTransaction(examples.DtmServer, func(tcc *dtmcli.Tcc) (rerr error) {
|
||||||
@ -167,6 +189,18 @@ func sagaNormal(t *testing.T) {
|
|||||||
transQuery(t, saga.Gid)
|
transQuery(t, saga.Gid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sagaBarrierNormal(t *testing.T) {
|
||||||
|
req := &examples.TransReq{Amount: 30}
|
||||||
|
saga := dtmcli.NewSaga(DtmServer).
|
||||||
|
Add(Busi+"/SagaBTransOut", Busi+"/SagaBTransOutCompensate", req).
|
||||||
|
Add(Busi+"/SagaBTransIn", Busi+"/SagaBTransInCompensate", req)
|
||||||
|
logrus.Printf("busi trans submit")
|
||||||
|
err := saga.Submit()
|
||||||
|
e2p(err)
|
||||||
|
WaitTransProcessed(saga.Gid)
|
||||||
|
assert.Equal(t, []string{"prepared", "succeed", "prepared", "succeed"}, getBranchesStatus(saga.Gid))
|
||||||
|
}
|
||||||
|
|
||||||
func sagaRollback(t *testing.T) {
|
func sagaRollback(t *testing.T) {
|
||||||
saga := genSaga("gid-rollbackSaga2", false, true)
|
saga := genSaga("gid-rollbackSaga2", false, true)
|
||||||
saga.Submit()
|
saga.Submit()
|
||||||
|
|||||||
@ -34,5 +34,5 @@ func StartSvr() {
|
|||||||
func PopulateMysql() {
|
func PopulateMysql() {
|
||||||
common.InitApp(common.GetProjectDir(), &config)
|
common.InitApp(common.GetProjectDir(), &config)
|
||||||
config.Mysql["database"] = ""
|
config.Mysql["database"] = ""
|
||||||
examples.RunSqlScript(config.Mysql, common.GetCurrentCodeDir()+"/dtmsvr.sql")
|
examples.RunSQLScript(config.Mysql, common.GetCurrentCodeDir()+"/dtmsvr.sql")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,6 @@ type exampleConfig struct {
|
|||||||
Mysql map[string]string `yaml:"Mysql"`
|
Mysql map[string]string `yaml:"Mysql"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var Config = exampleConfig{}
|
var config = exampleConfig{}
|
||||||
|
|
||||||
var dbName = "dtm_busi"
|
var dbName = "dtm_busi"
|
||||||
|
|||||||
@ -8,8 +8,8 @@ import (
|
|||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RunSqlScript 1
|
// RunSQLScript 1
|
||||||
func RunSqlScript(mysql map[string]string, script string) {
|
func RunSQLScript(mysql map[string]string, script string) {
|
||||||
conf := map[string]string{}
|
conf := map[string]string{}
|
||||||
common.MustRemarshal(mysql, &conf)
|
common.MustRemarshal(mysql, &conf)
|
||||||
conf["database"] = ""
|
conf["database"] = ""
|
||||||
@ -32,7 +32,7 @@ func RunSqlScript(mysql map[string]string, script string) {
|
|||||||
|
|
||||||
// PopulateMysql populate example mysql data
|
// PopulateMysql populate example mysql data
|
||||||
func PopulateMysql() {
|
func PopulateMysql() {
|
||||||
common.InitApp(common.GetProjectDir(), &Config)
|
common.InitApp(common.GetProjectDir(), &config)
|
||||||
Config.Mysql["database"] = dbName
|
config.Mysql["database"] = dbName
|
||||||
RunSqlScript(Config.Mysql, common.GetCurrentCodeDir()+"/examples.sql")
|
RunSQLScript(config.Mysql, common.GetCurrentCodeDir()+"/examples.sql")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,7 @@ var XaClient *dtmcli.XaClient = nil
|
|||||||
// UserAccount busi model
|
// UserAccount busi model
|
||||||
type UserAccount struct {
|
type UserAccount struct {
|
||||||
common.ModelBase
|
common.ModelBase
|
||||||
UserId int
|
UserID int
|
||||||
Balance string
|
Balance string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ func (u *UserAccount) TableName() string { return "user_account" }
|
|||||||
// UserAccountTrading freeze user account table
|
// UserAccountTrading freeze user account table
|
||||||
type UserAccountTrading struct {
|
type UserAccountTrading struct {
|
||||||
common.ModelBase
|
common.ModelBase
|
||||||
UserId int
|
UserID int
|
||||||
TradingBalance string
|
TradingBalance string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ type UserAccountTrading struct {
|
|||||||
func (u *UserAccountTrading) TableName() string { return "user_account_trading" }
|
func (u *UserAccountTrading) TableName() string { return "user_account_trading" }
|
||||||
|
|
||||||
func dbGet() *common.DB {
|
func dbGet() *common.DB {
|
||||||
return common.DbGet(Config.Mysql)
|
return common.DbGet(config.Mysql)
|
||||||
}
|
}
|
||||||
|
|
||||||
// XaFireRequest 1
|
// XaFireRequest 1
|
||||||
@ -54,8 +54,8 @@ func XaFireRequest() {
|
|||||||
func XaSetup(app *gin.Engine) {
|
func XaSetup(app *gin.Engine) {
|
||||||
app.POST(BusiAPI+"/TransInXa", common.WrapHandler(xaTransIn))
|
app.POST(BusiAPI+"/TransInXa", common.WrapHandler(xaTransIn))
|
||||||
app.POST(BusiAPI+"/TransOutXa", common.WrapHandler(xaTransOut))
|
app.POST(BusiAPI+"/TransOutXa", common.WrapHandler(xaTransOut))
|
||||||
Config.Mysql["database"] = "dtm_busi"
|
config.Mysql["database"] = "dtm_busi"
|
||||||
XaClient = dtmcli.NewXaClient(DtmServer, Config.Mysql, app, Busi+"/xa")
|
XaClient = dtmcli.NewXaClient(DtmServer, config.Mysql, app, Busi+"/xa")
|
||||||
}
|
}
|
||||||
|
|
||||||
func xaTransIn(c *gin.Context) (interface{}, error) {
|
func xaTransIn(c *gin.Context) (interface{}, error) {
|
||||||
|
|||||||
@ -16,13 +16,6 @@ type M = map[string]interface{}
|
|||||||
// DtmServer dtm service address
|
// DtmServer dtm service address
|
||||||
const DtmServer = "http://localhost:8080/api/dtmsvr"
|
const DtmServer = "http://localhost:8080/api/dtmsvr"
|
||||||
|
|
||||||
const (
|
|
||||||
// SagaBarrierBusiPort saga barrier sample port
|
|
||||||
SagaBarrierBusiPort = iota + 8090
|
|
||||||
// TccBarrierBusiPort tcc barrier sample port
|
|
||||||
TccBarrierBusiPort
|
|
||||||
)
|
|
||||||
|
|
||||||
// TransReq transaction request payload
|
// TransReq transaction request payload
|
||||||
type TransReq struct {
|
type TransReq struct {
|
||||||
Amount int `json:"amount"`
|
Amount int `json:"amount"`
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user