예제 #1
0
        public void Ctor_NotYetCompleted(int completionMode, TaskCompletionSource <int> tcs, ValueAwaiter <int> va)
        {
            Assert.False(tcs.Task.IsCompleted);
            Assert.False(va.IsCompleted);

            var    are    = new AutoResetEvent(false);
            Action action = () => are.Set();

            switch (completionMode)
            {
            case 0: va.OnCompleted(action); break;

            case 1: va.UnsafeOnCompleted(action); break;
            }

            tcs.SetResult(42);
            are.WaitOne();

            Assert.True(va.IsCompleted);
            Assert.Equal(42, va.GetResult());
        }
예제 #2
0
        public void Ctor_AlreadyCompletedInput(ValueAwaiter <string> va)
        {
            Assert.True(va.IsCompleted);
            Assert.Equal("hello", va.GetResult());

            int threadId = Environment.CurrentManagedThreadId;
            var are      = new AutoResetEvent(false);

            va.OnCompleted(() =>
            {
                Assert.NotEqual(threadId, Environment.CurrentManagedThreadId);
                are.Set();
            });
            are.WaitOne();

            va.UnsafeOnCompleted(() =>
            {
                Assert.NotEqual(threadId, Environment.CurrentManagedThreadId);
                are.Set();
            });
            are.WaitOne();
        }
예제 #3
0
        private async Task FlushAsyncAwaited(ValueAwaiter <FlushResult> awaitable, long count, CancellationToken cancellationToken)
        {
            // https://github.com/dotnet/corefxlab/issues/1334
            // Since the flush awaitable doesn't currently support multiple awaiters
            // we need to use a task to track the callbacks.
            // All awaiters get the same task
            lock (_flushLock)
            {
                if (_flushTcs == null || _flushTcs.Task.IsCompleted)
                {
                    _flushTcs = new TaskCompletionSource <object>();

                    awaitable.OnCompleted(_flushCompleted);
                }
            }

            _timeoutControl.StartTimingWrite(count);
            await _flushTcs.Task;

            _timeoutControl.StopTimingWrite();

            cancellationToken.ThrowIfCancellationRequested();
        }