diff --git a/test/Microsoft.VisualStudio.Threading.Tests/AsyncLazyTests.cs b/test/Microsoft.VisualStudio.Threading.Tests/AsyncLazyTests.cs index 051c960d5..21254abed 100644 --- a/test/Microsoft.VisualStudio.Threading.Tests/AsyncLazyTests.cs +++ b/test/Microsoft.VisualStudio.Threading.Tests/AsyncLazyTests.cs @@ -149,11 +149,10 @@ async delegate } [Fact] - public async Task ValueFactoryReleasedAfterExecution() + public void ValueFactoryReleasedAfterExecution() { - WeakReference collectible = await this.ValueFactoryReleasedAfterExecution_Helper(); + (WeakReference collectible, AsyncLazy lazy) = this.ValueFactoryReleasedAfterExecution_Helper(); - await Task.Yield(); for (int i = 0; i < 3; i++) { GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true); @@ -161,6 +160,7 @@ public async Task ValueFactoryReleasedAfterExecution() } Assert.False(collectible.IsAlive); + GC.KeepAlive(lazy); } [Theory, CombinatorialData] @@ -1070,18 +1070,14 @@ private JoinableTaskContext InitializeJTCAndSC() } [MethodImpl(MethodImplOptions.NoInlining)] - private async Task ValueFactoryReleasedAfterExecution_Helper() + private (WeakReference Collectible, AsyncLazy Lazy) ValueFactoryReleasedAfterExecution_Helper() { - var closure = new { value = new object() }; - var collectible = new WeakReference(closure); - var lazy = new AsyncLazy(async delegate - { - await Task.Yield(); - return closure.value; - }); + var valueFactory = new ValueFactory(); + var collectible = new WeakReference(valueFactory); + var lazy = new AsyncLazy(valueFactory.CreateValueAsync); - await lazy.GetValueAsync(); - return collectible; + _ = lazy.GetValue(); + return (collectible, lazy); } [MethodImpl(MethodImplOptions.NoInlining)] @@ -1119,6 +1115,14 @@ private abstract class DisposableBase public bool IsDisposed => this.disposalEvent.IsSet; } + private sealed class ValueFactory + { + /// + /// Creates the value returned by the test factory. + /// + internal Task CreateValueAsync() => Task.FromResult(new object()); + } + private class Disposable : DisposableBase, IDisposableObservable { public void Dispose() => this.disposalEvent.Set();