Restructure solution into folders and fix persistence issues
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
37987a394a
commit
7c39cb5562
62 files changed
+219
-151
No files matched your search
@@ -0,0 +1,78 @@
|
||||
using System.Net;
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.Core.Runtime;
|
||||
using Flypaper.Tests.Fakes;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Core;
|
||||
|
||||
public sealed class ConnectionGovernorTests
|
||||
{
|
||||
private static readonly IPAddress IpA = IPAddress.Parse("203.0.113.1");
|
||||
private static readonly IPAddress IpB = IPAddress.Parse("203.0.113.2");
|
||||
|
||||
[Fact]
|
||||
public void Rejects_when_per_ip_limit_reached()
|
||||
{
|
||||
var governor = CreateGovernor(maxConnections: 10, maxPerIp: 2);
|
||||
|
||||
governor.TryAcquire(Session(IpA), out _).ShouldBeTrue();
|
||||
governor.TryAcquire(Session(IpA), out _).ShouldBeTrue();
|
||||
governor.TryAcquire(Session(IpA), out var lease).ShouldBeFalse();
|
||||
lease.ShouldBeNull();
|
||||
|
||||
governor.TryAcquire(Session(IpB), out _).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rejects_when_total_limit_reached()
|
||||
{
|
||||
var governor = CreateGovernor(maxConnections: 2, maxPerIp: 10);
|
||||
|
||||
governor.TryAcquire(Session(IpA), out _).ShouldBeTrue();
|
||||
governor.TryAcquire(Session(IpB), out _).ShouldBeTrue();
|
||||
governor.TryAcquire(Session(IPAddress.Parse("203.0.113.3")), out _).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Disposing_lease_frees_slot()
|
||||
{
|
||||
var governor = CreateGovernor(maxConnections: 1, maxPerIp: 1);
|
||||
|
||||
governor.TryAcquire(Session(IpA), out var lease).ShouldBeTrue();
|
||||
governor.TryAcquire(Session(IpA), out _).ShouldBeFalse();
|
||||
|
||||
lease.Dispose();
|
||||
|
||||
governor.ActiveConnections.ShouldBe(0);
|
||||
governor.TryAcquire(Session(IpA), out _).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Disposing_lease_twice_releases_once()
|
||||
{
|
||||
var governor = CreateGovernor(maxConnections: 10, maxPerIp: 10);
|
||||
governor.TryAcquire(Session(IpA), out var first).ShouldBeTrue();
|
||||
governor.TryAcquire(Session(IpA), out _).ShouldBeTrue();
|
||||
|
||||
first.Dispose();
|
||||
first.Dispose();
|
||||
|
||||
governor.ActiveConnections.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Treats_ipv4_mapped_ipv6_as_same_ip()
|
||||
{
|
||||
var governor = CreateGovernor(maxConnections: 10, maxPerIp: 1);
|
||||
|
||||
governor.TryAcquire(Session(IpA), out _).ShouldBeTrue();
|
||||
governor.TryAcquire(Session(IpA.MapToIPv6()), out _).ShouldBeFalse();
|
||||
}
|
||||
|
||||
private static ConnectionGovernor CreateGovernor(int maxConnections, int maxPerIp) =>
|
||||
new(TestOptions.Monitor(new FlypaperOptions { MaxConnections = maxConnections, MaxConnectionsPerIp = maxPerIp }));
|
||||
|
||||
private static TrapSession Session(IPAddress ip) => new(Guid.NewGuid(), TrapProtocol.Http, ip, DateTimeOffset.UtcNow);
|
||||
}
|
||||
Reference in new issue
Block a user