mirror of https://github.com/dtm-labs/dtm.git
17 changed files with 394 additions and 138 deletions
@ -0,0 +1,63 @@ |
|||
package dtmsvr |
|||
|
|||
import ( |
|||
"fmt" |
|||
"testing" |
|||
|
|||
"github.com/stretchr/testify/assert" |
|||
"github.com/yedf/dtm/dtmcli" |
|||
"github.com/yedf/dtm/dtmgrpc" |
|||
"github.com/yedf/dtm/examples" |
|||
) |
|||
|
|||
func TestGrpcXa(t *testing.T) { |
|||
if config.DB["driver"] != "mysql" { |
|||
return |
|||
} |
|||
// xaGrpcLocalError(t)
|
|||
xaGrpcNormal(t) |
|||
xaGrpcRollback(t) |
|||
} |
|||
|
|||
func xaGrpcLocalError(t *testing.T) { |
|||
xc := examples.XaGrpcClient |
|||
err := xc.XaGlobalTransaction("xaGrpcLocalError", func(xa *dtmgrpc.XaGrpc) error { |
|||
return fmt.Errorf("an error") |
|||
}) |
|||
assert.Error(t, err, fmt.Errorf("an error")) |
|||
} |
|||
|
|||
func xaGrpcNormal(t *testing.T) { |
|||
xc := examples.XaGrpcClient |
|||
gid := "xaGrpcNormal" |
|||
err := xc.XaGlobalTransaction(gid, func(xa *dtmgrpc.XaGrpc) error { |
|||
req := dtmcli.MustMarshal(examples.GenTransReq(30, false, false)) |
|||
_, err := xa.CallBranch(req, examples.BusiGrpc+"/examples.Busi/TransOutXa") |
|||
if err != nil { |
|||
return err |
|||
} |
|||
_, err = xa.CallBranch(req, examples.BusiGrpc+"/examples.Busi/TransInXa") |
|||
return err |
|||
}) |
|||
assert.Equal(t, nil, err) |
|||
WaitTransProcessed(gid) |
|||
assert.Equal(t, []string{"prepared", "succeed", "prepared", "succeed"}, getBranchesStatus(gid)) |
|||
} |
|||
|
|||
func xaGrpcRollback(t *testing.T) { |
|||
xc := examples.XaGrpcClient |
|||
gid := "xaGrpcRollback" |
|||
err := xc.XaGlobalTransaction(gid, func(xa *dtmgrpc.XaGrpc) error { |
|||
req := dtmcli.MustMarshal(&examples.TransReq{Amount: 30, TransInResult: "FAILURE"}) |
|||
_, err := xa.CallBranch(req, examples.BusiGrpc+"/examples.Busi/TransOutXa") |
|||
if err != nil { |
|||
return err |
|||
} |
|||
_, err = xa.CallBranch(req, examples.BusiGrpc+"/examples.Busi/TransInXa") |
|||
return err |
|||
}) |
|||
assert.Error(t, err) |
|||
WaitTransProcessed(gid) |
|||
assert.Equal(t, []string{"succeed", "prepared"}, getBranchesStatus(gid)) |
|||
assert.Equal(t, "failed", getTransStatus(gid)) |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
package examples |
|||
|
|||
import ( |
|||
context "context" |
|||
|
|||
"github.com/yedf/dtm/dtmcli" |
|||
"github.com/yedf/dtm/dtmgrpc" |
|||
emptypb "google.golang.org/protobuf/types/known/emptypb" |
|||
) |
|||
|
|||
// XaGrpcClient XA client connection
|
|||
var XaGrpcClient *dtmgrpc.XaGrpcClient = nil |
|||
|
|||
// XaGrpcSetup 挂载http的api,创建XaClient
|
|||
func XaGrpcSetup() { |
|||
XaGrpcClient = dtmgrpc.NewXaGrpcClient(DtmGrpcServer, config.DB, BusiGrpc+"/examples.Busi/XaNotify") |
|||
} |
|||
|
|||
func (s *busiServer) XaNotify(ctx context.Context, in *dtmgrpc.BusiRequest) (*emptypb.Empty, error) { |
|||
err := XaGrpcClient.HandleCallback(in.Info.Gid, in.Info.BranchID, in.Info.BranchType) |
|||
return &emptypb.Empty{}, dtmgrpc.Result2Error(nil, err) |
|||
} |
|||
|
|||
// XaGrpcFireRequest 注册全局XA事务,调用XA的分支
|
|||
func XaGrpcFireRequest() string { |
|||
gid := dtmcli.MustGenGid(DtmServer) |
|||
busiData := dtmcli.MustMarshal(&TransReq{Amount: 30}) |
|||
err := XaGrpcClient.XaGlobalTransaction(gid, func(xa *dtmgrpc.XaGrpc) error { |
|||
_, err := xa.CallBranch(busiData, BusiGrpc+"/examples.Busi/TransOutXa") |
|||
if err != nil { |
|||
return err |
|||
} |
|||
_, err = xa.CallBranch(busiData, BusiGrpc+"/examples.Busi/TransInXa") |
|||
return err |
|||
}) |
|||
e2p(err) |
|||
return gid |
|||
} |
|||
Loading…
Reference in new issue