Ejemplo n.º 1
0
        public async Task WhenAny_CompletesWhenAnyOperationCompletes()
        {
            // Arrange
            var op1 = AsyncResult.Delay(1);
            var op2 = AsyncResult.Delay(Timeout.Infinite);

            // Act
            await AsyncResult.WhenAny(op1, op2);

            // Assert
            AssertCompleted(op1);
        }
Ejemplo n.º 2
0
        public async Task ToTask_CompletesWhenSourceCompletes()
        {
            // Arrange
            var op   = AsyncResult.Delay(1);
            var task = op.ToTask();

            // Act
            await task;

            // Assert
            Assert.True(op.IsCompleted);
        }
Ejemplo n.º 3
0
        public async Task WhenAll_CompletesWhenAllOperationsCompleted()
        {
            // Arrange
            var op1 = AsyncResult.Delay(1);
            var op2 = AsyncResult.Delay(2);

            // Act
            await AsyncResult.WhenAll(op1, op2);

            // Assert
            AssertCompleted(op1);
            AssertCompleted(op2);
        }
Ejemplo n.º 4
0
        public async Task TryAddCompletionCallback_ExecutesWhenOperationCompletes()
        {
            // Arrange
            var op             = AsyncResult.Delay(1);
            var callbackCalled = false;

            op.TryAddCompletionCallback(_ => callbackCalled = true, null);

            // Act
            await op;

            // Assert
            Assert.True(callbackCalled);
        }
Ejemplo n.º 5
0
        public async Task Unwrap_SucceedsIfBothOperationsSucceed()
        {
            // Arrange
            var op1 = AsyncResult.Delay(1);
            var op2 = AsyncResult.FromResult(op1);

            // Act
            var   op = op2.Unwrap();
            await op;

            // Assert
            Assert.True(op.IsCompletedSuccessfully);
            Assert.True(op1.IsCompleted);
            Assert.True(op2.IsCompleted);
        }
Ejemplo n.º 6
0
        public async Task ContinueWth_ExecutesOnAntecedentSuccess(AsyncContinuationOptions options, bool expectedCalled)
        {
            // Arrange
            var op = AsyncResult.Delay(1);
            var continuationCalled   = false;
            var continuationCanceled = false;
            var continuation         = op.ContinueWith(o => continuationCalled = true, options);

            // Act
            try
            {
                await continuation;
            }
            catch (OperationCanceledException)
            {
                continuationCanceled = true;
            }

            // Assert
            Assert.Equal(expectedCalled, continuationCalled);
            Assert.Equal(!expectedCalled, continuationCanceled);
        }
Ejemplo n.º 7
0
        public async Task Retry_CompletesWhenSourceCompletes()
        {
            // Arrange
            var counter = 3;

            IAsyncOperation OpFactory()
            {
                if (--counter > 0)
                {
                    return(AsyncResult.FromException(new Exception()));
                }
                else
                {
                    return(AsyncResult.Delay(1));
                }
            }

            // Act
            var   op = AsyncResult.Retry(OpFactory, 1);
            await op;

            // Assert
            AssertCompleted(op);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Creates an operation that completes after a time delay.
 /// </summary>
 /// <param name="delay">The time span to wait before completing the returned operation, or <c>TimeSpan.FromMilliseconds(-1)</c> to wait indefinitely.</param>
 /// <exception cref="ArgumentOutOfRangeException">Thrown if the <paramref name="delay"/> represents a negative time interval other than <c>TimeSpan.FromMillseconds(-1)</c>.</exception>
 /// <returns>An operation that represents the time delay.</returns>
 /// <seealso cref="Delay(int)"/>
 /// <seealso cref="Delay(float)"/>
 public static IAsyncOperation Delay(TimeSpan delay)
 {
     return(AsyncResult.Delay(delay, GetRootBehaviour().UpdateSource));
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Creates an operation that completes after a time delay.
 /// </summary>
 /// <param name="secondsDelay">The number of seconds to wait before completing the returned operation, or -1 to wait indefinitely.</param>
 /// <exception cref="ArgumentOutOfRangeException">Thrown if the <paramref name="secondsDelay"/> is less than -1.</exception>
 /// <returns>An operation that represents the time delay.</returns>
 /// <seealso cref="Delay(int)"/>
 /// <seealso cref="Delay(TimeSpan)"/>
 public static IAsyncOperation Delay(float secondsDelay)
 {
     return(AsyncResult.Delay(secondsDelay, GetRootBehaviour().UpdateSource));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Creates an operation that completes after a time delay.
 /// </summary>
 /// <param name="millisecondsDelay">The number of milliseconds to wait before completing the returned operation, or -1 to wait indefinitely.</param>
 /// <exception cref="ArgumentOutOfRangeException">Thrown if the <paramref name="millisecondsDelay"/> is less than -1.</exception>
 /// <returns>An operation that represents the time delay.</returns>
 /// <seealso cref="Delay(float)"/>
 /// <seealso cref="Delay(TimeSpan)"/>
 public static IAsyncOperation Delay(int millisecondsDelay)
 {
     return(AsyncResult.Delay(millisecondsDelay, _rootBehaviour.UpdateSource));
 }