|
6 | 6 | using Microsoft.Extensions.Logging; |
7 | 7 | using Microsoft.Extensions.Options; |
8 | 8 | using System; |
| 9 | +using System.Diagnostics; |
9 | 10 | using System.Net.Http; |
10 | 11 | using System.Net.Http.Headers; |
11 | 12 | using System.Text; |
12 | 13 | using System.Text.Json; |
13 | 14 | using System.Threading; |
14 | 15 | using System.Threading.Tasks; |
| 16 | +using Exception = System.Exception; |
15 | 17 |
|
16 | 18 | namespace DtmSample.Controllers |
17 | 19 | { |
@@ -65,6 +67,52 @@ public async Task<IActionResult> Simple(CancellationToken cancellationToken) |
65 | 67 | } |
66 | 68 | } |
67 | 69 |
|
| 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 | + |
68 | 116 | [HttpPost("wf-saga")] |
69 | 117 | public async Task<IActionResult> Saga(CancellationToken cancellationToken) |
70 | 118 | { |
|
0 commit comments