Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugging for in-memory test failures #30460

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/EFCore/Infrastructure/ModelCacheKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ public class ModelCacheKey
private readonly Type _dbContextType;
private readonly bool _designTime;

/// <summary>
/// X
/// </summary>
public virtual Type ContextType
=> _dbContextType;

/// <summary>
/// X
/// </summary>
public virtual bool DesignTime
=> _designTime;

/// <summary>
/// Initializes a new instance of the <see cref="ModelCacheKey" /> class.
/// </summary>
Expand Down
54 changes: 54 additions & 0 deletions src/EFCore/Infrastructure/ModelSource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Text;
using Microsoft.Extensions.Caching.Memory;

namespace Microsoft.EntityFrameworkCore.Infrastructure;
Expand Down Expand Up @@ -30,6 +32,7 @@ namespace Microsoft.EntityFrameworkCore.Infrastructure;
public class ModelSource : IModelSource
{
private readonly object _syncObject = new();
private Dictionary<object, IModel?> _dictionary = new();

/// <summary>
/// Creates a new <see cref="ModelSource" /> instance.
Expand Down Expand Up @@ -66,13 +69,64 @@ public ModelSource(ModelSourceDependencies dependencies)
{
if (!cache.TryGetValue(cacheKey, out model))
{
if (_dictionary.TryGetValue(cacheKey, out var existingModel))
{
var builder = new StringBuilder();
builder.AppendLine(
cacheKey is ModelCacheKey modelCacheKey
? $"Dictionary contains key not in cache: {modelCacheKey.ContextType.Name} (Design time: {modelCacheKey.DesignTime})"
: $"Dictionary contains key not in cache: {cacheKey.GetType()}: {cacheKey}");

var memoryCache = (MemoryCache)cache;
builder.AppendLine($"Count: {memoryCache.Count}");

var size = typeof(MemoryCache).GetRuntimeProperties().Single(e => e.Name == "Size")
.GetValue(memoryCache)!;
builder.AppendLine($"Size: {size}");

var options = (MemoryCacheOptions)typeof(MemoryCache).GetRuntimeFields().Single(e => e.Name == "_options")
.GetValue(memoryCache)!;
builder.AppendLine($"SizeLimit: {options.SizeLimit}");

var coherentState = typeof(MemoryCache).GetRuntimeFields().Single(e => e.Name == "_coherentState")
.GetValue(memoryCache)!;
var entries = (IEnumerable)coherentState.GetType().GetRuntimeFields().Single(e => e.Name == "_entries")
.GetValue(coherentState)!;

foreach (var entry in entries)
{
var key = entry.GetType().GetProperty("Key")!.GetValue(entry);
var cacheEntry = entry.GetType().GetProperty("Value")!.GetValue(entry)!;
var value = cacheEntry.GetType().GetRuntimeFields().Single(e => e.Name == "_value").GetValue(cacheEntry);

if (key is ModelCacheKey modelCacheKeyFromCache)
{
builder.AppendLine(
$"{modelCacheKeyFromCache.ContextType.Name} (Design time: {modelCacheKeyFromCache.DesignTime}: {value}");

if (key.Equals(cacheKey))
{
builder.AppendLine("Match!");
}
}
else
{
builder.AppendLine($"{key}: {value}");
}
}

throw new Exception(builder.ToString());
}

model = CreateModel(
context, modelCreationDependencies.ConventionSetBuilder, modelCreationDependencies.ModelDependencies);

model = modelCreationDependencies.ModelRuntimeInitializer.Initialize(
model, designTime, modelCreationDependencies.ValidationLogger);

model = cache.Set(cacheKey, model, new MemoryCacheEntryOptions { Size = 100, Priority = CacheItemPriority.High });

_dictionary[cacheKey] = model;
}
}
}
Expand Down