Skip to content

Commit 254a47b

Browse files
committed
test(DtmSample): add tests for workflow return values and fix null handling
- Add new test endpoints for different workflow return - Fix null value handling for the twice execute - Update workflow execution to handle null and empty byte arrays consistently
1 parent 9926030 commit 254a47b

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

samples/DtmSample/Controllers/WfTestController.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
using Microsoft.Extensions.Logging;
77
using Microsoft.Extensions.Options;
88
using System;
9+
using System.Diagnostics;
910
using System.Net.Http;
1011
using System.Net.Http.Headers;
1112
using System.Text;
1213
using System.Text.Json;
1314
using System.Threading;
1415
using System.Threading.Tasks;
16+
using Exception = System.Exception;
1517

1618
namespace DtmSample.Controllers
1719
{
@@ -65,6 +67,52 @@ public async Task<IActionResult> Simple(CancellationToken cancellationToken)
6567
}
6668
}
6769

70+
[HttpPost("wf-twice")]
71+
public async Task<IActionResult> SimpleTwice(CancellationToken cancellationToken)
72+
{
73+
try
74+
{
75+
string wfNameReturnNormal = $"wfNameReturnNormal-{Guid.NewGuid().ToString("N")[..8]}";
76+
_globalTransaction.Register(wfNameReturnNormal, async (wf, data) => await Task.FromResult(Encoding.UTF8.GetBytes("my result")));
77+
string wfNameReturnEmpty = $"wfNameReturnEmpty-{Guid.NewGuid().ToString("N")[..8]}";
78+
_globalTransaction.Register(wfNameReturnEmpty, async (wf, data) => await Task.FromResult(Encoding.UTF8.GetBytes("")));
79+
string wfNameReturnNull = $"wfNameReturnNull-{Guid.NewGuid().ToString("N")[..8]}";
80+
_globalTransaction.Register(wfNameReturnNull, (wf, data) => Task.FromResult<byte[]>(null));
81+
82+
string req = JsonSerializer.Serialize(new TransRequest("1", -30));
83+
84+
string gid;
85+
byte[] result1, result2;
86+
string resultStr1, resultStr2;
87+
gid = wfNameReturnNormal + " " + Guid.NewGuid().ToString("N");
88+
result1 = await _globalTransaction.Execute(wfNameReturnNormal, gid, Encoding.UTF8.GetBytes(req), true);
89+
result2 = await _globalTransaction.Execute(wfNameReturnNormal, gid, Encoding.UTF8.GetBytes(req), true);
90+
resultStr1 = Encoding.UTF8.GetString(result1);
91+
resultStr2 = Encoding.UTF8.GetString(result2);
92+
if ("my result" != resultStr1) throw new Exception("\"my result\" != resultStr1");
93+
if (resultStr1 != resultStr2) throw new Exception("resultStr1 != resultStr2");
94+
95+
gid = wfNameReturnEmpty + " " + Guid.NewGuid().ToString("N");
96+
result1 = await _globalTransaction.Execute(wfNameReturnEmpty, gid, Encoding.UTF8.GetBytes(req), true);
97+
result2 = await _globalTransaction.Execute(wfNameReturnEmpty, gid, Encoding.UTF8.GetBytes(req), true);
98+
if (null != result1) throw new Exception("String.Empty != resultStr1");
99+
if (result1 != result2) throw new Exception("resultStr1 != resultStr2");
100+
101+
gid = wfNameReturnNull + " " + Guid.NewGuid().ToString("N");
102+
result1 = await _globalTransaction.Execute(wfNameReturnNull, gid, Encoding.UTF8.GetBytes(req), true);
103+
result2 = await _globalTransaction.Execute(wfNameReturnNull, gid, Encoding.UTF8.GetBytes(req), true);
104+
if (null != result1) throw new Exception("String.Empty != resultStr1");
105+
if (result1 != result2) throw new Exception("resultStr1 != resultStr2");
106+
107+
return Ok(TransResponse.BuildSucceedResponse());
108+
}
109+
catch (Exception ex)
110+
{
111+
_logger.LogError(ex, "Workflow Error");
112+
return Ok(TransResponse.BuildFailureResponse());
113+
}
114+
}
115+
68116
[HttpPost("wf-saga")]
69117
public async Task<IActionResult> Saga(CancellationToken cancellationToken)
70118
{

src/Dtmworkflow/Workflow.Imp.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ internal async Task<byte[]> Process(WfFunc2 handler, byte[] data)
3939
var status = reply.Transaction.Status;
4040
if (status == DtmCommon.Constant.StatusSucceed)
4141
{
42-
var sRes = Convert.FromBase64String(reply.Transaction.Result);
42+
var sRes = reply.Transaction.Result != null
43+
? Convert.FromBase64String(reply.Transaction.Result)
44+
: null;
4345
return sRes;
4446
}
4547
else if (status == DtmCommon.Constant.StatusFailed)
@@ -75,11 +77,15 @@ internal async Task<byte[]> Process(WfFunc2 handler, byte[] data)
7577
}
7678

7779
if (err == null || err is DtmCommon.DtmFailureException)
78-
{
80+
{
81+
// The DTM server does not distinguish between byte[0] and null,
82+
// when execute the second time (eg. executeByQs), the previous query result returned is null.
7983
await this.Submit(res, err, default);
8084
}
81-
82-
return res;
85+
86+
// Before the DTM server side (v1.18.0) distinguish between byte[0] and null,
87+
// in order to be compatible with the same return values for twice call, byte[] has been uniformly changed to return null.
88+
return (res != null && res.Length == 0) ? null : res;
8389
}
8490

8591
private async Task SaveResult(string branchId, string op, StepResult sr)

0 commit comments

Comments
 (0)