| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Reflection;
- using System.Threading;
- using System.Threading.Tasks;
- using Xunit;
- using FluentAssertions;
- namespace Tests
- {
- public partial class AsyncTests
- {
- #pragma warning disable xUnit1013 // Public method should be marked as test
- public void AssertThrows<E>(Action a)
- where E : Exception
- {
- Assert.Throws<E>(a);
- }
- [Obsolete("Don't use this, use Assert.ThrowsAsync and await it", true)]
- public Task AssertThrows<E>(Func<Task> func)
- where E : Exception
- {
- return Assert.ThrowsAsync<E>(func);
- }
- public void AssertThrows<E>(Action a, Func<E, bool> assert)
- where E : Exception
- {
- var hasFailed = false;
- try
- {
- a();
- }
- catch (E e)
- {
- Assert.True(assert(e));
- hasFailed = true;
- }
- if (!hasFailed)
- {
- Assert.True(false);
- }
- }
- public void NoNext<T>(IAsyncEnumerator<T> e)
- {
- Assert.False(e.MoveNext().Result);
- }
- public void HasNext<T>(IAsyncEnumerator<T> e, T value)
- {
- Assert.True(e.MoveNext().Result);
- Assert.Equal(value, e.Current);
- }
- public async Task SequenceIdentity<T>(IAsyncEnumerable<T> enumerable)
- {
- var en1 = enumerable.GetAsyncEnumerator();
- var en2 = enumerable.GetAsyncEnumerator();
- Assert.Equal(en1.GetType(), en2.GetType());
- en1.Dispose();
- en2.Dispose();
- var e1t = enumerable.ToList();
- var e2t = enumerable.ToList();
- await Task.WhenAll(e1t, e2t);
- var e1Result = e1t.Result;
- var e2Result = e2t.Result;
- e1Result.ShouldAllBeEquivalentTo(e2Result);
- }
- #pragma warning restore xUnit1013 // Public method should be marked as test
- /// <summary>
- /// Waits WaitTimeoutMs or until cancellation is requested. If cancellation was not requested, MoveNext returns true.
- /// </summary>
- internal sealed class CancellationTestAsyncEnumerable : IAsyncEnumerable<int>
- {
- private readonly int iterationsBeforeDelay;
- public CancellationTestAsyncEnumerable(int iterationsBeforeDelay = 0)
- {
- this.iterationsBeforeDelay = iterationsBeforeDelay;
- }
- IAsyncEnumerator<int> IAsyncEnumerable<int>.GetAsyncEnumerator() => GetEnumerator();
- public TestEnumerator GetEnumerator() => new TestEnumerator(iterationsBeforeDelay);
- internal sealed class TestEnumerator : IAsyncEnumerator<int>
- {
- private readonly int iterationsBeforeDelay;
- public TestEnumerator(int iterationsBeforeDelay)
- {
- this.iterationsBeforeDelay = iterationsBeforeDelay;
- }
- int i = -1;
- public void Dispose()
- {
- }
- public bool MoveNextWasCalled { get; private set; }
- public int Current => i;
- public async Task<bool> MoveNextAsync()
- {
- MoveNextWasCalled = true;
- i++;
- if (Current >= iterationsBeforeDelay)
- {
- await Task.Delay(WaitTimeoutMs);
- }
- return true;
- }
- }
- }
- }
- }
|