|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using Microsoft.Extensions.Logging.Abstractions; |
| 3 | + |
| 4 | +namespace AutoMapper.UnitTests.Licensing; |
| 5 | + |
| 6 | +public class LicenseValidationBackgroundTests |
| 7 | +{ |
| 8 | + // Regression test for #4640: license validation must not run on the construction |
| 9 | + // thread. Building MapperConfiguration under a lazily-built DI singleton holds the |
| 10 | + // container's build lock, and validating there could deadlock the whole app under a |
| 11 | + // cold-start thread-pool starvation. Validation is logging-only, so it is offloaded |
| 12 | + // to a dedicated background thread. Rather than clamp the global thread pool (flaky, |
| 13 | + // process-wide), we assert the property that makes the deadlock impossible: the |
| 14 | + // constructor returns without validating, and validation logs on a *different* thread. |
| 15 | + [Fact] |
| 16 | + public void License_validation_runs_off_the_construction_thread() |
| 17 | + { |
| 18 | + var provider = new ThreadCapturingLoggerProvider(); |
| 19 | + var factory = new LoggerFactory(); |
| 20 | + factory.AddProvider(provider); |
| 21 | + |
| 22 | + var constructingThreadId = Environment.CurrentManagedThreadId; |
| 23 | + |
| 24 | + // A non-empty (junk) key forces the validation path to run; its result is logged, |
| 25 | + // and that logging must happen on a background thread, never on this one. |
| 26 | + _ = new MapperConfiguration(cfg => cfg.LicenseKey = "not-a-real-license-key", factory); |
| 27 | + |
| 28 | + // The constructor returned without blocking on validation. The license log should |
| 29 | + // arrive shortly, on a thread other than the one that built the configuration. |
| 30 | + provider.LicenseLogged.Wait(TimeSpan.FromSeconds(5)).ShouldBeTrue(); |
| 31 | + provider.LoggingThreadId.ShouldNotBe(constructingThreadId); |
| 32 | + } |
| 33 | + |
| 34 | + private sealed class ThreadCapturingLoggerProvider : ILoggerProvider |
| 35 | + { |
| 36 | + public readonly ManualResetEventSlim LicenseLogged = new(false); |
| 37 | + public int LoggingThreadId; |
| 38 | + |
| 39 | + public ILogger CreateLogger(string categoryName) => |
| 40 | + categoryName == "LuckyPennySoftware.AutoMapper.License" |
| 41 | + ? new CapturingLogger(this) |
| 42 | + : NullLogger.Instance; |
| 43 | + |
| 44 | + public void Dispose() => LicenseLogged.Dispose(); |
| 45 | + |
| 46 | + private sealed class CapturingLogger(ThreadCapturingLoggerProvider owner) : ILogger |
| 47 | + { |
| 48 | + public IDisposable BeginScope<TState>(TState state) => null; |
| 49 | + |
| 50 | + public bool IsEnabled(LogLevel logLevel) => true; |
| 51 | + |
| 52 | + public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, |
| 53 | + Func<TState, Exception, string> formatter) |
| 54 | + { |
| 55 | + owner.LoggingThreadId = Environment.CurrentManagedThreadId; |
| 56 | + owner.LicenseLogged.Set(); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments