|
|
@ -36,7 +36,7 @@ func TestDtmSvr(t *testing.T) { |
|
|
e2p(dbGet().Exec("truncate trans_branch").Error) |
|
|
e2p(dbGet().Exec("truncate trans_branch").Error) |
|
|
e2p(dbGet().Exec("truncate trans_log").Error) |
|
|
e2p(dbGet().Exec("truncate trans_log").Error) |
|
|
examples.ResetXaData() |
|
|
examples.ResetXaData() |
|
|
|
|
|
// tccNormal(t)
|
|
|
sagaCommittedPending(t) |
|
|
sagaCommittedPending(t) |
|
|
sagaPreparePending(t) |
|
|
sagaPreparePending(t) |
|
|
xaRollback(t) |
|
|
xaRollback(t) |
|
|
@ -95,7 +95,7 @@ func xaNormal(t *testing.T) { |
|
|
}) |
|
|
}) |
|
|
e2p(err) |
|
|
e2p(err) |
|
|
WaitTransProcessed(gid) |
|
|
WaitTransProcessed(gid) |
|
|
assert.Equal(t, []string{"finished", "finished"}, getBranchesStatus(gid)) |
|
|
assert.Equal(t, []string{"succeed", "succeed"}, getBranchesStatus(gid)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func xaRollback(t *testing.T) { |
|
|
func xaRollback(t *testing.T) { |
|
|
@ -119,68 +119,89 @@ func xaRollback(t *testing.T) { |
|
|
logrus.Errorf("global transaction failed, so rollback") |
|
|
logrus.Errorf("global transaction failed, so rollback") |
|
|
} |
|
|
} |
|
|
WaitTransProcessed(gid) |
|
|
WaitTransProcessed(gid) |
|
|
assert.Equal(t, []string{"rollbacked"}, getBranchesStatus(gid)) |
|
|
assert.Equal(t, []string{"failed"}, getBranchesStatus(gid)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func tccNormal(t *testing.T) { |
|
|
|
|
|
tcc := genTcc("gid-normal-tcc", false, false) |
|
|
|
|
|
tcc.Prepare(tcc.QueryPrepared) |
|
|
|
|
|
assert.Equal(t, "prepared", getSagaModel(tcc.Gid).Status) |
|
|
|
|
|
tcc.Commit() |
|
|
|
|
|
assert.Equal(t, "committed", getSagaModel(tcc.Gid).Status) |
|
|
|
|
|
WaitTransProcessed(tcc.Gid) |
|
|
|
|
|
assert.Equal(t, []string{"prepared", "succeed", "succeed", "prepared", "succeed", "succeed"}, getBranchesStatus(tcc.Gid)) |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
func sagaNormal(t *testing.T) { |
|
|
func sagaNormal(t *testing.T) { |
|
|
saga := genSaga("gid-noramlSaga", false, false) |
|
|
saga := genSaga("gid-noramlSaga", false, false) |
|
|
saga.Prepare() |
|
|
saga.Prepare(saga.QueryPrepared) |
|
|
assert.Equal(t, "prepared", getSagaModel(saga.Gid).Status) |
|
|
assert.Equal(t, "prepared", getSagaModel(saga.Gid).Status) |
|
|
saga.Commit() |
|
|
saga.Commit() |
|
|
assert.Equal(t, "committed", getSagaModel(saga.Gid).Status) |
|
|
assert.Equal(t, "committed", getSagaModel(saga.Gid).Status) |
|
|
WaitTransProcessed(saga.Gid) |
|
|
WaitTransProcessed(saga.Gid) |
|
|
assert.Equal(t, []string{"prepared", "finished", "prepared", "finished"}, getBranchesStatus(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.Commit() |
|
|
saga.Commit() |
|
|
WaitTransProcessed(saga.Gid) |
|
|
WaitTransProcessed(saga.Gid) |
|
|
saga.Prepare() |
|
|
saga.Prepare(saga.QueryPrepared) |
|
|
assert.Equal(t, "rollbacked", getSagaModel(saga.Gid).Status) |
|
|
assert.Equal(t, "failed", getSagaModel(saga.Gid).Status) |
|
|
assert.Equal(t, []string{"rollbacked", "finished", "rollbacked", "rollbacked"}, getBranchesStatus(saga.Gid)) |
|
|
assert.Equal(t, []string{"failed", "succeed", "failed", "failed"}, getBranchesStatus(saga.Gid)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func sagaPrepareCancel(t *testing.T) { |
|
|
func sagaPrepareCancel(t *testing.T) { |
|
|
saga := genSaga("gid1-prepareCancel", false, true) |
|
|
saga := genSaga("gid1-prepareCancel", false, true) |
|
|
saga.Prepare() |
|
|
saga.Prepare(saga.QueryPrepared) |
|
|
examples.TransQueryResult = "FAIL" |
|
|
examples.SagaTransQueryResult = "FAIL" |
|
|
config.PreparedExpire = -10 |
|
|
config.PreparedExpire = -10 |
|
|
CronTransOnce(-10*time.Second, "prepared") |
|
|
CronTransOnce(-10*time.Second, "prepared") |
|
|
examples.TransQueryResult = "" |
|
|
examples.SagaTransQueryResult = "" |
|
|
config.PreparedExpire = 60 |
|
|
config.PreparedExpire = 60 |
|
|
assert.Equal(t, "canceled", getSagaModel(saga.Gid).Status) |
|
|
assert.Equal(t, "canceled", getSagaModel(saga.Gid).Status) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func sagaPreparePending(t *testing.T) { |
|
|
func sagaPreparePending(t *testing.T) { |
|
|
saga := genSaga("gid1-preparePending", false, false) |
|
|
saga := genSaga("gid1-preparePending", false, false) |
|
|
saga.Prepare() |
|
|
saga.Prepare(saga.QueryPrepared) |
|
|
examples.TransQueryResult = "PENDING" |
|
|
examples.SagaTransQueryResult = "PENDING" |
|
|
CronTransOnce(-10*time.Second, "prepared") |
|
|
CronTransOnce(-10*time.Second, "prepared") |
|
|
examples.TransQueryResult = "" |
|
|
examples.SagaTransQueryResult = "" |
|
|
assert.Equal(t, "prepared", getSagaModel(saga.Gid).Status) |
|
|
assert.Equal(t, "prepared", getSagaModel(saga.Gid).Status) |
|
|
CronTransOnce(-10*time.Second, "prepared") |
|
|
CronTransOnce(-10*time.Second, "prepared") |
|
|
assert.Equal(t, "finished", getSagaModel(saga.Gid).Status) |
|
|
assert.Equal(t, "succeed", getSagaModel(saga.Gid).Status) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func sagaCommittedPending(t *testing.T) { |
|
|
func sagaCommittedPending(t *testing.T) { |
|
|
saga := genSaga("gid-committedPending", false, false) |
|
|
saga := genSaga("gid-committedPending", false, false) |
|
|
saga.Prepare() |
|
|
saga.Prepare(saga.QueryPrepared) |
|
|
examples.TransInResult = "PENDING" |
|
|
examples.SagaTransInResult = "PENDING" |
|
|
saga.Commit() |
|
|
saga.Commit() |
|
|
WaitTransProcessed(saga.Gid) |
|
|
WaitTransProcessed(saga.Gid) |
|
|
examples.TransInResult = "" |
|
|
examples.SagaTransInResult = "" |
|
|
assert.Equal(t, []string{"prepared", "finished", "prepared", "prepared"}, getBranchesStatus(saga.Gid)) |
|
|
assert.Equal(t, []string{"prepared", "succeed", "prepared", "prepared"}, getBranchesStatus(saga.Gid)) |
|
|
CronTransOnce(-10*time.Second, "committed") |
|
|
CronTransOnce(-10*time.Second, "committed") |
|
|
assert.Equal(t, []string{"prepared", "finished", "prepared", "finished"}, getBranchesStatus(saga.Gid)) |
|
|
assert.Equal(t, []string{"prepared", "succeed", "prepared", "succeed"}, getBranchesStatus(saga.Gid)) |
|
|
assert.Equal(t, "finished", getSagaModel(saga.Gid).Status) |
|
|
assert.Equal(t, "succeed", getSagaModel(saga.Gid).Status) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func genSaga(gid string, outFailed bool, inFailed bool) *dtm.Saga { |
|
|
func genSaga(gid string, outFailed bool, inFailed bool) *dtm.Saga { |
|
|
logrus.Printf("beginning a saga test ---------------- %s", gid) |
|
|
logrus.Printf("beginning a saga test ---------------- %s", gid) |
|
|
saga := dtm.SagaNew(examples.DtmServer, gid, examples.SagaBusi+"/TransQuery") |
|
|
saga := dtm.SagaNew(examples.DtmServer, gid) |
|
|
|
|
|
saga.QueryPrepared = examples.SagaBusi + "/TransQuery" |
|
|
req := examples.GenTransReq(30, outFailed, inFailed) |
|
|
req := examples.GenTransReq(30, outFailed, inFailed) |
|
|
saga.Add(examples.SagaBusi+"/TransOut", examples.SagaBusi+"/TransOutCompensate", &req) |
|
|
saga.Add(examples.SagaBusi+"/TransOut", examples.SagaBusi+"/TransOutCompensate", &req) |
|
|
saga.Add(examples.SagaBusi+"/TransIn", examples.SagaBusi+"/TransInCompensate", &req) |
|
|
saga.Add(examples.SagaBusi+"/TransIn", examples.SagaBusi+"/TransInCompensate", &req) |
|
|
return saga |
|
|
return saga |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func genTcc(gid string, outFailed bool, inFailed bool) *dtm.Tcc { |
|
|
|
|
|
logrus.Printf("beginning a saga test ---------------- %s", gid) |
|
|
|
|
|
tcc := dtm.TccNew(examples.DtmServer, gid) |
|
|
|
|
|
tcc.QueryPrepared = examples.TccBusi + "/TransQuery" |
|
|
|
|
|
req := examples.GenTransReq(30, outFailed, inFailed) |
|
|
|
|
|
tcc.Add(examples.TccBusi+"/TransOutTry", examples.TccBusi+"/TransOutConfirm", examples.TccBusi+"/TransOutCancel", &req) |
|
|
|
|
|
tcc.Add(examples.TccBusi+"/TransInTry", examples.TccBusi+"/TransInConfirm", examples.TccBusi+"/TransInCancel", &req) |
|
|
|
|
|
return tcc |
|
|
|
|
|
} |
|
|
|