public async Task Producer(string category, int pageSize, ITargetBlock <UnexpandedArticle[]> targetBlock)
        {
            if (string.IsNullOrWhiteSpace(category))
            {
                throw new ArgumentException(nameof(category));
            }

            if (targetBlock == null)
            {
                throw new ArgumentException(nameof(targetBlock));
            }

            var nextBatch = await _wikiArticle.AlphabeticalList(new ArticleListRequestParameters { Category = Uri.EscapeDataString(category), Limit = pageSize });

            bool isNextBatchAvailable;

            do
            {
                await targetBlock.SendAsync(nextBatch.Items);

                isNextBatchAvailable = !string.IsNullOrEmpty(nextBatch.Offset);

                if (isNextBatchAvailable)
                {
                    nextBatch = await _wikiArticle.AlphabeticalList(new ArticleListRequestParameters
                    {
                        Category = category,
                        Limit    = pageSize,
                        Offset   = nextBatch.Offset
                    });
                }
            } while (isNextBatchAvailable);

            targetBlock.Complete();
        }
Пример #2
0
        public void Given_Null_RequestParameters_Should_Throw_ArgumentNullException()
        {
            // Arrange

            // Act
            Func <Task <UnexpandedListArticleResultSet> > act = () => _sut.AlphabeticalList(default(ArticleListRequestParameters));

            // Assert
            act.ShouldThrow <ArgumentNullException>();
        }
        public void Given_Batches_Of_UnexpandedArticle_Should_Process_All_Batches()
        {
            // Arrange
            var expected = 5;
            var pageSize = 100;
            var articleBatchBufferBlock = new BufferBlock <UnexpandedArticle[]>();

            var fixture = new Fixture();

            var articleBatch1 = fixture.Build <UnexpandedListArticleResultSet>().With(x => x.Items, new Fixture {
                RepeatCount = pageSize
            }.Create <UnexpandedArticle[]>()).Create();
            var articleBatch2 = fixture.Build <UnexpandedListArticleResultSet>().With(x => x.Items, new Fixture {
                RepeatCount = pageSize
            }.Create <UnexpandedArticle[]>()).Create();
            var articleBatch3 = fixture.Build <UnexpandedListArticleResultSet>().With(x => x.Items, new Fixture {
                RepeatCount = pageSize
            }.Create <UnexpandedArticle[]>()).Create();
            var articleBatch4 = fixture.Build <UnexpandedListArticleResultSet>().With(x => x.Items, new Fixture {
                RepeatCount = pageSize
            }.Create <UnexpandedArticle[]>()).Create();
            var articleBatch5 = fixture.Build <UnexpandedListArticleResultSet>().With(x => x.Items, new Fixture {
                RepeatCount = pageSize
            }.Create <UnexpandedArticle[]>()).Create();

            // Set Last page
            articleBatch5.Offset = null;

            _wikiArticle
            .AlphabeticalList(Arg.Any <ArticleListRequestParameters>())
            .ReturnsForAnyArgs
            (
                articleBatch1,
                articleBatch2,
                articleBatch3,
                articleBatch4,
                articleBatch5
            );

            // Act
            _sut.Producer("a category", 500, articleBatchBufferBlock).Wait();


            // Assert
            articleBatchBufferBlock.Count.Should().Be(expected);
        }