Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check to that AutoResetUniTaskSource already returned to the pool #503

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/UniTask.NetCoreTests/UniTaskCompletionSourceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using FluentAssertions;
using NetCoreTests.Linq;
using Xunit;

namespace NetCoreTests
{
public class AutoResetUniTaskCompletionSourceTest
{
[Fact]
public async Task SetResultAfterReturn()
{
var source1 = AutoResetUniTaskCompletionSource.Create();
source1.TrySetResult();
await source1.Task;

source1.TrySetResult().Should().BeFalse();

var source2 = AutoResetUniTaskCompletionSource.Create();
source2.TrySetResult();
await source2.Task;

source2.TrySetResult().Should().BeFalse();
}

[Fact]
public async Task SetCancelAfterReturn()
{
var source = AutoResetUniTaskCompletionSource.Create();
source.TrySetResult();
await source.Task;

source.TrySetCanceled().Should().BeFalse();
}

[Fact]
public async Task SetExceptionAfterReturn()
{
var source = AutoResetUniTaskCompletionSource.Create();
source.TrySetResult();
await source.Task;

source.TrySetException(new UniTaskTestException()).Should().BeFalse();
}

[Fact]
public async Task SetResultWithValueAfterReturn()
{
var source1 = AutoResetUniTaskCompletionSource<int>.Create();
source1.TrySetResult(100);
(await source1.Task).Should().Be(100);

source1.TrySetResult(100).Should().BeFalse();

var source2 = AutoResetUniTaskCompletionSource.Create();
source2.TrySetResult();
await source2.Task;
source2.TrySetResult().Should().BeFalse();
}

[Fact]
public async Task SetCancelWithValueAfterReturn()
{
var source = AutoResetUniTaskCompletionSource<int>.Create();
source.TrySetResult(100);
(await source.Task).Should().Be(100);
source.TrySetCanceled().Should().BeFalse();
}

[Fact]
public async Task SetExceptionWithValueAfterReturn()
{
var source = AutoResetUniTaskCompletionSource<int>.Create();
source.TrySetResult(100);
(await source.Task).Should().Be(100);
source.TrySetException(new UniTaskTestException()).Should().BeFalse();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ static AutoResetUniTaskCompletionSource()
}

UniTaskCompletionSourceCore<AsyncUnit> core;
short version;

AutoResetUniTaskCompletionSource()
{
Expand All @@ -340,6 +341,7 @@ public static AutoResetUniTaskCompletionSource Create()
{
result = new AutoResetUniTaskCompletionSource();
}
result.version = result.core.Version;
TaskTracker.TrackActiveTask(result, 2);
return result;
}
Expand Down Expand Up @@ -383,19 +385,19 @@ public UniTask Task
[DebuggerHidden]
public bool TrySetResult()
{
return core.TrySetResult(AsyncUnit.Default);
return version == core.Version && core.TrySetResult(AsyncUnit.Default);
}

[DebuggerHidden]
public bool TrySetCanceled(CancellationToken cancellationToken = default)
{
return core.TrySetCanceled(cancellationToken);
return version == core.Version && core.TrySetCanceled(cancellationToken);
}

[DebuggerHidden]
public bool TrySetException(Exception exception)
{
return core.TrySetException(exception);
return version == core.Version && core.TrySetException(exception);
}

[DebuggerHidden]
Expand All @@ -409,7 +411,6 @@ public void GetResult(short token)
{
TryReturn();
}

}

[DebuggerHidden]
Expand Down Expand Up @@ -451,6 +452,7 @@ static AutoResetUniTaskCompletionSource()
}

UniTaskCompletionSourceCore<T> core;
short version;

AutoResetUniTaskCompletionSource()
{
Expand All @@ -463,6 +465,7 @@ public static AutoResetUniTaskCompletionSource<T> Create()
{
result = new AutoResetUniTaskCompletionSource<T>();
}
result.version = result.core.Version;
TaskTracker.TrackActiveTask(result, 2);
return result;
}
Expand Down Expand Up @@ -506,19 +509,19 @@ public UniTask<T> Task
[DebuggerHidden]
public bool TrySetResult(T result)
{
return core.TrySetResult(result);
return version == core.Version && core.TrySetResult(result);
}

[DebuggerHidden]
public bool TrySetCanceled(CancellationToken cancellationToken = default)
{
return core.TrySetCanceled(cancellationToken);
return version == core.Version && core.TrySetCanceled(cancellationToken);
}

[DebuggerHidden]
public bool TrySetException(Exception exception)
{
return core.TrySetException(exception);
return version == core.Version && core.TrySetException(exception);
}

[DebuggerHidden]
Expand Down Expand Up @@ -937,5 +940,5 @@ bool TrySignalCompletion(UniTaskStatus status)
}
return false;
}
}
}
}
Loading