From 4079d4e8cc477947d457a2627059de819be13759 Mon Sep 17 00:00:00 2001 From: ihourglass Date: Sun, 1 Dec 2024 21:28:42 +0800 Subject: [PATCH] Add lifetime test case. --- test/Rougamo.Fody.Tests/BasicTest.cs | 63 +++++ .../Attributes/TryLifetimeAttribute.cs | 44 +++ .../BasicUsage/LifetimeUseCase.cs | 254 ++++++++++++++++++ 3 files changed, 361 insertions(+) create mode 100644 test/TestAssemblies/BasicUsage/Attributes/TryLifetimeAttribute.cs create mode 100644 test/TestAssemblies/BasicUsage/LifetimeUseCase.cs diff --git a/test/Rougamo.Fody.Tests/BasicTest.cs b/test/Rougamo.Fody.Tests/BasicTest.cs index 82aff4d..5a5f9f6 100644 --- a/test/Rougamo.Fody.Tests/BasicTest.cs +++ b/test/Rougamo.Fody.Tests/BasicTest.cs @@ -600,6 +600,7 @@ public async Task LifetimeTest() var list = new List(); + #region Singleton list.Clear(); instance.SingletonNested(list); Assert.Equal(2, list.Count); @@ -616,6 +617,24 @@ public async Task LifetimeTest() Assert.Equal(3, list.Count); Assert.Same(list[0], list[2]); + list.Clear(); + ((IEnumerable)instance.SingletonNestedIterator(list)).ToArray(); + Assert.Equal(2, list.Count); + Assert.Same(list[0], list[1]); + ((IEnumerable)instance.SingletonIterator(list)).ToArray(); + Assert.Equal(3, list.Count); + Assert.Same(list[0], list[2]); + + list.Clear(); + await ((IAsyncEnumerable)instance.SingletonNestedAiterator(list)).ToArrayAsync(); + Assert.Equal(2, list.Count); + Assert.Same(list[0], list[1]); + await ((IAsyncEnumerable)instance.SingletonAiterator(list)).ToArrayAsync(); + Assert.Equal(3, list.Count); + Assert.Same(list[0], list[2]); + #endregion Singleton + + #region Pooled list.Clear(); instance.PooledNested(list); Assert.Equal(2, list.Count); @@ -632,6 +651,24 @@ public async Task LifetimeTest() Assert.Equal(3, list.Count); Assert.True(list[0] == list[2] || list[1] == list[2]); + list.Clear(); + ((IEnumerable)instance.PooledNestedIterator(list)).ToArray(); + Assert.Equal(2, list.Count); + Assert.NotSame(list[0], list[1]); + ((IEnumerable)instance.PooledIterator(list)).ToArray(); + Assert.Equal(3, list.Count); + Assert.True(list[0] == list[2] || list[1] == list[2]); + + list.Clear(); + await ((IAsyncEnumerable)instance.PooledNestedAiterator(list)).ToArrayAsync(); + Assert.Equal(2, list.Count); + Assert.NotSame(list[0], list[1]); + await ((IAsyncEnumerable)instance.PooledAiterator(list)).ToArrayAsync(); + Assert.Equal(3, list.Count); + Assert.True(list[0] == list[2] || list[1] == list[2]); + #endregion Pooled + + #region Transient list.Clear(); instance.TransientNested(list); Assert.Equal(2, list.Count); @@ -649,6 +686,32 @@ public async Task LifetimeTest() Assert.Equal(3, list.Count); Assert.NotSame(list[0], list[2]); Assert.NotSame(list[1], list[2]); + + list.Clear(); + ((IEnumerable)instance.TransientNestedIterator(list)).ToArray(); + Assert.Equal(2, list.Count); + Assert.NotSame(list[0], list[1]); + ((IEnumerable)instance.TransientIterator(list)).ToArray(); + Assert.Equal(3, list.Count); + Assert.NotSame(list[0], list[2]); + Assert.NotSame(list[1], list[2]); + + list.Clear(); + await ((IAsyncEnumerable)instance.TransientNestedAiterator(list)).ToArrayAsync(); + Assert.Equal(2, list.Count); + Assert.NotSame(list[0], list[1]); + await ((IAsyncEnumerable)instance.TransientAiterator(list)).ToArrayAsync(); + Assert.Equal(3, list.Count); + Assert.NotSame(list[0], list[2]); + Assert.NotSame(list[1], list[2]); + #endregion Transient + + list.Clear(); + instance.GenericPooled(list); + Assert.Single(list); + await (Task)instance.GenericPooledAsync(list); + Assert.Equal(2, list.Count); + Assert.Same(list[0], list[1]); } } } diff --git a/test/TestAssemblies/BasicUsage/Attributes/TryLifetimeAttribute.cs b/test/TestAssemblies/BasicUsage/Attributes/TryLifetimeAttribute.cs new file mode 100644 index 0000000..1c59f4c --- /dev/null +++ b/test/TestAssemblies/BasicUsage/Attributes/TryLifetimeAttribute.cs @@ -0,0 +1,44 @@ +using Rougamo; +using Rougamo.Context; +using Rougamo.Metadatas; +using System.Collections.Generic; + +namespace BasicUsage.Attributes +{ + [Advice(Feature.OnEntry)] + public abstract class TryLifetimeAttribute : MoAttribute + { + public override void OnEntry(MethodContext context) + { + var list = (List)context.Arguments[0]; + list.Add(this); + } + } + + [Lifetime(Lifetime.Singleton)] + public class SingletonAttribute : TryLifetimeAttribute + { + } + + [Lifetime(Lifetime.Pooled)] + public class PooledAttribute : TryLifetimeAttribute + { + } + + [Lifetime(Lifetime.Pooled)] + public class PooledAttribute : TryLifetimeAttribute, IResettable + { + public T Value { get; set; } + + public bool TryReset() + { + Value = default; + return true; + } + } + + [Lifetime(Lifetime.Transient)] + public class TransientAttribute : TryLifetimeAttribute + { + } +} diff --git a/test/TestAssemblies/BasicUsage/LifetimeUseCase.cs b/test/TestAssemblies/BasicUsage/LifetimeUseCase.cs new file mode 100644 index 0000000..d32d7c8 --- /dev/null +++ b/test/TestAssemblies/BasicUsage/LifetimeUseCase.cs @@ -0,0 +1,254 @@ +using BasicUsage.Attributes; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace BasicUsage +{ + public class LifetimeUseCase + { + [Singleton] + public void Singleton(List list) + { + } + + [Singleton] + public void SingletonNested(List list) + { + SingletonNestedCore(list); + } + + [Singleton] + private void SingletonNestedCore(List list) + { + } + + [Singleton] + public async Task SingletonAsync(List list) + { + await Task.CompletedTask; + } + + [Singleton] + public async Task SingletonNestedAsync(List list) + { + await SingletonNestedCoreAsync(list); + } + + [Singleton] + private async Task SingletonNestedCoreAsync(List list) + { + await Task.CompletedTask; + } + + [Singleton] + public IEnumerable SingletonIterator(List list) + { + yield return 1; + } + + [Singleton] + public IEnumerable SingletonNestedIterator(List list) + { + foreach (var item in SingletonNestedIteratorCore(list)) + { + yield return item; + } + } + + [Singleton] + private IEnumerable SingletonNestedIteratorCore(List list) + { + yield return 1; + } + + [Singleton] + public async IAsyncEnumerable SingletonAiterator(List list) + { + await Task.Yield(); + yield return 1; + } + + [Singleton] + public async IAsyncEnumerable SingletonNestedAiterator(List list) + { + await foreach (var item in SingletonNestedAiteratorCore(list)) + { + yield return item; + } + } + + [Singleton] + private async IAsyncEnumerable SingletonNestedAiteratorCore(List list) + { + await Task.Yield(); + yield return 1; + } + + [Pooled] + public void Pooled(List list) + { + } + + [Pooled] + public void PooledNested(List list) + { + PooledNestedCore(list); + } + + [Pooled] + private void PooledNestedCore(List list) + { + } + + [Pooled] + public async Task PooledAsync(List list) + { + await Task.CompletedTask; + } + + [Pooled] + public async Task PooledNestedAsync(List list) + { + await PooledNestedCoreAsync(list); + } + + [Pooled] + private async Task PooledNestedCoreAsync(List list) + { + await Task.CompletedTask; + } + + [Pooled] + public IEnumerable PooledIterator(List list) + { + yield return 1; + } + + [Pooled] + public IEnumerable PooledNestedIterator(List list) + { + foreach (var item in PooledNestedIteratorCore(list)) + { + yield return item; + } + } + + [Pooled] + private IEnumerable PooledNestedIteratorCore(List list) + { + yield return 1; + } + + [Pooled] + public async IAsyncEnumerable PooledAiterator(List list) + { + await Task.Yield(); + yield return 1; + } + + [Pooled] + public async IAsyncEnumerable PooledNestedAiterator(List list) + { + await foreach (var item in PooledNestedAiteratorCore(list)) + { + yield return item; + } + } + + [Pooled] + private async IAsyncEnumerable PooledNestedAiteratorCore(List list) + { + await Task.Yield(); + yield return 1; + } + + [Pooled(Value = 1)] + public void GenericPooled(List list) + { + } + + [Pooled(Value = 2)] + public async Task GenericPooledAsync(List list) + { + await Task.Yield(); + } + + [Transient] + public void Transient(List list) + { + } + + [Transient] + public void TransientNested(List list) + { + TransientNestedCore(list); + } + + [Transient] + private void TransientNestedCore(List list) + { + } + + [Transient] + public async Task TransientAsync(List list) + { + await Task.CompletedTask; + } + + [Transient] + public async Task TransientNestedAsync(List list) + { + await TransientNestedCoreAsync(list); + } + + [Transient] + private async Task TransientNestedCoreAsync(List list) + { + await Task.CompletedTask; + } + + [Transient] + public IEnumerable TransientIterator(List list) + { + yield return 1; + } + + [Transient] + public IEnumerable TransientNestedIterator(List list) + { + foreach (var item in TransientNestedIteratorCore(list)) + { + yield return item; + } + } + + [Transient] + private IEnumerable TransientNestedIteratorCore(List list) + { + yield return 1; + } + + [Transient] + public async IAsyncEnumerable TransientAiterator(List list) + { + await Task.Yield(); + yield return 1; + } + + [Transient] + public async IAsyncEnumerable TransientNestedAiterator(List list) + { + await foreach (var item in TransientNestedAiteratorCore(list)) + { + yield return item; + } + } + + [Transient] + private async IAsyncEnumerable TransientNestedAiteratorCore(List list) + { + await Task.Yield(); + yield return 1; + } + } +}