Skip to content
Open
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
30 changes: 17 additions & 13 deletions test/Microsoft.VisualStudio.Threading.Tests/AsyncLazyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,18 @@ async delegate
}

[Fact]
public async Task ValueFactoryReleasedAfterExecution()
public void ValueFactoryReleasedAfterExecution()
{
WeakReference collectible = await this.ValueFactoryReleasedAfterExecution_Helper();
(WeakReference collectible, AsyncLazy<object> lazy) = this.ValueFactoryReleasedAfterExecution_Helper();

await Task.Yield();
for (int i = 0; i < 3; i++)
{
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true);
GC.WaitForPendingFinalizers();
}

Assert.False(collectible.IsAlive);
GC.KeepAlive(lazy);
}

[Theory, CombinatorialData]
Expand Down Expand Up @@ -1070,18 +1070,14 @@ private JoinableTaskContext InitializeJTCAndSC()
}

[MethodImpl(MethodImplOptions.NoInlining)]
private async Task<WeakReference> ValueFactoryReleasedAfterExecution_Helper()
private (WeakReference Collectible, AsyncLazy<object> Lazy) ValueFactoryReleasedAfterExecution_Helper()
{
var closure = new { value = new object() };
var collectible = new WeakReference(closure);
var lazy = new AsyncLazy<object>(async delegate
{
await Task.Yield();
return closure.value;
});
var valueFactory = new ValueFactory();
var collectible = new WeakReference(valueFactory);
var lazy = new AsyncLazy<object>(valueFactory.CreateValueAsync);

await lazy.GetValueAsync();
return collectible;
_ = lazy.GetValue();
return (collectible, lazy);
}

[MethodImpl(MethodImplOptions.NoInlining)]
Expand Down Expand Up @@ -1119,6 +1115,14 @@ private abstract class DisposableBase
public bool IsDisposed => this.disposalEvent.IsSet;
}

private sealed class ValueFactory
{
/// <summary>
/// Creates the value returned by the test factory.
/// </summary>
internal Task<object> CreateValueAsync() => Task.FromResult(new object());
}

private class Disposable : DisposableBase, IDisposableObservable
{
public void Dispose() => this.disposalEvent.Set();
Expand Down
Loading