diff --git a/src/Dtmcli/Msg/Msg.cs b/src/Dtmcli/Msg/Msg.cs index 520127a..4d93909 100644 --- a/src/Dtmcli/Msg/Msg.cs +++ b/src/Dtmcli/Msg/Msg.cs @@ -152,6 +152,16 @@ public Msg SetDelay(long delay) this._delay = delay; return this; } + + /// + /// Set delay to call branch + /// + /// + public Msg SetDelay(TimeSpan delay) + { + this._delay = (long)delay.TotalSeconds; + return this; + } private void BuildCustomOptions() { diff --git a/src/Dtmgrpc/Msg/MsgGrpc.cs b/src/Dtmgrpc/Msg/MsgGrpc.cs index 7bfefa2..41e70eb 100644 --- a/src/Dtmgrpc/Msg/MsgGrpc.cs +++ b/src/Dtmgrpc/Msg/MsgGrpc.cs @@ -163,6 +163,16 @@ public MsgGrpc SetDelay(long delay) this._delay = delay; return this; } + + /// + /// Set delay to call branch, unit second + /// + /// + public MsgGrpc SetDelay(TimeSpan delay) + { + this._delay = (long)delay.TotalSeconds; + return this; + } private void BuildCustomOptions() { diff --git a/tests/Dtmcli.IntegrationTests/MsgHttpTest.cs b/tests/Dtmcli.IntegrationTests/MsgHttpTest.cs index fc3e1c6..1dbab25 100644 --- a/tests/Dtmcli.IntegrationTests/MsgHttpTest.cs +++ b/tests/Dtmcli.IntegrationTests/MsgHttpTest.cs @@ -59,4 +59,36 @@ public async Task Submit_With_EffectTime_Should_Succeed_Later() status = await ITTestHelper.GetTranStatus(gid); Assert.Equal("succeed", status); } + + [Fact] + public async Task Submit_With_Delay_Should_Succeed_Later() + { + var provider = ITTestHelper.AddDtmHttp(); + var transFactory = provider.GetRequiredService(); + + var gid = "msgTestGid" + Guid.NewGuid().ToString(); + Msg msg = transFactory.NewMsg(gid); + msg.SetDelay(TimeSpan.FromSeconds(10)); + + var req = ITTestHelper.GenBusiReq(false, false); + var busiUrl = ITTestHelper.BuisHttpUrl; + msg.Add(busiUrl + "/busi.Busi/TransOut", req) + .Add(busiUrl + "/busi.Busi/TransIn", req); + + await msg.Prepare(busiUrl + "/busi.Busi/QueryPrepared_404"); + await msg.Submit(); + + // Since the downstream execution is delayed by 10 seconds, it will be 'submitted' after 2 seconds and 'succeed' after 15 seconds + await Task.Delay(TimeSpan.FromSeconds(0)); + var status = await ITTestHelper.GetTranStatus(gid); + Assert.Equal("submitted", status); + + await Task.Delay(TimeSpan.FromSeconds(2)); + status = await ITTestHelper.GetTranStatus(gid); + Assert.Equal("submitted", status); + + await Task.Delay(TimeSpan.FromSeconds(13)); + status = await ITTestHelper.GetTranStatus(gid); + Assert.Equal("succeed", status); + } } \ No newline at end of file diff --git a/tests/Dtmgrpc.IntegrationTests/MsgGrpcTest.cs b/tests/Dtmgrpc.IntegrationTests/MsgGrpcTest.cs index 38d0d0c..865b3ec 100644 --- a/tests/Dtmgrpc.IntegrationTests/MsgGrpcTest.cs +++ b/tests/Dtmgrpc.IntegrationTests/MsgGrpcTest.cs @@ -92,6 +92,32 @@ public async Task Submit_With_EffectTime_Should_Succeed_Later() Assert.Equal("succeed", status); } + [Fact] + public async Task Submit_With_Delay_Should_Succeed_Later() + { + var provider = ITTestHelper.AddDtmGrpc(); + var transFactory = provider.GetRequiredService(); + + var gid = "msgTestGid" + Guid.NewGuid().ToString(); + var msg = transFactory.NewMsgGrpc(gid); + msg.SetDelay(TimeSpan.FromSeconds(10)); + var req = ITTestHelper.GenBusiReq(false, false); + var busiGrpc = ITTestHelper.BuisgRPCUrl; + msg.Add(busiGrpc + "/busi.Busi/TransOut", req) + .Add(busiGrpc + "/busi.Busi/TransIn", req); + + await msg.Prepare(busiGrpc + "/busi.Busi/QueryPrepared"); + await msg.Submit(); + + // Since the downstream execution is delayed by 10 seconds, it will be 'submitted' after 2 seconds and 'succeed' after 15 seconds + await Task.Delay(TimeSpan.FromSeconds(2)); + var status = await ITTestHelper.GetTranStatus(gid); + Assert.Equal("submitted", status); + + await Task.Delay(TimeSpan.FromSeconds(13)); + status = await ITTestHelper.GetTranStatus(gid); + Assert.Equal("succeed", status); + } private static readonly int TransOutUID = 1;