public Task GetTask(short token)
        {
            lock (this)
            {
                CheckTokenInsideLock(token);
                if (_task != null)
                {
                }
                else if (_exception is OperationCanceledException)
                {
                    _task = TaskUtils.TaskFactory <T> .Canceled;
                }
                else if (_exception != null)
                {
                    _task = TaskUtils.FromException <T>(_exception);
                }
                else if (_isComplete)
                {
                    _task = typeof(T) == typeof(Nothing) ? TaskUtils.CompletedTask : TaskUtils.TaskFactory <T> .FromResult(_result);
                }
                else
                {
                    _source = ValueTaskCompletionSource <T> .Create();

                    _task = _source.Task;
                }
                return(_task);
            }
        }
Example #2
0
        public async Task PartiallyCompletedCanceledNonGeneric2WhenAllTest()
        {
            var taskSources = new ValueTaskCompletionSource[3];

            for (var i = 0; i < taskSources.Count(); i++)
            {
                taskSources[i] = ValueTaskCompletionSource.Create();
            }

            taskSources[1].SetCanceled();


            var task = taskSources.Select(p => p.Task).WhenAll();

            Assert.IsFalse(task.IsCompleted);

            // Complete the tasks in non-sort order and wait some time in between
            taskSources[2].SetResult();
            await Task.Delay(10);

            taskSources[0].SetResult();

            Assert.IsTrue(task.IsCompleted);
            Assert.IsTrue(task.IsCanceled);

            await Assert.ThrowsExceptionAsync <TaskCanceledException>(async() =>
            {
                await task;
            });
        }
Example #3
0
        public async Task WhenAllTest()
        {
            var taskSources = new ValueTaskCompletionSource <int> [3];

            for (var i = 0; i < taskSources.Count(); i++)
            {
                taskSources[i] = ValueTaskCompletionSource <int> .Create();
            }

            var task = taskSources.Select(p => p.Task).WhenAll(preserveOrder: true);

            Assert.IsFalse(task.IsCompleted);

            // Complete the tasks in non-sort order and wait some time in between

            taskSources[1].SetResult(1);
            await Task.Delay(10);

            taskSources[2].SetResult(2);
            await Task.Delay(10);

            taskSources[0].SetResult(0);

            await Task.Delay(20); // Wait some time to allow the continuations to execute

            Assert.IsTrue(task.IsCompletedSuccessfully);
            var result = task.GetAwaiter().GetResult(); // We are allowed only once to get the result.

            Assert.IsNotNull(result);
            Assert.IsTrue(new[] { 0, 1, 2 }.SequenceEqual(result));
        }
Example #4
0
        public async Task PartiallyCompletedExceptionNonPreserveOrder2WhenAllTest()
        {
            var taskSources = new ValueTaskCompletionSource <int> [3];

            for (var i = 0; i < taskSources.Count(); i++)
            {
                taskSources[i] = ValueTaskCompletionSource <int> .Create();
            }

            taskSources[1].SetException(new CustomException());


            var task = taskSources.Select(p => p.Task).WhenAll(preserveOrder: false);

            Assert.IsFalse(task.IsCompleted);

            // Complete the tasks in non-sort order and wait some time in between
            taskSources[2].SetResult(2);
            await Task.Delay(10);

            taskSources[0].SetResult(0);

            Assert.IsTrue(task.IsCompleted);
            Assert.IsTrue(task.IsFaulted);

            await Assert.ThrowsExceptionAsync <CustomException>(async() =>
            {
                await task;
            });
        }
Example #5
0
        public async Task WhenAllNonGenericTest()
        {
            var taskSources = new ValueTaskCompletionSource[3];

            for (var i = 0; i < taskSources.Count(); i++)
            {
                taskSources[i] = ValueTaskCompletionSource.Create();
            }

            var task = taskSources.Select(p => p.Task).WhenAll();

            Assert.IsFalse(task.IsCompleted);

            // Complete the tasks in non-sort order and wait some time in between

            taskSources[1].SetResult();
            await Task.Delay(10);

            taskSources[2].SetResult();
            await Task.Delay(10);

            taskSources[0].SetResult();

            Assert.IsTrue(task.IsCompletedSuccessfully);
            task.GetAwaiter().GetResult();
        }
Example #6
0
        public async Task PartiallyCompletedCanceled2WhenAllTest()
        {
            var taskSources = new ValueTaskCompletionSource <int> [3];

            for (var i = 0; i < taskSources.Count(); i++)
            {
                taskSources[i] = ValueTaskCompletionSource <int> .Create();
            }

            taskSources[1].SetCanceled();


            var task = taskSources.Select(p => p.Task).WhenAll(preserveOrder: true);

            Assert.IsFalse(task.IsCompleted);

            // Complete the tasks in non-sort order and wait some time in between
            taskSources[2].SetResult(2);
            await Task.Delay(10);

            taskSources[0].SetResult(0);

            await Task.Delay(20); // Wait some time to allow the continuations to execute

            Assert.IsTrue(task.IsCompleted);
            Assert.IsTrue(task.IsCanceled);

            await Assert.ThrowsExceptionAsync <TaskCanceledException>(async() =>
            {
                await task;
            });
        }
Example #7
0
 private void EnsureHasTask()
 {
     if (!_source.HasTask)
     {
         _source = ValueTaskCompletionSource <T> .Create();
     }
 }
Example #8
0
        public static Task <T> FromException <T>(Exception exception)
        {
            var source = ValueTaskCompletionSource <T> .Create();

            source.TrySetException(exception);
            return(source.Task);
        }
Example #9
0
        public void DefaultTask(bool shouldFault)
        {
            Counters.Reset();
            var source = ValueTaskCompletionSource <int> .Create();

            Assert.Equal(1, Counters.TaskAllocated.Value);
            Assert.True(source.IsOptimized);

            Verify(source, shouldFault);
        }
Example #10
0
        public static ValueTask WaitForExitAsync(this Process process, CancellationToken cancellation = default)
        {
            var tcs = ValueTaskCompletionSource.Create();

#pragma warning disable CA1062
            process.EnableRaisingEvents = true;
#pragma warning restore CA1062
            process.Exited += (s, o) => tcs.TrySetResult();

            // This is needed in order to prevent a race condition when the process exits before we can setup our event handler.
            process.Refresh();
            if (process.HasExited)
            {
                tcs.TrySetResult();
            }

            if (cancellation.CanBeCanceled)
            {
                cancellation.Register(() => tcs.TrySetCanceled());
            }

            return(tcs.Task);
        }