Esempio n. 1
0
        public async Task TestCancelInOffering()
        {
            var iterator    = new TestIterator(3);
            var cts         = new CancellationTokenSource(50);
            var testBlock   = iterator.AsSourceBlock(cts.Token);
            var targetBlock = new BufferBlock <int>(new DataflowBlockOptions()
            {
                BoundedCapacity = 1
            });

            testBlock.LinkTo(targetBlock);

            // When link is added, call MoveNext once and buffer the result.
            // The task is not completed until the buffered value is consumed.
            await testBlock.Completion.NeverComplete();

            // targetBlock received 1 soon after linking
            (await targetBlock.ReceiveAsync(TestUtils.SometimeSoon)).Is(1);

            // 2 was buffered and targetBlock received it soon after the previous line
            (await targetBlock.ReceiveAsync(TestUtils.SometimeSoon)).Is(2);

            // The cancel is requested and no buffer in testBlock
            await testBlock.Completion.CanceledSoon();

            // targetBlock never receives 3
            await Assert.ThrowsAnyAsync <TimeoutException>(() => targetBlock.ReceiveAsync(TestUtils.SometimeSoon));
        }
Esempio n. 2
0
        public async Task TestCancelBeforeLink()
        {
            var iterator  = new TestIterator(3);
            var cts       = new CancellationTokenSource();
            var testBlock = iterator.AsSourceBlock(cts.Token);

            cts.Cancel();
            await testBlock.Completion.CanceledSoon();
        }
Esempio n. 3
0
        public async Task TestRange()
        {
            var iterator    = new TestIterator(3);
            var testBlock   = iterator.AsSourceBlock();
            var targetBlock = new BufferBlock <int>();

            testBlock.LinkTo(targetBlock);
            await testBlock.Completion.CompleteSoon();

            iterator.Disposed.IsTrue();

            targetBlock.TryReceiveAll(out var values).IsTrue();
            values.Is(1, 2, 3);
        }
Esempio n. 4
0
        public void TestReceive()
        {
            var iterator  = new TestIterator(3);
            var testBlock = iterator.AsSourceBlock().IsInstanceOf <IReceivableSourceBlock <int> >();

            // Cannot receive item before link
            testBlock.TryReceive(null, out _).IsFalse();
            iterator.Enumerating.IsFalse();

            // Can receive with link
            testBlock.Receive(TestUtils.SometimeSoon).Is(1);
            iterator.Enumerating.IsTrue();

            // Does not MoveNext after unlink
            testBlock.TryReceive(null, out _).IsFalse();
        }
        public async Task TestDisposeEnumerator(int receiveCount)
        {
            var iterator  = new TestIterator(2);
            var cts       = new CancellationTokenSource();
            var testBlock = iterator.AsSourceBlock(cts.Token);

            for (var i = 1; i <= receiveCount; i++)
            {
                (await testBlock.ReceiveAsync(TestUtils.SometimeSoon)).Is(i);
            }

            cts.Cancel();

            await testBlock.Completion.CanceledSoon();

            iterator.Disposed.IsTrue();
        }
Esempio n. 6
0
        public async Task TestCancelInOffering()
        {
            var iterator    = new TestIterator(3);
            var cts         = new CancellationTokenSource();
            var testBlock   = iterator.AsSourceBlock(cts.Token);
            var targetBlock = new TransformWithoutBufferBlock <int, int>(x => x);

            testBlock.LinkTo(targetBlock);
            cts.Cancel();

            // When link is added, call MoveNext once and buffer the result.
            // The task is not completed until the buffered value is consumed.
            await testBlock.Completion.NeverComplete();

            (await targetBlock.ReceiveAsync(TestUtils.SometimeSoon)).Is(1);

            await testBlock.Completion.CanceledSoon();
        }