Skip to content

Commit

Permalink
test(DtmSample): add tests for workflow return values and fix null ha…
Browse files Browse the repository at this point in the history
…ndling

- 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
  • Loading branch information
wooln committed Dec 27, 2024
1 parent 9926030 commit 254a47b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
48 changes: 48 additions & 0 deletions samples/DtmSample/Controllers/WfTestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Exception = System.Exception;

namespace DtmSample.Controllers
{
Expand Down Expand Up @@ -65,6 +67,52 @@ public async Task<IActionResult> Simple(CancellationToken cancellationToken)
}
}

[HttpPost("wf-twice")]
public async Task<IActionResult> SimpleTwice(CancellationToken cancellationToken)
{
try
{
string wfNameReturnNormal = $"wfNameReturnNormal-{Guid.NewGuid().ToString("N")[..8]}";
_globalTransaction.Register(wfNameReturnNormal, async (wf, data) => await Task.FromResult(Encoding.UTF8.GetBytes("my result")));
string wfNameReturnEmpty = $"wfNameReturnEmpty-{Guid.NewGuid().ToString("N")[..8]}";
_globalTransaction.Register(wfNameReturnEmpty, async (wf, data) => await Task.FromResult(Encoding.UTF8.GetBytes("")));
string wfNameReturnNull = $"wfNameReturnNull-{Guid.NewGuid().ToString("N")[..8]}";
_globalTransaction.Register(wfNameReturnNull, (wf, data) => Task.FromResult<byte[]>(null));

string req = JsonSerializer.Serialize(new TransRequest("1", -30));

string gid;
byte[] result1, result2;
string resultStr1, resultStr2;
gid = wfNameReturnNormal + " " + Guid.NewGuid().ToString("N");
result1 = await _globalTransaction.Execute(wfNameReturnNormal, gid, Encoding.UTF8.GetBytes(req), true);
result2 = await _globalTransaction.Execute(wfNameReturnNormal, gid, Encoding.UTF8.GetBytes(req), true);
resultStr1 = Encoding.UTF8.GetString(result1);
resultStr2 = Encoding.UTF8.GetString(result2);
if ("my result" != resultStr1) throw new Exception("\"my result\" != resultStr1");
if (resultStr1 != resultStr2) throw new Exception("resultStr1 != resultStr2");

gid = wfNameReturnEmpty + " " + Guid.NewGuid().ToString("N");
result1 = await _globalTransaction.Execute(wfNameReturnEmpty, gid, Encoding.UTF8.GetBytes(req), true);
result2 = await _globalTransaction.Execute(wfNameReturnEmpty, gid, Encoding.UTF8.GetBytes(req), true);
if (null != result1) throw new Exception("String.Empty != resultStr1");
if (result1 != result2) throw new Exception("resultStr1 != resultStr2");

gid = wfNameReturnNull + " " + Guid.NewGuid().ToString("N");
result1 = await _globalTransaction.Execute(wfNameReturnNull, gid, Encoding.UTF8.GetBytes(req), true);
result2 = await _globalTransaction.Execute(wfNameReturnNull, gid, Encoding.UTF8.GetBytes(req), true);
if (null != result1) throw new Exception("String.Empty != resultStr1");
if (result1 != result2) throw new Exception("resultStr1 != resultStr2");

return Ok(TransResponse.BuildSucceedResponse());
}
catch (Exception ex)
{
_logger.LogError(ex, "Workflow Error");
return Ok(TransResponse.BuildFailureResponse());
}
}

[HttpPost("wf-saga")]
public async Task<IActionResult> Saga(CancellationToken cancellationToken)
{
Expand Down
14 changes: 10 additions & 4 deletions src/Dtmworkflow/Workflow.Imp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ internal async Task<byte[]> Process(WfFunc2 handler, byte[] data)
var status = reply.Transaction.Status;
if (status == DtmCommon.Constant.StatusSucceed)
{
var sRes = Convert.FromBase64String(reply.Transaction.Result);
var sRes = reply.Transaction.Result != null
? Convert.FromBase64String(reply.Transaction.Result)
: null;
return sRes;
}
else if (status == DtmCommon.Constant.StatusFailed)
Expand Down Expand Up @@ -75,11 +77,15 @@ internal async Task<byte[]> Process(WfFunc2 handler, byte[] data)
}

if (err == null || err is DtmCommon.DtmFailureException)
{
{
// The DTM server does not distinguish between byte[0] and null,
// when execute the second time (eg. executeByQs), the previous query result returned is null.
await this.Submit(res, err, default);
}

return res;

// Before the DTM server side (v1.18.0) distinguish between byte[0] and null,
// in order to be compatible with the same return values for twice call, byte[] has been uniformly changed to return null.
return (res != null && res.Length == 0) ? null : res;
}

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

0 comments on commit 254a47b

Please sign in to comment.