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);
            }
        }
Beispiel #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;
            });
        }
Beispiel #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));
        }
Beispiel #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;
            });
        }
Beispiel #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();
        }
Beispiel #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;
            });
        }
Beispiel #7
0
 private void EnsureHasTask()
 {
     if (!_source.HasTask)
     {
         _source = ValueTaskCompletionSource <T> .Create();
     }
 }
Beispiel #8
0
        public static Task <T> FromException <T>(Exception exception)
        {
            var source = ValueTaskCompletionSource <T> .Create();

            source.TrySetException(exception);
            return(source.Task);
        }
Beispiel #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);
        }
Beispiel #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);
        }