mirror of https://github.com/dtm-labs/dtm.git
csharpjavadistributed-transactionsdtmgogolangmicroservicenodejsphpdatabasesagaseatatcctransactiontransactionsxapythondistributed
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.8 KiB
96 lines
2.8 KiB
package test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/yedf/dtm/common"
|
|
"github.com/yedf/dtm/dtmcli"
|
|
"github.com/yedf/dtm/dtmsvr"
|
|
"github.com/yedf/dtm/examples"
|
|
)
|
|
|
|
var DtmServer = examples.DtmServer
|
|
var Busi = examples.Busi
|
|
var app *gin.Engine
|
|
|
|
// BarrierModel barrier model for gorm
|
|
type BarrierModel struct {
|
|
common.ModelBase
|
|
dtmcli.BranchBarrier
|
|
}
|
|
|
|
// TableName gorm table name
|
|
func (BarrierModel) TableName() string { return "dtm_barrier.barrier" }
|
|
|
|
func getTransStatus(gid string) string {
|
|
sm := TransGlobal{}
|
|
dbr := dbGet().Model(&sm).Where("gid=?", gid).First(&sm)
|
|
e2p(dbr.Error)
|
|
return sm.Status
|
|
}
|
|
|
|
func getBranchesStatus(gid string) []string {
|
|
branches := []TransBranch{}
|
|
dbr := dbGet().Model(&TransBranch{}).Where("gid=?", gid).Order("id").Find(&branches)
|
|
e2p(dbr.Error)
|
|
status := []string{}
|
|
for _, branch := range branches {
|
|
status = append(status, branch.Status)
|
|
}
|
|
return status
|
|
}
|
|
|
|
func assertSucceed(t *testing.T, gid string) {
|
|
WaitTransProcessed(gid)
|
|
assert.Equal(t, dtmcli.StatusSucceed, getTransStatus(gid))
|
|
}
|
|
|
|
func genMsg(gid string) *dtmcli.Msg {
|
|
dtmcli.Logf("beginning a msg test ---------------- %s", gid)
|
|
msg := dtmcli.NewMsg(examples.DtmServer, gid)
|
|
msg.QueryPrepared = examples.Busi + "/CanSubmit"
|
|
req := examples.GenTransReq(30, false, false)
|
|
msg.Add(examples.Busi+"/TransOut", &req)
|
|
msg.Add(examples.Busi+"/TransIn", &req)
|
|
return msg
|
|
}
|
|
|
|
func transQuery(t *testing.T, gid string) {
|
|
resp, err := dtmcli.RestyClient.R().SetQueryParam("gid", gid).Get(examples.DtmServer + "/query")
|
|
e2p(err)
|
|
m := M{}
|
|
assert.Equal(t, resp.StatusCode(), 200)
|
|
dtmcli.MustUnmarshalString(resp.String(), &m)
|
|
assert.NotEqual(t, nil, m["transaction"])
|
|
assert.Equal(t, 4, len(m["branches"].([]interface{})))
|
|
|
|
resp, err = dtmcli.RestyClient.R().SetQueryParam("gid", "").Get(examples.DtmServer + "/query")
|
|
e2p(err)
|
|
assert.Equal(t, resp.StatusCode(), 500)
|
|
|
|
resp, err = dtmcli.RestyClient.R().SetQueryParam("gid", "1").Get(examples.DtmServer + "/query")
|
|
e2p(err)
|
|
assert.Equal(t, resp.StatusCode(), 200)
|
|
dtmcli.MustUnmarshalString(resp.String(), &m)
|
|
assert.Equal(t, nil, m["transaction"])
|
|
assert.Equal(t, 0, len(m["branches"].([]interface{})))
|
|
|
|
resp, err = dtmcli.RestyClient.R().Get(examples.DtmServer + "/all")
|
|
assert.Nil(t, err)
|
|
}
|
|
|
|
func TestUpdateBranchAsync(t *testing.T) {
|
|
common.DtmConfig.UpdateBranchSync = 0
|
|
saga := genSaga("gid-update-branch-async", false, false)
|
|
saga.SetOptions(&dtmcli.TransOptions{WaitResult: true})
|
|
err := saga.Submit()
|
|
assert.Nil(t, err)
|
|
WaitTransProcessed(saga.Gid)
|
|
time.Sleep(dtmsvr.UpdateBranchAsyncInterval)
|
|
assert.Equal(t, []string{dtmcli.StatusPrepared, dtmcli.StatusSucceed, dtmcli.StatusPrepared, dtmcli.StatusSucceed}, getBranchesStatus(saga.Gid))
|
|
assert.Equal(t, dtmcli.StatusSucceed, getTransStatus(saga.Gid))
|
|
common.DtmConfig.UpdateBranchSync = 1
|
|
}
|
|
|