-
Notifications
You must be signed in to change notification settings - Fork 261
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 #1465 from AElfProject/case/unit-cases
Improve unit test cases
- Loading branch information
Showing
14 changed files
with
377 additions
and
31 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
File renamed without changes.
File renamed without changes.
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,72 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Grpc.Core; | ||
using Grpc.Core.Testing; | ||
using Grpc.Core.Utils; | ||
using Moq; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace AElf.CrossChain.Grpc | ||
{ | ||
public class GrpcServerTests : GrpcCrossChainServerTestBase | ||
{ | ||
private CrossChainRpc.CrossChainRpcBase CrossChainGrpcServer; | ||
|
||
public GrpcServerTests() | ||
{ | ||
CrossChainGrpcServer = GetRequiredService<CrossChainRpc.CrossChainRpcBase>(); | ||
} | ||
|
||
[Fact] | ||
public async Task RequestIndexingParentChain() | ||
{ | ||
var requestData = new RequestCrossChainBlockData | ||
{ | ||
FromChainId = 0, | ||
NextHeight = 15 | ||
}; | ||
|
||
IServerStreamWriter<ResponseParentChainBlockData> responseStream = Mock.Of<IServerStreamWriter<ResponseParentChainBlockData>>(); | ||
var context = BuildServerCallContext(); | ||
await CrossChainGrpcServer.RequestIndexingParentChain(requestData, responseStream, context); | ||
} | ||
|
||
[Fact] | ||
public async Task RequestIndexingSideChain() | ||
{ | ||
var requestData = new RequestCrossChainBlockData | ||
{ | ||
FromChainId = 0, | ||
NextHeight = 10 | ||
}; | ||
|
||
IServerStreamWriter<ResponseSideChainBlockData> responseStream = Mock.Of<IServerStreamWriter<ResponseSideChainBlockData>>(); | ||
var context = BuildServerCallContext(); | ||
await CrossChainGrpcServer.RequestIndexingSideChain(requestData, responseStream, context); | ||
} | ||
|
||
[Fact] | ||
public async Task CrossChainIndexingShake() | ||
{ | ||
var request = new IndexingHandShake | ||
{ | ||
ListeningPort = 2000, | ||
ChainId = 0 | ||
}; | ||
var context = BuildServerCallContext(); | ||
var indexingHandShakeReply = await CrossChainGrpcServer.CrossChainIndexingShake(request, context); | ||
|
||
indexingHandShakeReply.ShouldNotBeNull(); | ||
indexingHandShakeReply.Result.ShouldBeTrue(); | ||
} | ||
|
||
private ServerCallContext BuildServerCallContext(Metadata metadata = null) | ||
{ | ||
var meta = metadata ?? new Metadata(); | ||
return TestServerCallContext.Create("mock", null, DateTime.UtcNow.AddHours(1), meta, CancellationToken.None, | ||
"ipv4:127.0.0.1:2000", null, null, m => TaskUtils.CompletedTask, () => new WriteOptions(), writeOptions => { }); | ||
} | ||
} | ||
} |
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,69 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using AElf.Common; | ||
using AElf.Consensus.DPoS; | ||
using AElf.Kernel.Consensus.Application; | ||
using AElf.Kernel.Consensus.Infrastructure; | ||
using Google.Protobuf; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace AElf.Kernel.Consensus | ||
{ | ||
public class ConsensusServiceTests : ConsensusTestBase | ||
{ | ||
private IConsensusService _consensusService; | ||
private ConsensusControlInformation _consensusControlInformation; | ||
public ConsensusServiceTests() | ||
{ | ||
_consensusService = GetRequiredService<IConsensusService>(); | ||
_consensusControlInformation = GetRequiredService<ConsensusControlInformation>(); | ||
} | ||
|
||
[Fact] | ||
public async Task ValidateConsensusBeforeExecutionAsync() | ||
{ | ||
var preHash = Hash.Generate(); | ||
var blockHeight = 100; | ||
var consensusExtraData = ByteString.CopyFromUtf8("test data").ToByteArray(); | ||
var result = await _consensusService.ValidateConsensusBeforeExecutionAsync(preHash, blockHeight, consensusExtraData); | ||
|
||
result.ShouldBeTrue(); | ||
} | ||
|
||
[Fact] | ||
public async Task ValidateConsensusAfterExecutionAsync() | ||
{ | ||
var preHash = Hash.Generate(); | ||
var blockHeight = 100; | ||
var consensusExtraData = ByteString.CopyFromUtf8("test data").ToByteArray(); | ||
var result = await _consensusService.ValidateConsensusAfterExecutionAsync(preHash, blockHeight, consensusExtraData); | ||
|
||
result.ShouldBeTrue(); | ||
} | ||
|
||
[Fact] | ||
public async Task GetNewConsensusInformationAsync() | ||
{ | ||
var bytes = await _consensusService.GetNewConsensusInformationAsync(); | ||
var dposTriggerInformation = DPoSTriggerInformation.Parser.ParseFrom(bytes); | ||
|
||
dposTriggerInformation.MiningInterval.ShouldBe(4000); | ||
dposTriggerInformation.IsBootMiner.ShouldBeTrue(); | ||
} | ||
|
||
[Fact] | ||
public async Task GenerateConsensusTransactionsAsync() | ||
{ | ||
var transactions = await _consensusService.GenerateConsensusTransactionsAsync(); | ||
|
||
transactions.ShouldNotBeNull(); | ||
transactions.Count().ShouldBe(3); | ||
|
||
transactions.Select(t =>t.RefBlockNumber).ShouldAllBe(x => x == 100); | ||
|
||
var prefix = ByteString.CopyFrom(Hash.Empty.Take(4).ToArray()); | ||
transactions.Select(t =>t.RefBlockPrefix).ShouldAllBe( p => p == prefix); | ||
} | ||
} | ||
} |
Oops, something went wrong.