From 663fa737f3dbddaf31cf841a851f757265720668 Mon Sep 17 00:00:00 2001 From: Artem Kolesnykov Date: Sun, 12 Feb 2023 17:02:33 +0200 Subject: [PATCH] Added a test case for https://github.com/Cysharp/UniTask/issues/444 --- .../Assets/Tests/AsyncReactivePropertyTest.cs | 63 +++++++++++++++++++ .../Tests/AsyncReactivePropertyTest.cs.meta | 3 + 2 files changed, 66 insertions(+) create mode 100644 src/UniTask/Assets/Tests/AsyncReactivePropertyTest.cs create mode 100644 src/UniTask/Assets/Tests/AsyncReactivePropertyTest.cs.meta diff --git a/src/UniTask/Assets/Tests/AsyncReactivePropertyTest.cs b/src/UniTask/Assets/Tests/AsyncReactivePropertyTest.cs new file mode 100644 index 0000000..f582443 --- /dev/null +++ b/src/UniTask/Assets/Tests/AsyncReactivePropertyTest.cs @@ -0,0 +1,63 @@ +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(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 property, CancellationToken token) + { + while (true) + { + try + { + await property.WaitAsync(token); + _callCounter++; + } + catch (OperationCanceledException) + { + break; + } + } + } + } +} diff --git a/src/UniTask/Assets/Tests/AsyncReactivePropertyTest.cs.meta b/src/UniTask/Assets/Tests/AsyncReactivePropertyTest.cs.meta new file mode 100644 index 0000000..0c29ebf --- /dev/null +++ b/src/UniTask/Assets/Tests/AsyncReactivePropertyTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 27665955eefb4448969b8cc4dd204600 +timeCreated: 1676129650 \ No newline at end of file