Пример #1
0
        public async Task TakeFromAnyCancel()
        {               //****************************************
            var MyCollections = new AsyncCollection <int>[] { new AsyncCollection <int>(), new AsyncCollection <int>() };
            ValueTask <CollectionTakeResult <int> > MyTask;

            //****************************************

            using (var MySource = new CancellationTokenSource())
            {
                MyTask = AsyncCollection <int> .TakeFromAny(MyCollections, MySource.Token);

                Assert.IsFalse(MyTask.IsCompleted, "Added too early");

                MySource.Cancel();
            }

            try
            {
                var MyResult = await MyTask;

                Assert.Fail("Task succeeded unexpectedly");
            }
            catch (OperationCanceledException)
            {
            }
        }
Пример #2
0
        public async Task TakeFromAnyMulti()
        {               //****************************************
            var MyCollections = new AsyncCollection <int>[] { new AsyncCollection <int>(), new AsyncCollection <int>() };
            //****************************************

            var MyTask1 = AsyncCollection <int> .TakeFromAny(MyCollections);

            var MyTask2 = AsyncCollection <int> .TakeFromAny(MyCollections);

            await MyCollections[0].Add(42);
            await MyCollections[1].Add(84);

            var MyResult1 = await MyTask1;
            var MyResult2 = await MyTask2;

            //****************************************

            Assert.IsTrue(MyResult1.HasItem, "No item");
            CollectionAssert.Contains(MyCollections, MyResult1.Source, "Result is not an expected collection");


            Assert.IsTrue(MyResult2.HasItem, "No item");
            CollectionAssert.Contains(MyCollections, MyResult2.Source, "Result is not an expected collection");

            Assert.AreNotSame(MyResult1.Source, MyResult2.Source, "Same collection for both takes");
            Assert.AreNotEqual(MyResult1.Item, MyResult2.Item, "Same result for both takes");
        }
Пример #3
0
        [Test]        //, MaxTime(1000)]
        public async Task TakeFromAnyAddComplete()
        {             //****************************************
            var MyCollections = new AsyncCollection <int>[] { new AsyncCollection <int>(), new AsyncCollection <int>() };
            //****************************************

            var MyTask = AsyncCollection <int> .TakeFromAny(MyCollections);

            await MyCollections[0].AddComplete(42);

            //****************************************

            var MyResult = await MyTask;

            await Task.Yield();

            Thread.Sleep(100);             // Cancellations happen after the task is completed

            Assert.IsTrue(MyResult.HasItem, "Take result failed unexpectedly");
            Assert.AreSame(MyCollections[0], MyResult.Source, "Collection was not as expected");
            Assert.AreEqual(42, MyResult.Item, "Item was not as expected");

            Assert.AreEqual(0, MyCollections[0].Count, "Item not removed");
            Assert.AreEqual(0, MyCollections[0].WaitingToTake, "Tasks unexpectedly waiting");

            Assert.AreEqual(0, MyCollections[1].Count, "Counter decremented");
            Assert.AreEqual(0, MyCollections[1].WaitingToTake, "Tasks unexpectedly waiting");

            Assert.IsTrue(MyCollections[0].IsCompleted, "Not completed");
        }
Пример #4
0
        public void TakeFromAny()
        {               //****************************************
            var MyCollections = new AsyncCollection <int>[] { new AsyncCollection <int>(), new AsyncCollection <int>() };
            //****************************************

            var MyTask = AsyncCollection <int> .TakeFromAny(MyCollections);

            //****************************************

            Assert.IsFalse(MyTask.IsCompleted, "Task completed unexpectedly");
        }
Пример #5
0
        public async Task TakeFromAnyInitial([Values(0, 1)] int index)
        {               //****************************************
            var MyCollections = new AsyncCollection <int>[] { new AsyncCollection <int>(), new AsyncCollection <int>() };
            //****************************************

            await MyCollections[index].Add(42);

            var MyTask = AsyncCollection <int> .TakeFromAny(MyCollections);

            //****************************************

            Assert.IsTrue(MyTask.IsCompleted, "Task completed unexpectedly");

            Assert.IsTrue(MyTask.Result.HasItem, "Take result failed unexpectedly");
            Assert.AreSame(MyCollections[index], MyTask.Result.Source, "Collection was not as expected");
            Assert.AreEqual(42, MyTask.Result.Item, "Item was not as expected");
        }
Пример #6
0
        public async Task TakeFromAnySingle()
        {               //****************************************
            var MyCollections = new AsyncCollection <int>[] { new AsyncCollection <int>(), new AsyncCollection <int>() };
            //****************************************

            var MyTask1 = AsyncCollection <int> .TakeFromAny(MyCollections).AsTask();

            var MyTask2 = AsyncCollection <int> .TakeFromAny(MyCollections).AsTask();

            await MyCollections[0].Add(42);

            var MyTask = await Task.WhenAny(MyTask1, MyTask2);

            //****************************************

            Assert.IsTrue(MyTask.Result.HasItem, "No item");
            CollectionAssert.Contains(MyCollections, MyTask.Result.Source, "Result is not an expected collection");
            Assert.AreEqual(42, MyTask.Result.Item, "Result item is not as expected");
        }
Пример #7
0
        public async Task TakeFromAnyMultiCancel()
        {               //****************************************
            var MyCollections = new AsyncCollection <int>[] { new AsyncCollection <int>(), new AsyncCollection <int>() };

            //****************************************

            using var MySource = new CancellationTokenSource();

            var MyTask1 = AsyncCollection <int> .TakeFromAny(MyCollections, MySource.Token);

            var MyTask2 = AsyncCollection <int> .TakeFromAny(MyCollections);

            MySource.Cancel();

            await MyCollections[0].Add(42);
            await MyCollections[1].Add(84);

            try
            {
                var MyResult1 = await MyTask1;

                Assert.Fail("Task succeeded unexpectedly");
            }
            catch (OperationCanceledException)
            {
            }

            var MyResult2 = await MyTask2;

            //****************************************

            Assert.IsTrue(MyResult2.HasItem, "No item");
            CollectionAssert.Contains(MyCollections, MyResult2.Source, "Result is not an expected collection");

            Assert.IsFalse(MyCollections[0].Count == 0 && MyCollections[1].Count == 0, "All items were removed");
        }
Пример #8
0
        public async Task TakeFromAnyComplete()
        {               //****************************************
            var MyCollections = new AsyncCollection <int>[] { new AsyncCollection <int>(), new AsyncCollection <int>() };
            ValueTask <CollectionTakeResult <int> > MyTask;

            //****************************************

            MyTask = AsyncCollection <int> .TakeFromAny(MyCollections);

            Assert.IsFalse(MyTask.IsCompleted, "Added too early");

            _ = MyCollections[0].CompleteAdding();
            _ = MyCollections[1].CompleteAdding();

            try
            {
                var MyResult = await MyTask;

                Assert.Fail("Task succeeded unexpectedly");
            }
            catch (InvalidOperationException)
            {
            }
        }