Ejemplo n.º 1
0
                Will_Process_The_Requested_Block_Range()
                {
                    const long StartBlock = 1000;
                    const long EndBlock   = 1205;

                    for (var block = StartBlock; block <= EndBlock; block++)
                    {
                        MockBlockProcessor.Setup(p => p.ProcessBlockAsync(block)).Returns(Task.CompletedTask);
                    }

                    Assert.True(await Processor.ExecuteAsync(StartBlock, EndBlock));

                    MockBlockProcessor.VerifyAll();
                }
Ejemplo n.º 2
0
                Requests_Last_Block_Processed_From_Strategy_And_Uses_The_Previous_Block_Number()
                {
                    const long LastBlockProcessed = 11;
                    const long ExpectedStartBlock = LastBlockProcessed - 1;
                    const long EndingBlock        = ExpectedStartBlock;

                    MockProcessingStrategy
                    .Setup(s => s.GetLastBlockProcessedAsync())
                    .ReturnsAsync(LastBlockProcessed);

                    MockBlockProcessor
                    .Setup(p => p.ProcessBlockAsync(ExpectedStartBlock))
                    .Returns(Task.CompletedTask);

                    await Processor.ExecuteAsync(startBlock : null, endBlock : EndingBlock);

                    MockProcessingStrategy.VerifyAll();
                    MockBlockProcessor.VerifyAll();
                }
Ejemplo n.º 3
0
                public async Task Will_Wait_For_Minimum_Block_Confirmations()
                {
                    const int RequiredBlockConfirmations = 6;
                    long      maxBlockNumber             = 0;
                    int       numberOfWaitCycles         = 0;
                    int       blocksProcessed            = 0;

                    var cancellationTokenSource = new CancellationTokenSource();

                    MockProcessingStrategy
                    .Setup(s => s.MinimumBlockConfirmations)
                    .Returns(RequiredBlockConfirmations);

                    MockBlockProcessor
                    .Setup(p => p.GetMaxBlockNumberAsync())
                    .Callback(() => maxBlockNumber++)
                    .ReturnsAsync(() => maxBlockNumber);

                    MockProcessingStrategy
                    .Setup(s => s.WaitForNextBlock(It.IsAny <int>()))
                    .Callback <int>((retryNumber) => numberOfWaitCycles++)
                    .Returns(Task.CompletedTask);

                    MockBlockProcessor
                    .Setup(p => p.ProcessBlockAsync(0))
                    .Returns(() =>
                    {
                        blocksProcessed++;
                        cancellationTokenSource.Cancel();
                        return(Task.CompletedTask);
                    });

                    var result = await Processor.ExecuteAsync(startBlock : 0, endBlock : null, cancellationToken : cancellationTokenSource.Token);

                    Assert.False(result, "Result should be false because execution should have been stopped by cancellation token");

                    Assert.Equal(1, blocksProcessed);
                    Assert.Equal(RequiredBlockConfirmations - 1, numberOfWaitCycles);
                    Assert.Equal(RequiredBlockConfirmations, maxBlockNumber);
                    MockBlockProcessor.VerifyAll();
                    MockProcessingStrategy.VerifyAll();
                }
Ejemplo n.º 4
0
                public async Task Last_Block_Processsed_Is_Only_Used_If_Greater_Than_Minimum_Block_Number()
                {
                    const long LastBlockProcessed   = 11;
                    const long MinimumStartingBlock = 20;
                    const long EndingBlock          = MinimumStartingBlock;

                    MockProcessingStrategy.SetupGet(s => s.MinimumBlockNumber).Returns(MinimumStartingBlock);

                    MockProcessingStrategy
                    .Setup(s => s.GetLastBlockProcessedAsync())
                    .ReturnsAsync(LastBlockProcessed);

                    MockBlockProcessor
                    .Setup(p => p.ProcessBlockAsync(MinimumStartingBlock))
                    .Returns(Task.CompletedTask);

                    await Processor.ExecuteAsync(startBlock : null, endBlock : EndingBlock);

                    MockProcessingStrategy.VerifyAll();
                    MockBlockProcessor.VerifyAll();
                }
Ejemplo n.º 5
0
                public async Task Will_Wait_For_Minimum_Block_Confirmations()
                {
                    const int RequiredBlockConfirmations = 6;
                    long      maxBlockNumber             = 0;
                    int       numberOfWaitCycles         = 0;
                    int       blocksProcessed            = 0;

                    MockProcessingStrategy
                    .Setup(s => s.MinimumBlockConfirmations)
                    .Returns(RequiredBlockConfirmations);

                    MockBlockProcessor
                    .Setup(p => p.GetMaxBlockNumberAsync())
                    .Callback(() => maxBlockNumber++)
                    .ReturnsAsync(() => maxBlockNumber);

                    MockProcessingStrategy
                    .Setup(s => s.WaitForNextBlock(It.IsAny <int>()))
                    .Callback <int>((retryNumber) => numberOfWaitCycles++)
                    .Returns(Task.CompletedTask);

                    MockBlockProcessor
                    .Setup(p => p.ProcessBlockAsync(0))
                    .Returns(() =>
                    {
                        blocksProcessed++;
                        return(Task.CompletedTask);
                    });

                    var result = await Processor.ExecuteAsync(startBlock : 0, endBlock : 0);

                    Assert.True(result);

                    Assert.Equal(1, blocksProcessed);
                    Assert.Equal(RequiredBlockConfirmations - 1, numberOfWaitCycles);
                    Assert.Equal(RequiredBlockConfirmations, maxBlockNumber);
                    MockBlockProcessor.VerifyAll();
                    MockProcessingStrategy.VerifyAll();
                }
Ejemplo n.º 6
0
                Will_Run_Continuously()
                {
                    var cancellationTokenSource = new CancellationTokenSource();

                    MockBlockProcessor
                    .Setup(p => p.ProcessBlockAsync(0))
                    .Returns(Task.CompletedTask)
                    .Verifiable("ProcessBlockAsync should have been called for Block 0");

                    MockBlockProcessor
                    .Setup(p => p.ProcessBlockAsync(1))
                    .Callback <long>(blkNum => cancellationTokenSource.Cancel())
                    .Returns(Task.CompletedTask)
                    .Verifiable("ProcessBlockAsync should have been called for Block 1");

                    var result = await Processor.ExecuteAsync(startBlock : 0, endBlock : null, cancellationToken : cancellationTokenSource.Token);

                    Assert.False(result,
                                 "Result should be false because execution was cancelled by cancellation token source after block number 1");

                    MockBlockProcessor.VerifyAll();
                }
Ejemplo n.º 7
0
                public async Task When_A_Block_Is_Not_Found_Will_Ask_Strategy_To_Wait_For_Next_Block_Until_Cancelled()
                {
                    var cancellationTokenSource = new CancellationTokenSource();

                    MockBlockProcessor
                    .Setup(p => p.ProcessBlockAsync(0))
                    .Throws(new BlockNotFoundException(0));

                    MockProcessingStrategy.Setup(s => s.WaitForNextBlock(0)).Returns(Task.CompletedTask);
                    MockProcessingStrategy.Setup(s => s.WaitForNextBlock(1)).Returns(Task.CompletedTask);
                    MockProcessingStrategy.Setup(s => s.WaitForNextBlock(2)).Returns(() =>
                    {
                        cancellationTokenSource.Cancel();
                        return(Task.CompletedTask);
                    });

                    var result = await Processor.ExecuteAsync(startBlock : 0, endBlock : null, cancellationToken : cancellationTokenSource.Token);

                    Assert.False(result,
                                 "Result should be false because execution was cancelled by cancellation token source");

                    MockBlockProcessor.VerifyAll();
                    MockProcessingStrategy.VerifyAll();
                }