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

@@ -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);
}