Skip to content

Commit

Permalink
test(Dtmgrpc.IntegrationTests): add workflow testing support
Browse files Browse the repository at this point in the history
- Update project references to include Dtmworkflow
- Modify ITTestHelper to support workflow testing
- Add new test class WorkflowGrpcTest with workflow test cases
  • Loading branch information
wooln committed Dec 27, 2024
1 parent 9926030 commit fe5255a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Dtmgrpc\Dtmgrpc.csproj" />
<ProjectReference Include="..\..\src\DtmSERedisBarrier\DtmSERedisBarrier.csproj" />
<ProjectReference Include="..\..\src\Dtmworkflow\Dtmworkflow.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions tests/Dtmgrpc.IntegrationTests/ITTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dtmworkflow;
using Xunit.Abstractions;
using Xunit.Sdk;

Expand All @@ -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<string> GetTranStatus(string gid)
Expand Down Expand Up @@ -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;
});
Expand Down
98 changes: 98 additions & 0 deletions tests/Dtmgrpc.IntegrationTests/WorkflowGrpcTest.cs
Original file line number Diff line number Diff line change
@@ -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<IWorkflowFactory>();
var loggerFactory = provider.GetRequiredService<ILoggerFactory>();
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<IWorkflowFactory>();
var loggerFactory = provider.GetRequiredService<ILoggerFactory>();
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<IWorkflowFactory>();
var loggerFactory = provider.GetRequiredService<ILoggerFactory>();
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<BusiReq>(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);
}
}
}

0 comments on commit fe5255a

Please sign in to comment.