mirror of
https://github.com/Cysharp/UniTask.git
synced 2026-05-16 12:00:10 +00:00
exception testing
This commit is contained in:
126
src/UniTask.NetCoreTests/Linq/_Exception.cs
Normal file
126
src/UniTask.NetCoreTests/Linq/_Exception.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class UniTaskTestException : Exception
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<int> ThrowImmediate()
|
||||
{
|
||||
return UniTaskAsyncEnumerable.Throw<int>(new UniTaskTestException());
|
||||
}
|
||||
public static IUniTaskAsyncEnumerable<int> ThrowAfter()
|
||||
{
|
||||
return new ThrowAfter<int>(new UniTaskTestException());
|
||||
}
|
||||
public static IUniTaskAsyncEnumerable<int> ThrowInMoveNext()
|
||||
{
|
||||
return new ThrowIn<int>(new UniTaskTestException());
|
||||
}
|
||||
|
||||
|
||||
public static IEnumerable<IUniTaskAsyncEnumerable<int>> Throws()
|
||||
{
|
||||
yield return ThrowImmediate();
|
||||
yield return ThrowAfter();
|
||||
yield return ThrowInMoveNext();
|
||||
yield return UniTaskAsyncEnumerable.Range(1, 3).Concat(ThrowImmediate());
|
||||
yield return UniTaskAsyncEnumerable.Range(1, 3).Concat(ThrowAfter());
|
||||
yield return UniTaskAsyncEnumerable.Range(1, 3).Concat(ThrowInMoveNext());
|
||||
}
|
||||
}
|
||||
|
||||
internal class ThrowIn<TValue> : IUniTaskAsyncEnumerable<TValue>
|
||||
{
|
||||
readonly Exception exception;
|
||||
|
||||
public ThrowIn(Exception exception)
|
||||
{
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(exception, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : IUniTaskAsyncEnumerator<TValue>
|
||||
{
|
||||
readonly Exception exception;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
public Enumerator(Exception exception, CancellationToken cancellationToken)
|
||||
{
|
||||
this.exception = exception;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TValue Current => default;
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
ExceptionDispatchInfo.Capture(exception).Throw();
|
||||
return new UniTask<bool>(false);
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class ThrowAfter<TValue> : IUniTaskAsyncEnumerable<TValue>
|
||||
{
|
||||
readonly Exception exception;
|
||||
|
||||
public ThrowAfter(Exception exception)
|
||||
{
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new Enumerator(exception, cancellationToken);
|
||||
}
|
||||
|
||||
class Enumerator : IUniTaskAsyncEnumerator<TValue>
|
||||
{
|
||||
readonly Exception exception;
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
public Enumerator(Exception exception, CancellationToken cancellationToken)
|
||||
{
|
||||
this.exception = exception;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public TValue Current => default;
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var tcs = new UniTaskCompletionSource<bool>();
|
||||
|
||||
var awaiter = UniTask.Yield().GetAwaiter();
|
||||
awaiter.UnsafeOnCompleted(() =>
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
tcs.TrySetException(exception);
|
||||
});
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user