Skip to content

Commit

Permalink
sample: add DisposeTestSample
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Dec 7, 2023
1 parent 1393ff6 commit d6b9b73
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 3 deletions.
104 changes: 104 additions & 0 deletions samples/DotNetCoreSample/DisposeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) Weihan Li. All rights reserved.
// Licensed under the Apache license.

using WeihanLi.Common.Helpers;

namespace DotNetCoreSample;

public class DisposeTest
{
public static void MainTest()
{
Console.WriteLine(@$"---- {nameof(MainTest)} start");
{
using var service = new TestService()
{
Name = "MainTest"
};
}
{
var service = new TestService()
{
Name = "MainTest1"
};
service.Dispose();
}
{
// forget to dispose
var service = new TestService()
{
Name = "MainTest2"
};
Console.WriteLine(service.GetType());

service = null;
Console.WriteLine(service is null);
}
GC.Collect();
GC.WaitForPendingFinalizers();

Console.WriteLine(@$"---- {nameof(MainTest)} end");
}

public static async ValueTask MainTestAsync()
{
Console.WriteLine(@$"---- {nameof(MainTestAsync)} start");
{
await using var service = new TestService()
{
Name = "MainTestAsync"
};
}
{
var service = new TestService()
{
Name = "MainTestAsync1"
};
await service.DisposeAsync();
}
{
// forget to dispose
var service = new TestService()
{
Name = "MainTestAsync2"
};
Console.WriteLine(service.GetType());

service = null;
Console.WriteLine(service is null);
}
GC.Collect();
GC.WaitForPendingFinalizers();

Console.WriteLine(@$"---- {nameof(MainTestAsync)} end");
}
}

file sealed class TestService : DisposableBase
{
public required string Name { get; init; }

protected override void Dispose(bool disposing)
{
if (disposing)
{
Console.WriteLine($@"<<{Name}>> disposes managed resources");
}
Console.WriteLine($@"<<{Name}>> disposes unmanaged resources");
base.Dispose(disposing);
}

protected override async ValueTask DisposeAsyncCore()
{
{
Console.WriteLine($@"<<{Name}>> disposes managed resources async");
await Task.Yield();
}
await base.DisposeAsyncCore();
}

~TestService()
{
Console.WriteLine($@"<<{Name}>> Finalizer executing");
}
}
19 changes: 16 additions & 3 deletions samples/DotNetCoreSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,24 @@
// Console.WriteLine(@"Exited");
// });
// ApplicationHelper.RuntimeInfo.Dump();
Console.WriteLine(ApplicationHelper.ResolvePath("yarn.cmd"));
await AppHostTest.MainTest();
// Console.WriteLine(ApplicationHelper.ResolvePath("yarn.cmd"));
// await AppHostTest.MainTest();
// NewtonJsonFormatterTest.MainTest();

// ConsoleHelper.ReadKeyWithPrompt("Press any key to exit");
DisposeTest.MainTest();
Console.WriteLine();
await DisposeTest.MainTestAsync();
Console.WriteLine();

ConsoleHelper.ReadKeyWithPrompt("Press any key to continue");

await DisposeTest.MainTestAsync();
Console.WriteLine();

GC.Collect();
GC.WaitForPendingFinalizers();

ConsoleHelper.ReadKeyWithPrompt("Press any key to exit");

internal struct TestStruct
{
Expand Down

0 comments on commit d6b9b73

Please sign in to comment.