Exemplo n.º 1
0
        public void Run(string uri)
        {
            // Declaration of all the pipeline blocks
            GetTopStoriesBlock              getTopStories              = new GetTopStoriesBlock();
            FilterTopStoriesBlock           filterTopStories           = new FilterTopStoriesBlock();
            TraverseTopStoriesCommentsBlock traverseTopStoriesComments = new TraverseTopStoriesCommentsBlock();
            ComputeTopCommentsPerStoryBlock computeTopCommentsPerStory = new ComputeTopCommentsPerStoryBlock();

            // Links and interactions betwen each block
            getTopStories.block.LinkTo(filterTopStories.block, this.linkOptions);
            filterTopStories.bufferBlock.LinkTo(traverseTopStoriesComments.block, linkOptions);
            traverseTopStoriesComments.bufferBlock.LinkTo(computeTopCommentsPerStory.block, linkOptions);

            // Triggering the start of the pipeline
            getTopStories.block.Post(uri);
            getTopStories.block.Complete();

            // Buffer at the end of the pipeline
            var receiveAllStories = Task.Run(() =>
            {
                for (int i = 0; i <= Constants.NBSTORIES; i++)
                {
                    this.topStories.Add(computeTopCommentsPerStory.bufferBlock.Receive());    // Wait for all the Top Story objects to arrive
                }
            });

            Task.WaitAll(receiveAllStories);
            computeTopCommentsPerStory.bufferBlock.Complete(); //End of the pipeline

            commentsRegistry = traverseTopStoriesComments.commentsRegistry;
            printResults();
        }
        public void GetTopStoriesBlock()
        {
            string uri         = "https://hacker-news.firebaseio.com/v0/topstories.json";
            var    linkOptions = new DataflowLinkOptions {
                PropagateCompletion = true
            };

            BufferBlock <Queue <string> > bufferBlock        = new BufferBlock <Queue <string> >();
            GetTopStoriesBlock            getTopStoriesBlock = new GetTopStoriesBlock();

            getTopStoriesBlock.block.LinkTo(bufferBlock, linkOptions);

            getTopStoriesBlock.block.Post(uri);
            getTopStoriesBlock.block.Complete();
            var result = bufferBlock.ReceiveAsync().Result;

            Assert.IsNotNull(result);
        }