42 lines
3.2 KiB
C#
42 lines
3.2 KiB
C#
using System.Diagnostics;
|
|
using System.Data;
|
|
using System.Security.Cryptography;
|
|
using Dapper;
|
|
using Microsoft.Data.SqlClient;
|
|
using Microsoft.Extensions.Configuration;
|
|
using PlotLine.Data;
|
|
internal static class GeographyDatabaseTests
|
|
{
|
|
public static async Task Run()
|
|
{
|
|
var start=new ProcessStartInfo("sudo"){RedirectStandardOutput=true,RedirectStandardError=true};
|
|
foreach(var arg in new[]{"-n","cat","/etc/plotdirector/plotdirector.env"})start.ArgumentList.Add(arg);
|
|
using var process=Process.Start(start)!;var env=await process.StandardOutput.ReadToEndAsync();await process.WaitForExitAsync();
|
|
if(process.ExitCode!=0)throw new InvalidOperationException("Development configuration unavailable.");
|
|
var connectionString=env.Split('\n').Single(s=>s.StartsWith("ConnectionStrings__DefaultConnection=")).Split('=',2)[1].Trim().Trim('"','\'');
|
|
if(new SqlConnectionStringBuilder(connectionString).InitialCatalog!="PlotDirector_Development")throw new InvalidOperationException("Development only.");
|
|
using var db=new SqlConnection(connectionString);await db.OpenAsync();
|
|
if(await db.ExecuteScalarAsync<string>("SELECT DB_NAME()")!="PlotDirector_Development")throw new InvalidOperationException("Wrong effective database.");
|
|
var config=new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string,string?>{{"ConnectionStrings:DefaultConnection",connectionString}}).Build();
|
|
var factory=new SqlConnectionFactory(config);var provider="FakeConcurrency-"+Guid.NewGuid().ToString("N");
|
|
var keys=Enumerable.Range(0,16).Select(_=>Convert.ToHexString(RandomNumberGenerator.GetBytes(32))).ToArray();
|
|
var leases=Enumerable.Range(0,16).Select(_=>Guid.NewGuid()).ToArray();
|
|
try
|
|
{
|
|
var results=await Task.WhenAll(Enumerable.Range(0,16).Select(i=>new GeographyRepository(factory).ReserveAsync(provider,TimeSpan.FromSeconds(15),keys[i],leases[i])));
|
|
if(results.Count(x=>x)!=1)throw new InvalidOperationException("Concurrent workers acquired more than one provider slot.");
|
|
var winner=Array.IndexOf(results,true);
|
|
// A fresh repository (process-equivalent connection) must still see the persisted occupied slot.
|
|
if(await new GeographyRepository(factory).ReserveAsync(provider,TimeSpan.FromSeconds(15),keys[0],Guid.NewGuid()))throw new InvalidOperationException("Restart bypassed gate.");
|
|
await new GeographyRepository(factory).ReleaseAsync(leases[winner],3600);
|
|
if(await new GeographyRepository(factory).ReserveAsync(provider,TimeSpan.FromSeconds(15),keys[0],Guid.NewGuid()))throw new InvalidOperationException("Provider cooldown bypassed.");
|
|
Console.WriteLine("PASS 16 concurrent SQL provider reservations: exactly one winner, fresh-connection recovery and persistent provider cooldown; zero external calls.");
|
|
}
|
|
finally
|
|
{
|
|
// Only this test's random synthetic provider and exact random request keys are removed.
|
|
await db.ExecuteAsync("DELETE dbo.GeographicLookupLease WHERE RequestKey IN @keys; DELETE dbo.GeographicProviderGate WHERE Provider=@provider",new{keys,provider});
|
|
}
|
|
}
|
|
}
|