Compare commits

...

5 Commits

5 changed files with 115 additions and 115 deletions

View File

@ -0,0 +1,43 @@
using System;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Xunit;
namespace NetCoreTests
{
public class TaskExtensionsTest
{
[Fact]
public async Task PropagateException()
{
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
await ThrowAsync().AsUniTask();
});
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
await ThrowOrValueAsync().AsUniTask();
});
}
[Fact]
public async Task PropagateWhenAllException()
{
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
await Task.WhenAll(ThrowAsync(), ThrowAsync(), ThrowAsync()).AsUniTask();
});
}
async Task ThrowAsync()
{
throw new InvalidOperationException();
}
async Task<int> ThrowOrValueAsync()
{
throw new InvalidOperationException();
}
}
}

View File

@ -20,6 +20,8 @@ namespace Cysharp.Threading.Tasks
{
ITriggerHandler<T> head; // head.prev is last
ITriggerHandler<T> iteratingHead;
bool preserveRemoveSelf;
ITriggerHandler<T> iteratingNode;
void LogError(Exception ex)
@ -42,7 +44,6 @@ namespace Cysharp.Threading.Tasks
while (h != null)
{
iteratingNode = h;
var next = h.Next;
try
{
@ -54,8 +55,19 @@ namespace Cysharp.Threading.Tasks
Remove(h);
}
if (preserveRemoveSelf)
{
preserveRemoveSelf = false;
iteratingNode = null;
var next = h.Next;
Remove(h);
h = next;
}
else
{
h = h.Next;
}
}
iteratingNode = null;
if (iteratingHead != null)
@ -85,6 +97,7 @@ namespace Cysharp.Threading.Tasks
LogError(ex);
}
preserveRemoveSelf = false;
iteratingNode = null;
var next = h.Next;
Remove(h);
@ -119,6 +132,7 @@ namespace Cysharp.Threading.Tasks
LogError(ex);
}
preserveRemoveSelf = false;
iteratingNode = null;
var next = h.Next;
Remove(h);
@ -153,6 +167,7 @@ namespace Cysharp.Threading.Tasks
LogError(ex);
}
preserveRemoveSelf = false;
iteratingNode = null;
var next = h.Next;
Remove(h);
@ -226,6 +241,13 @@ namespace Cysharp.Threading.Tasks
{
if (handler == null) throw new ArgumentNullException(nameof(handler));
if (iteratingNode != null && iteratingNode == handler)
{
// if remove self, reserve remove self after invoke completed.
preserveRemoveSelf = true;
}
else
{
var prev = handler.Prev;
var next = handler.Next;
@ -286,3 +308,4 @@ namespace Cysharp.Threading.Tasks
}
}
}
}

View File

@ -28,7 +28,7 @@ namespace Cysharp.Threading.Tasks
p.TrySetCanceled();
break;
case TaskStatus.Faulted:
p.TrySetException(x.Exception);
p.TrySetException(x.Exception.InnerException);
break;
case TaskStatus.RanToCompletion:
p.TrySetResult(x.Result);
@ -58,7 +58,7 @@ namespace Cysharp.Threading.Tasks
p.TrySetCanceled();
break;
case TaskStatus.Faulted:
p.TrySetException(x.Exception);
p.TrySetException(x.Exception.InnerException);
break;
case TaskStatus.RanToCompletion:
p.TrySetResult();

View File

@ -1,63 +0,0 @@
using Cysharp.Threading.Tasks;
using FluentAssertions;
using System;
using System.Collections;
using System.Threading;
using UnityEngine.TestTools;
namespace Cysharp.Threading.TasksTests
{
public class AsyncReactivePropertyTest
{
private int _callCounter;
[UnityTest]
public IEnumerator WaitCancelWait() => UniTask.ToCoroutine(async () =>
{
// Test case for https://github.com/Cysharp/UniTask/issues/444
var property = new AsyncReactiveProperty<int>(0);
var cts1 = new CancellationTokenSource();
var cts2 = new CancellationTokenSource();
WaitForProperty(property, cts1.Token);
WaitForProperty(property, cts2.Token);
_callCounter = 0;
property.Value = 1;
_callCounter.Should().Be(2);
cts2.Cancel();
cts2.Dispose();
cts1.Cancel();
cts1.Dispose();
var cts3 = new CancellationTokenSource();
WaitForProperty(property, cts3.Token);
_callCounter = 0;
property.Value = 2;
_callCounter.Should().Be(1);
cts3.Cancel();
cts3.Dispose();
await UniTask.CompletedTask;
});
private async void WaitForProperty(AsyncReactiveProperty<int> property, CancellationToken token)
{
while (true)
{
try
{
await property.WaitAsync(token);
_callCounter++;
}
catch (OperationCanceledException)
{
break;
}
}
}
}
}

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 27665955eefb4448969b8cc4dd204600
timeCreated: 1676129650