/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Servers/Kestrel/shared/test/TestServiceContext.cs
92 строки
3 KB
James Newton-King
Server memory pool metrics updates (#63032)
09 авг 2025, 04:00
Не верифицирован
09 авг 2025, 04:00
ef2f62f
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Buffers; using System.IO.Pipelines; using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Time.Testing; namespace Microsoft.AspNetCore.InternalTesting; internal class TestServiceContext : ServiceContext { public TestServiceContext() : this(disableHttp1LineFeedTerminators: true) { } public TestServiceContext(ILoggerFactory loggerFactory = null, KestrelTrace kestrelTrace = null, bool disableHttp1LineFeedTerminators = true, KestrelMetrics metrics = null) { loggerFactory ??= NullLoggerFactory.Instance; kestrelTrace ??= CreateLoggingTrace(loggerFactory); metrics ??= new KestrelMetrics(new TestMeterFactory()); Initialize(loggerFactory, kestrelTrace, disableHttp1LineFeedTerminators, metrics); } private static KestrelTrace CreateLoggingTrace(ILoggerFactory loggerFactory) { return new KestrelTrace(loggerFactory); } public void InitializeHeartbeat() { DateHeaderValueManager = new DateHeaderValueManager(TimeProvider.System); Heartbeat = new Heartbeat( new IHeartbeatHandler[] { DateHeaderValueManager, ConnectionManager }, TimeProvider.System, DebuggerWrapper.Singleton, Log, Heartbeat.Interval); FakeTimeProvider = null; TimeProvider = TimeProvider.System; } private void Initialize(ILoggerFactory loggerFactory, KestrelTrace kestrelTrace, bool disableHttp1LineFeedTerminators, KestrelMetrics metrics) { LoggerFactory = loggerFactory; Log = kestrelTrace; Scheduler = PipeScheduler.ThreadPool; FakeTimeProvider = new FakeTimeProvider(); TimeProvider = FakeTimeProvider; DateHeaderValueManager = new DateHeaderValueManager(FakeTimeProvider); ConnectionManager = new ConnectionManager(Log, ResourceCounter.Unlimited); HttpParser = new HttpParser<Http1ParsingHandler>(Log.IsEnabled(LogLevel.Information), disableHttp1LineFeedTerminators); ServerOptions = new KestrelServerOptions { AddServerHeader = false }; DateHeaderValueManager.OnHeartbeat(); Metrics = metrics; ShutdownTimeout = TestConstants.DefaultTimeout; } public TimeSpan ShutdownTimeout { get; set; } public ILoggerFactory LoggerFactory { get; set; } public FakeTimeProvider FakeTimeProvider { get; set; } public IMemoryPoolFactory<byte> MemoryPoolFactory { get; set; } = new WrappingMemoryPoolFactory(() => TestMemoryPoolFactory.Create()); public string DateHeaderValue => DateHeaderValueManager.GetDateHeaderValues().String; internal sealed class WrappingMemoryPoolFactory : IMemoryPoolFactory<byte> { private readonly Func<MemoryPool<byte>> _memoryPoolFactory; public WrappingMemoryPoolFactory(Func<MemoryPool<byte>> memoryPoolFactory) { _memoryPoolFactory = memoryPoolFactory; } public MemoryPool<byte> Create(MemoryPoolOptions options = null) => _memoryPoolFactory(); } }