Add Flypaper solution skeleton with HTTP tarpit, SQLite event log and tests
This commit is contained in:
1 parent
20314268c6
commit
f9161f48e7
55 files changed
+1331
No files matched your search
@@ -0,0 +1,88 @@
|
||||
using System.Net;
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Core.Strategies;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NSubstitute;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests;
|
||||
|
||||
public sealed class TrapRunnerTests
|
||||
{
|
||||
private readonly IConnectionGovernor _governor = Substitute.For<IConnectionGovernor>();
|
||||
private readonly ITrapEventSink _sink = Substitute.For<ITrapEventSink>();
|
||||
private readonly FakeTrapChannel _channel = new();
|
||||
|
||||
[Fact]
|
||||
public async Task Rejects_without_opening_channel_when_governor_is_full()
|
||||
{
|
||||
_governor.TryAcquire(Arg.Any<TrapSession>(), out Arg.Any<IDisposable?>()).Returns(false);
|
||||
var opened = false;
|
||||
|
||||
var outcome = await CreateRunner(TimeSpan.FromSeconds(1)).RunAsync(
|
||||
Context(),
|
||||
_ =>
|
||||
{
|
||||
opened = true;
|
||||
return ValueTask.FromResult<ITrapChannel>(_channel);
|
||||
},
|
||||
CancellationToken.None);
|
||||
|
||||
outcome.ShouldBe(TrapOutcome.Rejected);
|
||||
opened.ShouldBeFalse();
|
||||
_sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.Outcome == TrapOutcome.Rejected && e.BytesSent == 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Times_out_after_max_hold_duration_and_releases_lease()
|
||||
{
|
||||
var lease = AllowAcquire();
|
||||
|
||||
var outcome = await CreateRunner(TimeSpan.FromMilliseconds(200)).RunAsync(Context(), Open, CancellationToken.None);
|
||||
|
||||
outcome.ShouldBe(TrapOutcome.Timeout);
|
||||
lease.Received(1).Dispose();
|
||||
_sink.Received(1).Publish(Arg.Is<TrapEvent>(e =>
|
||||
e.Outcome == TrapOutcome.Timeout && e.BytesSent == _channel.Writes.Count && e.BytesSent > 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Reports_client_abort_when_caller_cancels()
|
||||
{
|
||||
AllowAcquire();
|
||||
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
|
||||
|
||||
var outcome = await CreateRunner(TimeSpan.FromMinutes(1)).RunAsync(Context(), Open, cts.Token);
|
||||
|
||||
outcome.ShouldBe(TrapOutcome.ClientAborted);
|
||||
_sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.Outcome == TrapOutcome.ClientAborted && e.Strategy == "drip"));
|
||||
}
|
||||
|
||||
private IDisposable AllowAcquire()
|
||||
{
|
||||
var lease = Substitute.For<IDisposable>();
|
||||
_governor.TryAcquire(Arg.Any<TrapSession>(), out Arg.Any<IDisposable?>())
|
||||
.Returns(call =>
|
||||
{
|
||||
call[1] = lease;
|
||||
return true;
|
||||
});
|
||||
return lease;
|
||||
}
|
||||
|
||||
private ValueTask<ITrapChannel> Open(CancellationToken ct) => ValueTask.FromResult<ITrapChannel>(_channel);
|
||||
|
||||
private TrapRunner CreateRunner(TimeSpan maxHoldDuration)
|
||||
{
|
||||
var drip = new DripStrategy(Options.Create(new DripOptions { Interval = TimeSpan.FromMilliseconds(20) }), TimeProvider.System);
|
||||
return new TrapRunner(
|
||||
_governor,
|
||||
[drip],
|
||||
_sink,
|
||||
Options.Create(new FlypaperOptions { MaxHoldDuration = maxHoldDuration }),
|
||||
TimeProvider.System);
|
||||
}
|
||||
|
||||
private static TrapContext Context() =>
|
||||
new(new TrapSession(Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse("203.0.113.9"), DateTimeOffset.UtcNow), "GET /.env");
|
||||
}
|
||||
Reference in new issue
Block a user