Beispiel #1
0
        public void PriorityProducerConsumer_NoPending_Duplicates3()
        {
            using var ppc = new PriorityProducerConsumer <int>(3, true);
            ppc.Produce(5, 1);
            ppc.Produce(6, 1);
            ppc.Produce(5, 1);
            var consumerTask1 = ppc.ConsumeAsync();
            var consumerTask2 = ppc.ConsumeAsync();
            var consumerTask3 = ppc.ConsumeAsync();

            Assert.AreEqual(TaskStatus.RanToCompletion, consumerTask1.Status);
            Assert.AreEqual(TaskStatus.RanToCompletion, consumerTask2.Status);
            Assert.AreEqual(TaskStatus.WaitingForActivation, consumerTask3.Status);
            Assert.AreEqual(6, consumerTask1.Result);
            Assert.AreEqual(5, consumerTask2.Result);
        }
        public async Task PriorityProducerConsumer_Pending_Priority3()
        {
            var ppc          = new PriorityProducerConsumer <int>(2);
            var values       = new int[3];
            var tcsConsumer  = new TaskCompletionSource <bool>();
            var tcsProducer  = new TaskCompletionSource <bool>();
            var consumerTask = Task.Run(async() => {
                for (var i = 0; i < 3; i++)
                {
                    var task = ppc.ConsumeAsync();
                    tcsConsumer.TrySetResult(true);
                    values[i] = await task;
                    await tcsProducer.Task;
                }
            });

            Assert.AreEqual(TaskStatus.WaitingForActivation, consumerTask.Status);

            await tcsConsumer.Task;

            ppc.Produce(5, 1);
            ppc.Produce(6, 1);
            ppc.Produce(7);
            tcsProducer.SetResult(false);

            await consumerTask;

            Assert.AreEqual(TaskStatus.RanToCompletion, consumerTask.Status);
            Assert.AreEqual(5, values[0]);
            Assert.AreEqual(7, values[1]);
            Assert.AreEqual(6, values[2]);
        }
 public void PriorityProducerConsumer_NoPending()
 {
     using (var ppc = new PriorityProducerConsumer <int>()) {
         ppc.Produce(5);
         var consumerTask = ppc.ConsumeAsync();
         Assert.AreEqual(TaskStatus.RanToCompletion, consumerTask.Status);
         Assert.AreEqual(5, consumerTask.Result);
     }
 }
        public async Task PriorityProducerConsumer_Pending_Dispose()
        {
            var ppc          = new PriorityProducerConsumer <int>();
            var consumerTask = ppc.ConsumeAsync();

            Assert.AreEqual(TaskStatus.WaitingForActivation, consumerTask.Status);

            ppc.Dispose();
            await consumerTask.ContinueWith(t => { });

            Assert.AreEqual(TaskStatus.Canceled, consumerTask.Status);
        }
        public async Task PriorityProducerConsumer_Pending_Priority2()
        {
            var ppc           = new PriorityProducerConsumer <int>(2);
            var consumerTask1 = ppc.ConsumeAsync();
            var consumerTask2 = ppc.ConsumeAsync();

            Assert.AreEqual(TaskStatus.WaitingForActivation, consumerTask1.Status);
            Assert.AreEqual(TaskStatus.WaitingForActivation, consumerTask2.Status);

            ppc.Produce(6, 1);
            await consumerTask1;

            Assert.AreEqual(TaskStatus.RanToCompletion, consumerTask1.Status);
            Assert.AreEqual(TaskStatus.WaitingForActivation, consumerTask2.Status);
            Assert.AreEqual(6, consumerTask1.Result);

            ppc.Produce(5);
            await consumerTask2;

            Assert.AreEqual(TaskStatus.RanToCompletion, consumerTask2.Status);
            Assert.AreEqual(5, consumerTask2.Result);
        }
        public async Task PriorityProducerConsumer_Pending()
        {
            using (var ppc = new PriorityProducerConsumer <int>()) {
                var consumerTask = ppc.ConsumeAsync();
                Assert.AreEqual(TaskStatus.WaitingForActivation, consumerTask.Status);

                ppc.Produce(5);
                await consumerTask;

                Assert.AreEqual(TaskStatus.RanToCompletion, consumerTask.Status);
                Assert.AreEqual(5, consumerTask.Result);
            }
        }
Beispiel #7
0
        public async Task PriorityProducerConsumer_ExcludeDuplicates_HigherPriority()
        {
            using var ppc = new PriorityProducerConsumer <int>(maxPriority: 2, excludeDuplicates: true);

            ppc.Produce(value: 1, priority: 1);
            ppc.Produce(value: 2, priority: 1);
            ppc.Produce(value: 3, priority: 1);
            ppc.Produce(value: 1, priority: 0);
            ppc.Produce(value: 2, priority: 0);

            var data1 = await ppc.ConsumeAsync(CancellationToken.None);

            data1.Should().Be(1);

            var data2 = await ppc.ConsumeAsync(CancellationToken.None);

            data2.Should().Be(2);

            var data3 = await ppc.ConsumeAsync(CancellationToken.None);

            data3.Should().Be(3);
        }
Beispiel #8
0
            private async Task ConsumerLoop()
            {
                while (!_ppc.IsDisposed)
                {
                    try {
                        var item = await _ppc.ConsumeAsync();

                        if (item.IsAwaitable)
                        {
                            var disposable = new PrioritizerDisposable(_ppc.CancellationToken);
                            item.SetResult(disposable);
                            await disposable.Task;
                        }
                        else
                        {
                            item.SetResult(EmptyDisposable.Instance);
                        }
                    } catch (OperationCanceledException) when(_ppc.IsDisposed)
                    {
                        return;
                    }
                }
            }