Send a head start of the response immediately before dripping

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
TomandClaude Sonnet 5.5 committed 2026-11-16 22:54:03 +01:00
1 parent 13a78f4268
commit e48b6bb832
17 files changed
+120 -8

No files matched your search

@@ -6,7 +6,7 @@ namespace Flypaper.Tests.Core;
public sealed class HoldMethodParserTests
{
private static readonly HoldMethodInput Valid = new("Alternating", "50", "1000", "1", "64", "01:00:00");
private static readonly HoldMethodInput Valid = new("Alternating", "50", "1000", "1", "64", "2048", "01:00:00");
[Fact]
public void Parses_valid_input()
@@ -18,6 +18,7 @@ public sealed class HoldMethodParserTests
settings.MaxDelay.ShouldBe(TimeSpan.FromSeconds(1));
settings.MinChunkSize.ShouldBe(1);
settings.MaxChunkSize.ShouldBe(64);
settings.HeadStartSize.ShouldBe(2048);
settings.MaxHoldDuration.ShouldBe(TimeSpan.FromHours(1));
settings.ToConfiguration().Keys.ShouldBe(SettingKeys.HoldMethod, ignoreOrder: true);
}
@@ -56,6 +57,19 @@ public sealed class HoldMethodParserTests
errors.Keys.ShouldBe([nameof(HoldMethodInput.MaxDelayMs), nameof(HoldMethodInput.MaxChunkSize)], ignoreOrder: true);
}
[Theory]
[InlineData("0")]
[InlineData("65536")]
public void Accepts_head_start_bounds(string value) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { HeadStartSize = value }).Settings.ShouldNotBeNull();
[Theory]
[InlineData("-1")]
[InlineData("65537")]
[InlineData("")]
public void Rejects_head_start_out_of_range(string value) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { HeadStartSize = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.HeadStartSize)]);
[Theory]
[InlineData("10")]
[InlineData("00:00:00")]
@@ -22,6 +22,46 @@ public sealed class HtmlDripStrategyTests
Encoding.UTF8.GetString(channel.Writes.SelectMany(write => write).ToArray()).ShouldStartWith("<!doctype html>");
}
[Fact]
public async Task Sends_head_start_without_delay_before_dripping()
{
var options = new DripOptions { MinDelay = TimeSpan.FromSeconds(30), MaxDelay = TimeSpan.FromSeconds(30), HeadStartSize = 1024 };
var strategy = new HtmlDripStrategy(TestOptions.Monitor(options), new SeededRandomSource(21), TimeProvider.System);
var channel = new FakeTrapChannel();
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(200));
await Should.ThrowAsync<OperationCanceledException>(() => strategy.RunAsync(channel, cts.Token));
channel.Writes.Count.ShouldBe(2);
var head = channel.Writes.First();
head.Length.ShouldBeGreaterThanOrEqualTo(1024);
Encoding.UTF8.GetString(head).ShouldStartWith("<!doctype html>");
}
[Fact]
public async Task Skips_head_start_when_disabled()
{
var options = new DripOptions { MinDelay = TimeSpan.FromSeconds(30), MaxDelay = TimeSpan.FromSeconds(30), HeadStartSize = 0 };
var strategy = new HtmlDripStrategy(TestOptions.Monitor(options), new SeededRandomSource(21), TimeProvider.System);
var channel = new FakeTrapChannel();
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(200));
await Should.ThrowAsync<OperationCanceledException>(() => strategy.RunAsync(channel, cts.Token));
channel.Writes.Count.ShouldBe(1);
channel.Writes.First().Length.ShouldBeLessThanOrEqualTo(64);
}
[Theory]
[InlineData(1024, "html-drip 5-10ms 1-64B head 1024B")]
[InlineData(0, "html-drip 5-10ms 1-64B")]
public void Describe_marks_the_head_start_variant(int headStart, string expected)
{
var options = new DripOptions { MinDelay = TimeSpan.FromMilliseconds(5), MaxDelay = TimeSpan.FromMilliseconds(10), HeadStartSize = headStart };
new HtmlDripStrategy(TestOptions.Monitor(options), new SeededRandomSource(1), TimeProvider.System).Describe().ShouldBe(expected);
}
[Theory]
[InlineData(TrapProtocol.Http, true)]
[InlineData(TrapProtocol.Ssh, false)]
@@ -29,7 +69,7 @@ public sealed class HtmlDripStrategyTests
CreateStrategy().Supports(protocol).ShouldBe(expected);
private static HtmlDripStrategy CreateStrategy() => new(
TestOptions.Monitor(new DripOptions { MinDelay = TimeSpan.FromMilliseconds(1), MaxDelay = TimeSpan.FromMilliseconds(5) }),
TestOptions.Monitor(new DripOptions { MinDelay = TimeSpan.FromMilliseconds(1), MaxDelay = TimeSpan.FromMilliseconds(5), HeadStartSize = 0 }),
new SeededRandomSource(21),
TimeProvider.System);
}
@@ -44,7 +44,7 @@ public sealed class StrategySelectionTests
var options = TestOptions.Monitor(new DripOptions { MinDelay = TimeSpan.FromMilliseconds(100), MaxDelay = TimeSpan.FromMilliseconds(250), MinChunkSize = 2, MaxChunkSize = 8 });
var text = new TextDripStrategy(options, new SeededRandomSource(1), TimeProvider.System);
text.Describe().ShouldBe("text-drip 100-250ms 2-8B");
text.Describe().ShouldBe("text-drip 100-250ms 2-8B head 1024B");
text.MediaType.ShouldStartWith("text/plain");
new HtmlDripStrategy(options, new SeededRandomSource(1), TimeProvider.System).MediaType.ShouldStartWith("text/html");
}
+1 -1
View File
@@ -59,7 +59,7 @@ public sealed class TrapRunnerTests
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 == "html-drip 5-20ms 1-64B"));
_sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.Outcome == TrapOutcome.ClientAborted && e.Strategy == "html-drip 5-20ms 1-64B head 1024B"));
}
[Theory]