Skip to content

Commit 468d88a

Browse files
committed
add extra execute context function for compatibility
1 parent 86519a8 commit 468d88a

8 files changed

+39
-29
lines changed

client/workflow/workflow.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,36 @@ func Register2(name string, handler WfFunc2, custom ...func(wf *Workflow)) error
5353
return defaultFac.register(name, handler, custom...)
5454
}
5555

56-
// Execute will execute a workflow with the gid and specified params
56+
// Execute is the same as ExecuteCtx, but with context.Background
57+
func Execute(name string, gid string, data []byte) error {
58+
return ExecuteCtx(context.Background(), name, gid, data)
59+
}
60+
61+
// ExecuteCtx will execute a workflow with the gid and specified params
5762
// if the workflow with the gid does not exist, then create a new workflow and execute it
5863
// if the workflow with the gid exists, resume to execute it
59-
func Execute(ctx context.Context, name string, gid string, data []byte) error {
64+
func ExecuteCtx(ctx context.Context, name string, gid string, data []byte) error {
6065
_, err := defaultFac.execute(ctx, name, gid, data)
6166
return err
6267
}
6368

6469
// Execute2 is the same as Execute, but workflow func can return result
65-
func Execute2(ctx context.Context, name string, gid string, data []byte) ([]byte, error) {
70+
func Execute2(name string, gid string, data []byte) ([]byte, error) {
71+
return Execute2Ctx(context.Background(), name, gid, data)
72+
}
73+
74+
// Execute2Ctx is the same as Execute2, but with context.Background
75+
func Execute2Ctx(ctx context.Context, name string, gid string, data []byte) ([]byte, error) {
6676
return defaultFac.execute(ctx, name, gid, data)
6777
}
6878

6979
// ExecuteByQS is like Execute, but name and gid will be obtained from qs
70-
func ExecuteByQS(ctx context.Context, qs url.Values, body []byte) error {
80+
func ExecuteByQS(qs url.Values, body []byte) error {
81+
return ExecuteByQSCtx(context.Background(), qs, body)
82+
}
83+
84+
// ExecuteByQSCtx is the same as ExecuteByQS, but with context.Background
85+
func ExecuteByQSCtx(ctx context.Context, qs url.Values, body []byte) error {
7186
name := qs.Get("op")
7287
gid := qs.Get("gid")
7388
_, err := defaultFac.execute(ctx, name, gid, body)

test/busi/base_http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func BaseAddRoute(app *gin.Engine) {
8888
app.POST(BusiAPI+"/workflow/resume", dtmutil.WrapHandler(func(ctx *gin.Context) interface{} {
8989
data, err := ioutil.ReadAll(ctx.Request.Body)
9090
logger.FatalIfError(err)
91-
return workflow.ExecuteByQS(ctx, ctx.Request.URL.Query(), data)
91+
return workflow.ExecuteByQS(ctx.Request.URL.Query(), data)
9292
}))
9393
app.POST(BusiAPI+"/TransIn", dtmutil.WrapHandler(func(c *gin.Context) interface{} {
9494
return handleGeneralBusiness(c, MainSwitch.TransInResult.Fetch(), reqFrom(c).TransInResult, "transIn")

test/dtmsvr_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestUpdateBranchAsync(t *testing.T) {
7070
return err
7171
})
7272
assert.Nil(t, err)
73-
err = workflow.Execute(context.Background(), gid, gid, nil)
73+
err = workflow.Execute(gid, gid, nil)
7474
assert.Nil(t, err)
7575

7676
time.Sleep(dtmsvr.UpdateBranchAsyncInterval)

test/workflow_grpc_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package test
88

99
import (
10-
"context"
1110
"database/sql"
1211
"testing"
1312

@@ -34,7 +33,7 @@ func TestWorkflowGrpcSimple(t *testing.T) {
3433
_, err = busi.BusiCli.TransInBSaga(wf.NewBranchCtx(), &req)
3534
return err
3635
})
37-
err := workflow.Execute(context.Background(), gid, gid, dtmgimp.MustProtoMarshal(req))
36+
err := workflow.Execute(gid, gid, dtmgimp.MustProtoMarshal(req))
3837
assert.Error(t, err)
3938
assert.Equal(t, StatusFailed, getTransStatus(gid))
4039
}
@@ -62,7 +61,7 @@ func TestWorkflowGrpcRollback(t *testing.T) {
6261
return err
6362
})
6463
before := getBeforeBalances("mysql")
65-
err := workflow.Execute(context.Background(), gid, gid, dtmgimp.MustProtoMarshal(req))
64+
err := workflow.Execute(gid, gid, dtmgimp.MustProtoMarshal(req))
6665
assert.Error(t, err, dtmcli.ErrFailure)
6766
assert.Equal(t, StatusFailed, getTransStatus(gid))
6867
assertSameBalance(t, before, "mysql")
@@ -107,7 +106,7 @@ func TestWorkflowMixed(t *testing.T) {
107106
assert.Nil(t, err)
108107
before := getBeforeBalances("mysql")
109108
req := &busi.ReqGrpc{Amount: 30}
110-
err = workflow.Execute(context.Background(), gid, gid, dtmgimp.MustProtoMarshal(req))
109+
err = workflow.Execute(gid, gid, dtmgimp.MustProtoMarshal(req))
111110
assert.Nil(t, err)
112111
assert.Equal(t, StatusSucceed, getTransStatus(gid))
113112
assertNotSameBalance(t, before, "mysql")
@@ -128,7 +127,7 @@ func TestWorkflowGrpcError(t *testing.T) {
128127
_, err = busi.BusiCli.TransIn(wf.NewBranchCtx(), &req)
129128
return err
130129
})
131-
err := workflow.Execute(context.Background(), gid, gid, dtmgimp.MustProtoMarshal(req))
130+
err := workflow.Execute(gid, gid, dtmgimp.MustProtoMarshal(req))
132131
assert.Error(t, err)
133132
cronTransOnceForwardCron(t, gid, 1000)
134133
assert.Equal(t, StatusSucceed, getTransStatus(gid))

test/workflow_http_ret_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package test
22

33
import (
4-
"context"
54
"testing"
65

76
"github.com/dtm-labs/dtm/client/dtmcli/dtmimp"
@@ -22,13 +21,13 @@ func TestWorkflowRet(t *testing.T) {
2221
return []byte("result of workflow"), err
2322
})
2423

25-
ret, err := workflow.Execute2(context.Background(), gid, gid, dtmimp.MustMarshal(req))
24+
ret, err := workflow.Execute2(gid, gid, dtmimp.MustMarshal(req))
2625
assert.Nil(t, err)
2726
assert.Equal(t, "result of workflow", string(ret))
2827
assert.Equal(t, StatusSucceed, getTransStatus(gid))
2928

3029
// the second execute will return result directly
31-
ret, err = workflow.Execute2(context.Background(), gid, gid, dtmimp.MustMarshal(req))
30+
ret, err = workflow.Execute2(gid, gid, dtmimp.MustMarshal(req))
3231
assert.Nil(t, err)
3332
assert.Equal(t, "result of workflow", string(ret))
3433
assert.Equal(t, StatusSucceed, getTransStatus(gid))

test/workflow_http_test.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package test
88

99
import (
10-
"context"
1110
"database/sql"
1211
"testing"
1312

@@ -42,7 +41,7 @@ func TestWorkflowNormal(t *testing.T) {
4241
return nil
4342
})
4443

45-
err := workflow.Execute(context.Background(), gid, gid, dtmimp.MustMarshal(req))
44+
err := workflow.Execute(gid, gid, dtmimp.MustMarshal(req))
4645
assert.Nil(t, err)
4746
assert.Equal(t, StatusSucceed, getTransStatus(gid))
4847
}
@@ -83,7 +82,7 @@ func TestWorkflowRollback(t *testing.T) {
8382
})
8483
before := getBeforeBalances("mysql")
8584

86-
err := workflow.Execute(context.Background(), gid, gid, dtmimp.MustMarshal(req))
85+
err := workflow.Execute(gid, gid, dtmimp.MustMarshal(req))
8786
assert.Error(t, err, dtmcli.ErrFailure)
8887
assert.Equal(t, StatusFailed, getTransStatus(gid))
8988
assertSameBalance(t, before, "mysql")
@@ -121,7 +120,7 @@ func TestWorkflowTcc(t *testing.T) {
121120
})
122121

123122
before := getBeforeBalances("mysql")
124-
err := workflow.Execute(context.Background(), gid, gid, dtmimp.MustMarshal(req))
123+
err := workflow.Execute(gid, gid, dtmimp.MustMarshal(req))
125124
assert.Nil(t, err)
126125
assert.Equal(t, StatusSucceed, getTransStatus(gid))
127126
assertNotSameBalance(t, before, "mysql")
@@ -159,7 +158,7 @@ func TestWorkflowTccRollback(t *testing.T) {
159158
})
160159

161160
before := getBeforeBalances("mysql")
162-
err := workflow.Execute(context.Background(), gid, gid, dtmimp.MustMarshal(req))
161+
err := workflow.Execute(gid, gid, dtmimp.MustMarshal(req))
163162
assert.Error(t, err)
164163
assert.Equal(t, StatusFailed, getTransStatus(gid))
165164
assertSameBalance(t, before, "mysql")
@@ -178,7 +177,7 @@ func TestWorkflowError(t *testing.T) {
178177
return err
179178
})
180179

181-
err := workflow.Execute(context.Background(), gid, gid, dtmimp.MustMarshal(req))
180+
err := workflow.Execute(gid, gid, dtmimp.MustMarshal(req))
182181
assert.Error(t, err)
183182
cronTransOnceForwardCron(t, gid, 1000)
184183
assert.Equal(t, StatusSucceed, getTransStatus(gid))
@@ -197,7 +196,7 @@ func TestWorkflowOngoing(t *testing.T) {
197196
return err
198197
})
199198

200-
err := workflow.Execute(context.Background(), gid, gid, dtmimp.MustMarshal(req))
199+
err := workflow.Execute(gid, gid, dtmimp.MustMarshal(req))
201200
assert.Error(t, err)
202201
cronTransOnceForwardCron(t, gid, 1000)
203202
assert.Equal(t, StatusSucceed, getTransStatus(gid))
@@ -225,7 +224,7 @@ func TestWorkflowResumeSkip(t *testing.T) {
225224
return err
226225
})
227226

228-
err := workflow.Execute(context.Background(), gid, gid, dtmimp.MustMarshal(req))
227+
err := workflow.Execute(gid, gid, dtmimp.MustMarshal(req))
229228
assert.Error(t, err)
230229
cronTransOnceForwardCron(t, gid, 1000)
231230
assert.Equal(t, StatusSucceed, getTransStatus(gid))

test/workflow_ongoing_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package test
88

99
import (
10-
"context"
1110
"database/sql"
1211
"testing"
1312

@@ -48,7 +47,7 @@ func TestWorkflowSimpleResume(t *testing.T) {
4847
return err
4948
})
5049

51-
err := workflow.Execute(context.Background(), gid, gid, dtmimp.MustMarshal(req))
50+
err := workflow.Execute(gid, gid, dtmimp.MustMarshal(req))
5251
assert.Error(t, err)
5352
cronTransOnceForwardNow(t, gid, 1000)
5453
assert.Equal(t, StatusSucceed, getTransStatus(gid))
@@ -95,7 +94,7 @@ func TestWorkflowGrpcRollbackResume(t *testing.T) {
9594
})
9695
before := getBeforeBalances("mysql")
9796
req := &busi.ReqGrpc{Amount: 30, TransInResult: "FAILURE"}
98-
err := workflow.Execute(context.Background(), gid, gid, dtmgimp.MustProtoMarshal(req))
97+
err := workflow.Execute(gid, gid, dtmgimp.MustProtoMarshal(req))
9998
assert.Error(t, err, dtmcli.ErrOngoing)
10099
assert.Equal(t, StatusPrepared, getTransStatus(gid))
101100
cronTransOnceForwardNow(t, gid, 1000)
@@ -141,7 +140,7 @@ func TestWorkflowXaResume(t *testing.T) {
141140
return err
142141
})
143142
before := getBeforeBalances("mysql")
144-
err := workflow.Execute(context.Background(), gid, gid, nil)
143+
err := workflow.Execute(gid, gid, nil)
145144
assert.Equal(t, dtmcli.ErrOngoing, err)
146145

147146
cronTransOnceForwardNow(t, gid, 1000)

test/workflow_xa_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package test
88

99
import (
10-
"context"
1110
"database/sql"
1211
"testing"
1312

@@ -35,7 +34,7 @@ func TestWorkflowXaAction(t *testing.T) {
3534
return err
3635
})
3736
before := getBeforeBalances("mysql")
38-
err := workflow.Execute(context.Background(), gid, gid, nil)
37+
err := workflow.Execute(gid, gid, nil)
3938
assert.Nil(t, err)
4039
assert.Equal(t, StatusSucceed, getTransStatus(gid))
4140
assertNotSameBalance(t, before, "mysql")
@@ -59,7 +58,7 @@ func TestWorkflowXaRollback(t *testing.T) {
5958
return err
6059
})
6160
before := getBeforeBalances("mysql")
62-
err := workflow.Execute(context.Background(), gid, gid, nil)
61+
err := workflow.Execute(gid, gid, nil)
6362
assert.Equal(t, dtmcli.ErrFailure, err)
6463
assert.Equal(t, StatusFailed, getTransStatus(gid))
6564
assertSameBalance(t, before, "mysql")

0 commit comments

Comments
 (0)