64 lines
4.7 KiB
C#
64 lines
4.7 KiB
C#
using System.Data;
|
|
using System.Diagnostics;
|
|
using Microsoft.Data.SqlClient;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.FileProviders;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using PlotLine.Data;
|
|
using PlotLine.Services;
|
|
|
|
internal static class SqlLaunchDiagnosticsTests
|
|
{
|
|
sealed class EnvironmentStub(string name) : IHostEnvironment
|
|
{
|
|
public string EnvironmentName { get; set; } = name;
|
|
public string ApplicationName { get; set; } = "Tests";
|
|
public string ContentRootPath { get; set; } = ".";
|
|
public IFileProvider ContentRootFileProvider { get; set; } = new NullFileProvider();
|
|
}
|
|
sealed class CaptureLog : ILogger<DevelopmentSqlDiagnostics>
|
|
{
|
|
public readonly List<string> Lines=[];
|
|
public IDisposable? BeginScope<TState>(TState state) where TState:notnull => null;
|
|
public bool IsEnabled(LogLevel level)=>true;
|
|
public void Log<TState>(LogLevel level,EventId id,TState state,Exception? error,Func<TState,Exception?,string> format) { lock(Lines)Lines.Add(format(state,error)); }
|
|
}
|
|
public static void Run()
|
|
{
|
|
static void Check(bool ok,string name){if(!ok)throw new Exception(name);}
|
|
var configuration=new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string,string?>{{"SqlDiagnostics:Enabled","true"}}).Build();
|
|
var log=new CaptureLog();var diagnostics=new DevelopmentSqlDiagnostics(new EnvironmentStub("Development"),configuration,log);
|
|
diagnostics.StartAsync(default).GetAwaiter().GetResult();using var listener=new DiagnosticListener("SqlClientDiagnosticListener");
|
|
var frame=diagnostics.BeginRequest("request-test");
|
|
using var procedure=new SqlCommand("dbo.UserOnboarding_Get"){CommandType=CommandType.StoredProcedure};procedure.Parameters.AddWithValue("@Secret","never-record-this-value");
|
|
using var text=new SqlCommand("SELECT 'never-record-this-prose'");
|
|
void Command(SqlCommand command,bool cancelled=false)
|
|
{
|
|
var id=Guid.NewGuid();listener.Write("Microsoft.Data.SqlClient.WriteCommandBefore",new{OperationId=id,Command=command});
|
|
if(cancelled)listener.Write("Microsoft.Data.SqlClient.WriteCommandError",new{OperationId=id,Command=command,Exception=new OperationCanceledException("never-record-error-message")});
|
|
else listener.Write("Microsoft.Data.SqlClient.WriteCommandAfter",new{OperationId=id,Command=command});
|
|
}
|
|
Command(procedure);Command(procedure);Command(text,true);
|
|
diagnostics.EndRequest(frame,"Writer/Review",100,200);
|
|
using(var operation=new Activity("StoryIntelligence.Poll").Start())Command(procedure);
|
|
var joined=string.Join('\n',log.Lines);
|
|
Check(frame.Count==3&&frame.Errors==1&&frame.Procedures["dbo.UserOnboarding_Get"]==2,"Correlated counts/repeated procedures/cancellation");
|
|
Check(!joined.Contains("never-record"),"SQL values, text, parameters and exception messages omitted");
|
|
Check(joined.Contains("\"Background\":true")&&joined.Contains("StoryIntelligence.Poll"),"Background activity attribution");
|
|
var captured=log.Lines.Count;configuration["SqlDiagnostics:Enabled"]="false";Command(procedure);
|
|
Check(log.Lines.Count==captured,"Disabling the capture setting stops recording without a service restart");
|
|
diagnostics.StopAsync(default).GetAwaiter().GetResult();
|
|
configuration["SqlDiagnostics:Enabled"]="true";
|
|
var productionLog=new CaptureLog();var production=new DevelopmentSqlDiagnostics(new EnvironmentStub("Production"),configuration,productionLog);
|
|
production.StartAsync(default).GetAwaiter().GetResult();Command(procedure);Check(!production.Enabled&&productionLog.Lines.Count==0,"Production cannot enable SQL trace");production.StopAsync(default).GetAwaiter().GetResult();
|
|
var disabled=new DevelopmentSqlDiagnostics(new EnvironmentStub("Development"),new ConfigurationBuilder().Build(),new CaptureLog());Check(!disabled.Enabled,"Trace defaults off");
|
|
var idle=new QueueIdleBackoff(TimeSpan.FromSeconds(3),TimeSpan.FromSeconds(6));
|
|
for(int i=0;i<8;i++)Check(idle.Next(false)==TimeSpan.FromSeconds(3),"Normal initial queue cadence");
|
|
for(int i=0;i<100;i++)Check(idle.Next(false)==TimeSpan.FromSeconds(6),"Bounded idle queue cadence");
|
|
Check(idle.Next(true)==TimeSpan.Zero&&idle.Next(false)==TimeSpan.FromSeconds(3),"Successful work immediately resets idle backoff");
|
|
Check(idle.Next(false,true)==TimeSpan.FromSeconds(3),"Failures retain retry cadence");
|
|
Check(new QueueIdleBackoff(TimeSpan.FromSeconds(10),TimeSpan.FromSeconds(6)).Next(false)==TimeSpan.FromSeconds(10),"Never shorten configured poll delay");
|
|
}
|
|
}
|