diff --git a/tests/Dtmgrpc.IntegrationTests/Dtmgrpc.IntegrationTests.csproj b/tests/Dtmgrpc.IntegrationTests/Dtmgrpc.IntegrationTests.csproj index 28b9aec..42a896c 100644 --- a/tests/Dtmgrpc.IntegrationTests/Dtmgrpc.IntegrationTests.csproj +++ b/tests/Dtmgrpc.IntegrationTests/Dtmgrpc.IntegrationTests.csproj @@ -24,6 +24,7 @@ + diff --git a/tests/Dtmgrpc.IntegrationTests/ITTestHelper.cs b/tests/Dtmgrpc.IntegrationTests/ITTestHelper.cs index 7943580..48a1b8f 100644 --- a/tests/Dtmgrpc.IntegrationTests/ITTestHelper.cs +++ b/tests/Dtmgrpc.IntegrationTests/ITTestHelper.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Dtmworkflow; using Xunit.Abstractions; using Xunit.Sdk; @@ -13,6 +14,7 @@ public class ITTestHelper public static string DTMHttpUrl = "http://localhost:36789"; public static string DTMgRPCUrl = "http://localhost:36790"; public static string BuisgRPCUrl = "localhost:5005"; + public static string BuisgRPCUrlWithProtocol = "http://localhost:5005"; private static System.Net.Http.HttpClient _client = new System.Net.Http.HttpClient(); public static async Task GetTranStatus(string gid) @@ -67,6 +69,13 @@ public static ServiceProvider AddDtmGrpc(int dtmTimout = 10000) services.AddLogging(); services.AddDtmGrpc(x => { + x.DtmUrl = DTMHttpUrl; + x.DtmGrpcUrl = DTMgRPCUrl; + x.DtmTimeout = dtmTimout; + }); + services.AddDtmWorkflow(x => + { + x.DtmUrl = DTMHttpUrl; x.DtmGrpcUrl = DTMgRPCUrl; x.DtmTimeout = dtmTimout; }); diff --git a/tests/Dtmgrpc.IntegrationTests/WorkflowGrpcTest.cs b/tests/Dtmgrpc.IntegrationTests/WorkflowGrpcTest.cs new file mode 100644 index 0000000..81fbde0 --- /dev/null +++ b/tests/Dtmgrpc.IntegrationTests/WorkflowGrpcTest.cs @@ -0,0 +1,98 @@ +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using busi; +using Dtmworkflow; +using Grpc.Net.Client; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Xunit; + +namespace Dtmgrpc.IntegrationTests +{ + public class WorkflowGrpcTest + { + [Fact] + public async Task Execute_Http_Should_Succeed() + { + var provider = ITTestHelper.AddDtmGrpc(); + var workflowFactory = provider.GetRequiredService(); + var loggerFactory = provider.GetRequiredService(); + WorlflowGlobalTransaction workflowGlobalTransaction = new WorlflowGlobalTransaction(workflowFactory, loggerFactory); + + string wfName1 = $"wf-simple-{Guid.NewGuid().ToString("D")[..8]}"; + workflowGlobalTransaction.Register(wfName1, async (workflow, data) => await Task.FromResult("my result"u8.ToArray())); + + string gid = wfName1 + Guid.NewGuid().ToString()[..8]; + var req = ITTestHelper.GenBusiReq(false, false); + await workflowGlobalTransaction.Execute(wfName1, gid, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(req)), isHttp: true); + + string status = await ITTestHelper.GetTranStatus(gid); + Assert.Equal("succeed", status); + } + + [Fact] + public async Task Execute_gPRC_Should_Succeed() + { + var provider = ITTestHelper.AddDtmGrpc(); + var workflowFactory = provider.GetRequiredService(); + var loggerFactory = provider.GetRequiredService(); + WorlflowGlobalTransaction workflowGlobalTransaction = new WorlflowGlobalTransaction(workflowFactory, loggerFactory); + + string wfName1 = $"wf-simple-{Guid.NewGuid().ToString("D")[..8]}"; + workflowGlobalTransaction.Register(wfName1, async (workflow, data) => await Task.FromResult("my result"u8.ToArray())); + + string gid = wfName1 + Guid.NewGuid().ToString()[..8]; + var req = ITTestHelper.GenBusiReq(false, false); + await workflowGlobalTransaction.Execute(wfName1, gid, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(req)), isHttp: false); + + string status = await ITTestHelper.GetTranStatus(gid); + Assert.Equal("succeed", status); + } + + [Fact] + public async Task Execute_Success() + { + var provider = ITTestHelper.AddDtmGrpc(); + var workflowFactory = provider.GetRequiredService(); + var loggerFactory = provider.GetRequiredService(); + WorlflowGlobalTransaction workflowGlobalTransaction = new WorlflowGlobalTransaction(workflowFactory, loggerFactory); + + Busi.BusiClient busiClient = new Busi.BusiClient(GrpcChannel.ForAddress(ITTestHelper.BuisgRPCUrlWithProtocol)); + + string wfName1 = $"wf-simple-{Guid.NewGuid().ToString("D")[..8]}"; + workflowGlobalTransaction.Register(wfName1, async (workflow, data) => + { + BusiReq request = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(data)); + workflow.NewBranch().OnRollback(async (barrier) => + { + await busiClient.TransInRevertAsync(request); + }); + await busiClient.TransInAsync(request); + + workflow.NewBranch().OnRollback(async (barrier) => + { + await busiClient.TransOutRevertAsync(request); + }); + await busiClient.TransOutAsync(request); + + return await Task.FromResult("my result"u8.ToArray()); + }); + + string gid = wfName1 + Guid.NewGuid().ToString()[..8]; + var req = ITTestHelper.GenBusiReq(false, false); + byte[] result = await workflowGlobalTransaction.Execute(wfName1, gid, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(req))); + Assert.Equal("my result", Encoding.UTF8.GetString(result)); + string status = await ITTestHelper.GetTranStatus(gid); + Assert.Equal("succeed", status); + + // again + result = await workflowGlobalTransaction.Execute(wfName1, gid, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(req))); + Assert.Equal("my result", Encoding.UTF8.GetString(result)); + status = await ITTestHelper.GetTranStatus(gid); + Assert.Equal("succeed", status); + } + } +} \ No newline at end of file