Path: redb-route/tests/redb.Route.Tests/Core/CollectibleContextCleanupTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests/Core/CollectibleContextCleanupTests.cs
----
// The test emits its module with PersistedAssemblyBuilder, which .NET 9 introduced.
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Loader;
using redb.Route.Core;
using redb.Route.Serialization;
namespace redb.Route.Tests.Core;
///
/// A type of a collectible load context that went through the JSON serializer of redb.Route no longer keeps the
/// context alive: on unload the hot reload caches of the .NET libraries (System.Text.Json's above all) are emptied.
/// Without that, System.Text.Json roots the type for good (https://github.com/dotnet/runtime/issues/65323).
///
[Collection(CollectibleContextCollection.Name)]
public sealed class CollectibleContextCleanupTests
{
// A context with no assembly: clearing "for" it leaves out no library.
private static readonly AssemblyLoadContext NoAssemblies = new("no-assemblies", isCollectible: true);
[Fact]
public async Task A_module_type_serialized_as_json_lets_its_context_unload()
{
var module = SerializeFromCollectibleModule(keepCleanup: true);
await Collect(module);
module.IsAlive.Should().BeFalse("the System.Text.Json caches are emptied when the context unloads");
}
[Fact]
public async Task Without_the_cleanup_the_json_caches_keep_the_context_alive()
{
var module = SerializeFromCollectibleModule(keepCleanup: false);
await Collect(module);
module.IsAlive.Should().BeTrue("the control: System.Text.Json roots a type it serialized");
// Let the context go for the tests that follow.
CollectibleContextCleanup.ClearMetadataUpdateCaches(NoAssemblies);
}
[Fact]
public void The_handlers_of_the_loaded_libraries_are_found()
{
CollectibleContextCleanup.ClearMetadataUpdateCaches(NoAssemblies)
.Should().BeGreaterThan(0, "System.Text.Json declares a hot reload handler");
}
///
/// Loads an emitted module with one POCO into a collectible context, serializes and deserializes an instance
/// through and unloads the context. Out of line so nothing of the caller roots it.
///
[MethodImpl(MethodImplOptions.NoInlining)]
private static WeakReference SerializeFromCollectibleModule(bool keepCleanup)
{
new RouteContext().Dispose(); // the cleanup starts with the first route context
var context = new AssemblyLoadContext($"module-{Guid.NewGuid():N}", isCollectible: true);
var type = context.LoadFromStream(new MemoryStream(EmitModuleWithPoco())).GetType("Module.Order")!;
CollectibleContextCleanup.IsWatched(context).Should().BeTrue("a collectible context is watched once its assembly loads");
var order = Activator.CreateInstance(type)!;
type.GetProperty("Name")!.SetValue(order, "first");
var serializer = new JsonMessageSerializer();
var json = serializer.Serialize