예제 #1
0
        public async Task RunInBackgroundAsync_Using_OnBatchProcessed()
        {
            LogBatchProcessedArgs lastBatchProcessedArgs = null;

            var mockLogProcessor = new Mock <ILogProcessor>();
            var web3Mock         = new Web3Mock();
            var logs             = new[] { new FilterLog() };

            web3Mock.GetLogsMock
            .Setup(p => p.SendRequestAsync(It.IsAny <NewFilterInput>(), null))
            .ReturnsAsync(logs);

            web3Mock.BlockNumberMock
            .Setup(p => p.SendRequestAsync(null))
            .ReturnsAsync(100.ToHexBigInteger());

            var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));

            var builder = new LogsProcessorBuilder(web3Mock.Eth)
                          .Set(p => p.BlocksPerBatch     = 1)
                          .Set(p => p.MinimumBlockNumber = 0)
                          // escape hatch
                          .OnBatchProcessed((args) =>
            {
                lastBatchProcessedArgs = args;
                cancellationTokenSource.Cancel();
            })
                          .Add(mockLogProcessor.Object);

            var processor = builder.Build();

            var backgroundProcessingTask = processor.ProcessContinuallyInBackgroundAsync(cancellationTokenSource.Token);

            //simulate doing some work until cancellation is requested
            while (!backgroundProcessingTask.IsCompleted)
            {
                await Task.Delay(10);
            }

            Assert.Equal((uint)1, lastBatchProcessedArgs.BatchesProcessedSoFar);
            Assert.Equal(0, lastBatchProcessedArgs.LastRangeProcessed.From.Value);
            Assert.Equal(0, lastBatchProcessedArgs.LastRangeProcessed.To.Value);

            Assert.Equal(0, await builder.BlockProgressRepository.GetLastBlockNumberProcessedAsync());
        }
예제 #2
0
        public async Task ProcessContinuallyAsync_Using_OnBatchProcessed()
        {
            LogBatchProcessedArgs lastBatchProcessedArgs = null;

            var mockLogProcessor = new Mock <ILogProcessor>();
            var web3Mock         = new Web3Mock();
            var logs             = new[] { new FilterLog() };

            web3Mock.GetLogsMock
            .Setup(p => p.SendRequestAsync(It.IsAny <NewFilterInput>(), null))
            .ReturnsAsync(logs);

            web3Mock.BlockNumberMock
            .Setup(p => p.SendRequestAsync(null))
            .ReturnsAsync(100.ToHexBigInteger());

            var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));

            var builder = new LogsProcessorBuilder(web3Mock.ContractServiceMock.Object)
                          .Set(p => p.BlocksPerBatch     = 1)
                          .Set(p => p.MinimumBlockNumber = 0)
                          // escape hatch
                          .OnBatchProcessed((args) =>
            {
                lastBatchProcessedArgs = args;
                cancellationTokenSource.Cancel();
            })
                          .Add(mockLogProcessor.Object);

            var processor = builder.Build();

            var rangesProcessed = await processor.ProcessContinuallyAsync(cancellationTokenSource.Token);

            Assert.Equal(1, rangesProcessed);
            Assert.Equal((uint)1, lastBatchProcessedArgs.BatchesProcessedSoFar);
            Assert.Equal(0, lastBatchProcessedArgs.LastRangeProcessed.From.Value);
            Assert.Equal(0, lastBatchProcessedArgs.LastRangeProcessed.To.Value);

            Assert.Equal(0, await builder.BlockProgressRepository.GetLastBlockNumberProcessedAsync());
        }