当前位置: 首页>>代码示例>>C#>>正文


C# BufferBlock.PostRange方法代码示例

本文整理汇总了C#中BufferBlock.PostRange方法的典型用法代码示例。如果您正苦于以下问题:C# BufferBlock.PostRange方法的具体用法?C# BufferBlock.PostRange怎么用?C# BufferBlock.PostRange使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BufferBlock的用法示例。


在下文中一共展示了BufferBlock.PostRange方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestLinkTo_MaxMessages

        public async Task TestLinkTo_MaxMessages()
        {
            Assert.Throws<ArgumentOutOfRangeException>(() => new DataflowLinkOptions { MaxMessages = -2 });
            Assert.Throws<ArgumentOutOfRangeException>(() => new DataflowLinkOptions { MaxMessages = 0 });

            const int MaxMessages = 3, ExtraMessages = 2;

            for (int mode = 0; mode < 3; mode++)
            {
                int consumedMessages = 0, remainingMessages = 0;
                var options = new DataflowLinkOptions() { MaxMessages = MaxMessages };
                var source = new BufferBlock<int>();
                var target = new ActionBlock<int>(x => consumedMessages++);
                var otherTarget = new ActionBlock<int>(x => remainingMessages++);

                switch (mode)
                {
                    case 0:
                        source.LinkTo(target, options);
                        break;
                    case 1:
                        source.LinkTo(target, options, x => true); // Injects FilteredLinkPropagator
                        break;
                    case 2:
                        using (source.LinkTo(target)) source.LinkTo(target, options); // Injects NopLinkPropagator
                        break;
                }
                source.LinkTo(otherTarget);

                source.PostRange(0, MaxMessages + ExtraMessages);
                source.Complete();
                await source.Completion;

                target.Complete();
                otherTarget.Complete();
                await Task.WhenAll(target.Completion, otherTarget.Completion);

                Assert.Equal(expected: MaxMessages, actual: consumedMessages);
                Assert.Equal(expected: ExtraMessages, actual: remainingMessages);
            }
        }
开发者ID:gitter-badger,项目名称:corefx,代码行数:41,代码来源:DataflowBlockExtensionTests.cs

示例2: TestLinkTo_Append

        public async Task TestLinkTo_Append()
        {
            var append = new DataflowLinkOptions() { Append = true, PropagateCompletion = true };
            var prepend = new DataflowLinkOptions() { Append = false, PropagateCompletion = true };

            var source = new BufferBlock<int>();
            var targets = new ActionBlock<int>[6];

            int[] consumedMessages = new int[targets.Length];
            for (int i = 0; i < targets.Length; i++)
            {
                int localI = i;
                targets[localI] = new ActionBlock<int>(x => consumedMessages[localI]++);
            }

            int lostMessages = 0;
            var extraTarget = new ActionBlock<int>(x => lostMessages++);

            // Link in a different order but use prepend/append to get them into expected/right order
            source.LinkTo(targets[2], prepend, x => x <= 2);
            source.LinkTo(targets[3], append, x => x <= 3);
            using (source.LinkTo(extraTarget, prepend))
            {
                source.LinkTo(targets[4], append, x => x <= 4);
                source.LinkTo(targets[1], prepend, x => x <= 1);
                using (source.LinkTo(extraTarget, append))
                {
                    source.LinkTo(targets[0], prepend, x => x <= 0);
                    source.LinkTo(targets[5], append, x => x <= 5);
                    using (source.LinkTo(extraTarget, prepend)) { }
                }
            }

            source.PostRange(0, targets.Length); // one message for each source
            source.Complete();
            await source.Completion;
            await Task.WhenAll(from target in targets select target.Completion);
            Assert.All(consumedMessages, i => Assert.Equal(expected: 1, actual: i));
            Assert.Equal(expected: 0, actual: lostMessages);
        }
开发者ID:gitter-badger,项目名称:corefx,代码行数:40,代码来源:DataflowBlockExtensionTests.cs

示例3: TestLinkTo_BasicLinking

        public async Task TestLinkTo_BasicLinking()
        {
            foreach (bool propagateCompletion in DataflowTestHelpers.BooleanValues)
            {
                int counter = 0;
                var source = new BufferBlock<int>();
                var target = new ActionBlock<int>(i => counter++);

                using (source.LinkTo(target, new DataflowLinkOptions { PropagateCompletion = propagateCompletion }))
                {
                    source.PostRange(0, 2);
                    source.Complete();
                    await source.Completion;

                    if (propagateCompletion)
                    {
                        await target.Completion;
                        Assert.Equal(expected: 2, actual: counter);
                    }
                    else
                    {
                        Assert.False(target.Completion.IsCompleted);
                    }
                }
            }

            var completedSource = new BufferBlock<int>();
            completedSource.Complete();
            await completedSource.Completion;
            using (completedSource.LinkTo(DataflowBlock.NullTarget<int>()))
            using (completedSource.LinkTo(DataflowBlock.NullTarget<int>()))
            {
                // just make sure we can link while completed
            }
        }
开发者ID:gitter-badger,项目名称:corefx,代码行数:35,代码来源:DataflowBlockExtensionTests.cs

示例4: TestLinkTo_Predicate

 public async Task TestLinkTo_Predicate()
 {
     int counter = 0;
     var source = new BufferBlock<int>();
     var target = new ActionBlock<int>(i => counter++);
     using (source.LinkTo(target, i => i % 2 == 0))
     using (source.LinkTo(DataflowBlock.NullTarget<int>()))
     {
         source.PostRange(0, 6);
         source.Complete();
         await source.Completion.ContinueWith(delegate { target.Complete(); }, TaskScheduler.Default);
         await target.Completion;
     }
     Assert.Equal(expected: 3, actual: counter);
 }
开发者ID:gitter-badger,项目名称:corefx,代码行数:15,代码来源:DataflowBlockExtensionTests.cs

示例5: TestEncapsulate_EncapsulateBoundedTarget

        public async Task TestEncapsulate_EncapsulateBoundedTarget()
        {
            // source->||BoundedTransform->buffer||->sink

            int messagesReceived = 0;
            var transform = new TransformBlock<int, int>(x => {
                messagesReceived++;
                return x;
            }, new ExecutionDataflowBlockOptions() { BoundedCapacity = 1 });

            var buffer = new BufferBlock<int>();
            transform.LinkTo(buffer);
            var ignored = transform.Completion.ContinueWith(completion => buffer.Complete(), TaskScheduler.Default);

            IPropagatorBlock<int, int> encapsulated = DataflowBlock.Encapsulate(transform, buffer);
            encapsulated.LinkTo(new ActionBlock<int>(x => { }));

            var source = new BufferBlock<int>();
            source.LinkTo(encapsulated);
            ignored = source.Completion.ContinueWith(completion => encapsulated.Complete(), TaskScheduler.Default);

            // Feed
            const int messagesSent = 10;
            source.PostRange(0, messagesSent);
            source.Complete();

            await encapsulated.Completion;
            Assert.Equal(messagesReceived, messagesSent);
        }
开发者ID:gitter-badger,项目名称:corefx,代码行数:29,代码来源:DataflowBlockExtensionTests.cs

示例6: TestAsObservableAndAsObserver_AsObservableDoesntConsume

        public void TestAsObservableAndAsObserver_AsObservableDoesntConsume()
        {
            var b = new BufferBlock<int>();
            b.PostRange(0, 2);

            Assert.Equal(expected: 2, actual: b.Count);
            Assert.NotNull(b.AsObservable());
            Assert.Equal(expected: 2, actual: b.Count);
        }
开发者ID:gitter-badger,项目名称:corefx,代码行数:9,代码来源:DataflowBlockExtensionTests.cs

示例7: TestReceiveAsync_LongChain

        public async Task TestReceiveAsync_LongChain()
        {
            const int Length = 10000;

            var bb = new BufferBlock<int>();

            Task t = bb.ReceiveAsync();
            for (int i = 1; i < Length; i++)
            {
                t = t.ContinueWith(_ => bb.ReceiveAsync(),
                    CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default).Unwrap();
            }

            bb.PostRange(0, Length);

            await t;
        }
开发者ID:gitter-badger,项目名称:corefx,代码行数:17,代码来源:DataflowBlockExtensionTests.cs

示例8: TestAsObservableAndAsObserver_DataPropagation

        public async Task TestAsObservableAndAsObserver_DataPropagation()
        {
            // Test that preset data flows correctly
            {
                var bb = new BufferBlock<int>();
                bb.PostRange(0, 2);
                bb.Complete();

                int nextValueExpected = 0;
                var ab = new ActionBlock<int>(i => {
                    Assert.True(i == nextValueExpected, string.Format("Expected next value to be {0} but got {1}", nextValueExpected, i));
                    nextValueExpected++;
                });

                bb.AsObservable().Subscribe(ab.AsObserver());
                await ab.Completion;
            }

            // Test that new data flows correctly
            {
                int nextValueExpected = -2;
                var ab = new ActionBlock<int>(i => {
                    Assert.True(i == nextValueExpected, string.Format("Expected next value to be {0} but got {1}", nextValueExpected, i));
                    nextValueExpected++;
                });

                var bb = new BufferBlock<int>();
                bb.AsObservable().Subscribe(ab.AsObserver());

                bb.PostRange(-2, 0);
                bb.Complete();

                await ab.Completion;
            }

            // Test that unsubscribing stops flow of data and stops completion
            {
                var target = new BufferBlock<int>();
                var source = new BufferBlock<int>();

                using (source.AsObservable().Subscribe(target.AsObserver()))
                {
                    source.PostItems(1, 2);
                    Assert.Equal(expected: 1, actual: await target.ReceiveAsync());
                    Assert.Equal(expected: 2, actual: await target.ReceiveAsync());
                }

                source.Post(3);
                var wb = new WriteOnceBlock<int>(i => i);
                source.LinkTo(wb);
                await wb.Completion;

                source.Complete();
                await source.Completion;

                Assert.False(target.Completion.IsCompleted);
            }
        }
开发者ID:gitter-badger,项目名称:corefx,代码行数:58,代码来源:DataflowBlockExtensionTests.cs

示例9: TestReceiveAsync_ManyInOrder

        public async Task TestReceiveAsync_ManyInOrder()
        {
            var bb = new BufferBlock<int>();

            Task<int>[] tasks = Enumerable.Range(0, 100).Select(_ => bb.ReceiveAsync()).ToArray();
            Assert.All(tasks, t => Assert.False(t.IsCompleted));

            bb.PostRange(0, tasks.Length);

            for (int i = 0; i < tasks.Length; i++)
            {
                Assert.Equal(expected: i, actual: await tasks[i]);
            }
        }
开发者ID:gitter-badger,项目名称:corefx,代码行数:14,代码来源:DataflowBlockExtensionTests.cs

示例10: TestLinkToOptions

        public async Task TestLinkToOptions()
        {
            const int Messages = 1;
            foreach (bool append in DataflowTestHelpers.BooleanValues)
            {
                var bb = new BufferBlock<int>();
                var values = new int[Messages];
                var targets = new ActionBlock<int>[Messages];
                for (int i = 0; i < Messages; i++)
                {
                    int slot = i;
                    targets[i] = new ActionBlock<int>(item => values[slot] = item);
                    bb.LinkTo(targets[i], new DataflowLinkOptions { MaxMessages = 1, Append = append });
                }

                bb.PostRange(0, Messages);
                bb.Complete();
                await bb.Completion;

                for (int i = 0; i < Messages; i++)
                {
                    Assert.Equal(
                        expected: append ? i : Messages - i - 1,
                        actual: values[i]);
                }
            }
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:27,代码来源:BufferBlockTests.cs

示例11: TestCircularLinking

 [OuterLoop] // waits for a period of time
 public async Task TestCircularLinking()
 {
     for (int boundedCapacity = 1; boundedCapacity <= 3; boundedCapacity++)
     {
         var b = new BufferBlock<int>(new DataflowBlockOptions { BoundedCapacity = boundedCapacity });
         b.PostRange(0, boundedCapacity);
         using (b.LinkTo(b))
         {
             await Task.Delay(200);
         }
         Assert.Equal(expected: boundedCapacity, actual: b.Count);
     }
 }
开发者ID:noahfalk,项目名称:corefx,代码行数:14,代码来源:BufferBlockTests.cs

示例12: TestReceives

        public void TestReceives()
        {
            for (int test = 0; test < 3; test++)
            {
                var bb = new BufferBlock<int>();
                bb.PostRange(0, 5);

                int item;
                switch (test)
                {
                    case 0:
                        IList<int> items;
                        Assert.True(bb.TryReceiveAll(out items));
                        Assert.Equal(expected: 5, actual: items.Count);
                        for (int i = 0; i < items.Count; i++)
                        {
                            Assert.Equal(expected: i, actual: items[i]);
                        }
                        Assert.False(bb.TryReceiveAll(out items));
                        break;

                    case 1:
                        for (int i = 0; i < 5; i++)
                        {
                            Assert.True(bb.TryReceive(f => true, out item));
                            Assert.Equal(expected: i, actual: item);
                        }
                        Assert.False(bb.TryReceive(f => true, out item));
                        break;

                    case 2:
                        for (int i = 0; i < 5; i++)
                        {
                            Assert.False(bb.TryReceive(f => f == i + 1, out item));
                            Assert.True(bb.TryReceive(f => f == i, out item));
                            Assert.Equal(expected: i, actual: item);
                        }
                        Assert.False(bb.TryReceive(f => true, out item));
                        break;
                }
            }
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:42,代码来源:BufferBlockTests.cs

示例13: TestYieldingNoResults

        public async Task TestYieldingNoResults()
        {
            foreach (int dop in new[] { 1, Environment.ProcessorCount })
            foreach (int boundedCapacity in new[] { DataflowBlockOptions.Unbounded, 1, 2 })
            {
                const int Modes = 3, Iters = 100;
                var tb = new TransformManyBlock<int, int>(i => {
                    switch (i % Modes)
                    {
                        default:
                        case 0:
                            return new List<int> { i };
                        case 1:
                            return new int[0];
                        case 2:
                            return new Collection<int> { i, i + 1 };
                    }
                }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = dop, BoundedCapacity = boundedCapacity });

                var source = new BufferBlock<int>();
                source.PostRange(0, Modes * Iters);
                source.Complete();
                source.LinkTo(tb, new DataflowLinkOptions { PropagateCompletion = true });

                int received = 0;
                while (await tb.OutputAvailableAsync())
                {
                    await tb.ReceiveAsync();
                    received++;
                }
                Assert.Equal(expected: Modes * Iters, actual: received);
            }
        }
开发者ID:GeneralRookie,项目名称:corefx,代码行数:33,代码来源:TransformManyBlockTests.cs


注:本文中的BufferBlock.PostRange方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。