Пример #1
0
        public async Task Target_TestMaxMessages()
        {
            var outputs = new List <int>();

            var tenTimesTransformer = new TransformBlock <int, int>(
                x => x * 10,
                new ExecutionDataflowBlockOptions()
            {
                BoundedCapacity = 1
            });
            var target = tenTimesTransformer
                         .ChainToTarget(new ActionBlock <int>(
                                            async i =>
            {
                outputs.Add(i);
                await Task.Delay(100);         // Delay to postpone offer
            },
                                            new ExecutionDataflowBlockOptions()
            {
                BoundedCapacity = 1
            }
                                            ));

            // sourceBlock emits 1, 2, 3, 4, 5
            var sourceBlock = new BufferBlock <int>();

            for (var i = 1; i <= 5; i++)
            {
                sourceBlock.Post(i);
            }

            sourceBlock.LinkTo(target, new DataflowLinkOptions()
            {
                MaxMessages = 2
            });

            await Task.Delay(500);

            // Expect first 2 items
            outputs.Is(10, 20);
        }