-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from dtm-labs/feat/addmoretests
test: add more tests
- Loading branch information
Showing
10 changed files
with
280 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Dtmgrpc.Tests | ||
{ | ||
internal class CusServerCallContext : Grpc.Core.ServerCallContext | ||
{ | ||
private Grpc.Core.Metadata _reqMetadata; | ||
|
||
public CusServerCallContext(Grpc.Core.Metadata reqMetadata) | ||
{ | ||
this._reqMetadata = reqMetadata; | ||
} | ||
|
||
protected override string MethodCore => throw new NotImplementedException(); | ||
|
||
protected override string HostCore => throw new NotImplementedException(); | ||
|
||
protected override string PeerCore => throw new NotImplementedException(); | ||
|
||
protected override DateTime DeadlineCore => throw new NotImplementedException(); | ||
|
||
protected override Grpc.Core.Metadata RequestHeadersCore => _reqMetadata; | ||
|
||
protected override CancellationToken CancellationTokenCore => throw new NotImplementedException(); | ||
|
||
protected override Grpc.Core.Metadata ResponseTrailersCore => throw new NotImplementedException(); | ||
|
||
protected override Grpc.Core.Status StatusCore { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } | ||
protected override Grpc.Core.WriteOptions WriteOptionsCore { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } | ||
|
||
protected override Grpc.Core.AuthContext AuthContextCore => throw new NotImplementedException(); | ||
|
||
protected override Grpc.Core.ContextPropagationToken CreatePropagationTokenCore(Grpc.Core.ContextPropagationOptions options) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
protected override Task WriteResponseHeadersAsyncCore(Grpc.Core.Metadata responseHeaders) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Dtmgrpc.Tests | ||
{ | ||
public class DtmTransFactoryTests | ||
{ | ||
[Fact] | ||
public void NewMsg_Should_Succeed() | ||
{ | ||
var tFactory = BuildFactory(); | ||
|
||
var gid = "TestMsgNormal"; | ||
|
||
var msg = tFactory.NewMsgGrpc(gid); | ||
|
||
Assert.NotNull(msg); | ||
} | ||
|
||
[Fact] | ||
public void NewSaga_Should_Succeed() | ||
{ | ||
var tFactory = BuildFactory(); | ||
|
||
var gid = "TestSagaNormal"; | ||
|
||
var saga = tFactory.NewSagaGrpc(gid); | ||
|
||
Assert.NotNull(saga); | ||
} | ||
|
||
[Fact] | ||
public void NewTcc_Should_Succeed() | ||
{ | ||
var tFactory = BuildFactory(); | ||
|
||
var gid = "TestTccNormal"; | ||
|
||
var saga = tFactory.NewTccGrpc(gid); | ||
|
||
Assert.NotNull(saga); | ||
} | ||
|
||
private DtmTransFactory BuildFactory() | ||
{ | ||
var dtmClient = new Mock<IDtmgRPCClient>(); | ||
var bbFactory = new Mock<IBranchBarrierFactory>(); | ||
var option = Microsoft.Extensions.Options.Options.Create(new DtmCommon.DtmOptions { }); | ||
|
||
return new DtmTransFactory(option, dtmClient.Object, bbFactory.Object); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
using DtmCommon; | ||
using Grpc.Core; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Dtmgrpc.Tests | ||
{ | ||
public class UtilsTests | ||
{ | ||
private static DtmFailureException DtmFailure = new DtmFailureException(); | ||
private static DtmOngingException DtmOnging = new DtmOngingException(); | ||
|
||
[Fact] | ||
public void DtmError2GrpcError_Should_Throw_Aborted_RpcException() | ||
{ | ||
var ex = Assert.Throws<RpcException>(()=> DtmGImp.Utils.DtmError2GrpcError(DtmFailure)); | ||
Assert.Equal(StatusCode.Aborted, ex.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public void DtmError2GrpcError_Should_Throw_FailedPrecondition_RpcException() | ||
{ | ||
var ex = Assert.Throws<RpcException>(() => DtmGImp.Utils.DtmError2GrpcError(DtmOnging)); | ||
Assert.Equal(StatusCode.FailedPrecondition, ex.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public void DtmError2GrpcError_Should_Throw_Unknown_RpcException() | ||
{ | ||
var ex = Assert.Throws<RpcException>(() => DtmGImp.Utils.DtmError2GrpcError(new System.ArgumentNullException())); | ||
Assert.Equal(StatusCode.Unknown, ex.StatusCode); | ||
} | ||
|
||
[Theory] | ||
[InlineData(StatusCode.Aborted, "ONGOING")] | ||
[InlineData(StatusCode.FailedPrecondition, "other")] | ||
public void GrpcError2DtmError_Should_Be_DtmOngingException(StatusCode code, string msg) | ||
{ | ||
var rpcEx = new RpcException(new Status(code, msg), msg); | ||
var ex = DtmGImp.Utils.GrpcError2DtmError(rpcEx); | ||
Assert.IsType<DtmOngingException>(ex); | ||
} | ||
|
||
[Fact] | ||
public void GrpcError2DtmError_Should_Be_DtmFailureException() | ||
{ | ||
var rpcEx = new RpcException(new Status(StatusCode.Aborted, "Other")); | ||
var ex = DtmGImp.Utils.GrpcError2DtmError(rpcEx); | ||
Assert.IsType<DtmFailureException>(ex); | ||
} | ||
|
||
[Fact] | ||
public void GrpcError2DtmError_Should_Be_RawException() | ||
{ | ||
var rpcEx = new System.ArgumentNullException(); | ||
var ex = DtmGImp.Utils.GrpcError2DtmError(rpcEx); | ||
Assert.IsType<System.ArgumentNullException>(ex); | ||
} | ||
|
||
[Fact] | ||
public void String2DtmError_Should_Succeed() | ||
{ | ||
var fEx = DtmGImp.Utils.String2DtmError(Constant.ResultFailure); | ||
Assert.IsType<DtmFailureException>(fEx); | ||
|
||
var oEx = DtmGImp.Utils.String2DtmError(Constant.ResultOngoing); | ||
Assert.IsType<DtmOngingException>(oEx); | ||
|
||
var nullEx = DtmGImp.Utils.String2DtmError(Constant.ResultSuccess); | ||
Assert.Null(nullEx); | ||
|
||
nullEx = DtmGImp.Utils.String2DtmError(string.Empty); | ||
Assert.Null(nullEx); | ||
|
||
nullEx = DtmGImp.Utils.String2DtmError("other"); | ||
Assert.Null(nullEx); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, "")] | ||
[InlineData("http://a.b.com/", "a.b.com")] | ||
[InlineData("https://a.b.com", "a.b.com")] | ||
public void GetWithoutPrefixgRPCUrl_Should_Succeed(string url, string exp) | ||
{ | ||
var res = DtmGImp.Utils.GetWithoutPrefixgRPCUrl(url); | ||
Assert.Equal(exp, res); | ||
} | ||
|
||
[Fact] | ||
public void TransInfo2Metadata_Should_Succeed() | ||
{ | ||
var meta = DtmGImp.Utils.TransInfo2Metadata("1", "2", "3", "4", "5"); | ||
|
||
Assert.Equal("1", meta.Get(Constant.Md.Gid).Value); | ||
Assert.Equal("2", meta.Get(Constant.Md.TransType).Value); | ||
Assert.Equal("3", meta.Get(Constant.Md.BranchId).Value); | ||
Assert.Equal("4", meta.Get(Constant.Md.Op).Value); | ||
Assert.Equal("5", meta.Get(Constant.Md.Dtm).Value); | ||
} | ||
|
||
[Fact] | ||
public void DtmGet_Should_Succeed() | ||
{ | ||
var meta = new Metadata(); | ||
meta.Add("a", "b"); | ||
meta.Add("c", "d"); | ||
var context = new CusServerCallContext(meta); | ||
|
||
var str1 = DtmGImp.Utils.DtmGet(context, "a"); | ||
Assert.Equal("b", str1); | ||
|
||
var str2 = DtmGImp.Utils.DtmGet(context, "d"); | ||
Assert.Null(str2); | ||
} | ||
|
||
[Fact] | ||
public void DtmGet_When_Header_IsNull_Should_Succeed() | ||
{ | ||
var context = new CusServerCallContext(null); | ||
|
||
var str = DtmGImp.Utils.DtmGet(context, "a"); | ||
Assert.Null(str); | ||
} | ||
} | ||
} |