Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit edcc6ff

Browse files
committed
Add back compat test for BackgroundWorker finalization
1 parent 5579d0e commit edcc6ff

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/System.ComponentModel.EventBasedAsync/tests/BackgroundWorkerTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,39 @@ public void DisposeTwiceShouldNotThrow()
295295
bw.Dispose();
296296
}
297297

298+
[Fact]
299+
public void TestFinalization()
300+
{
301+
// BackgroundWorker has a finalizer that exists purely for backwards compatibility
302+
// with existing code that may override Dispose to clean up native resources.
303+
// https://github.com/dotnet/corefx/pull/752
304+
305+
ManualResetEventSlim mres = SetEventWhenFinalizedBackgroundWorker.CreateAndThrowAway();
306+
307+
GC.Collect();
308+
GC.WaitForPendingFinalizers();
309+
GC.Collect();
310+
311+
Assert.True(mres.Wait(10000));
312+
}
313+
314+
private sealed class SetEventWhenFinalizedBackgroundWorker : BackgroundWorker
315+
{
316+
private ManualResetEventSlim _setWhenFinalized;
317+
318+
internal static ManualResetEventSlim CreateAndThrowAway()
319+
{
320+
var mres = new ManualResetEventSlim();
321+
new SetEventWhenFinalizedBackgroundWorker() { _setWhenFinalized = mres };
322+
return mres;
323+
}
324+
325+
protected override void Dispose(bool disposing)
326+
{
327+
_setWhenFinalized.Set();
328+
}
329+
}
330+
298331
private static void Wait(int milliseconds)
299332
{
300333
Task.Delay(milliseconds).Wait();

0 commit comments

Comments
 (0)