43 lines
1.5 KiB
C#
43 lines
1.5 KiB
C#
using Flypaper.Core;
|
|
using Flypaper.Core.Strategies;
|
|
using Shouldly;
|
|
|
|
namespace Flypaper.Tests;
|
|
|
|
public sealed class DripStrategyTests
|
|
{
|
|
[Fact]
|
|
public async Task Drips_single_bytes_until_cancelled()
|
|
{
|
|
var strategy = CreateStrategy(TimeSpan.FromMilliseconds(20));
|
|
var channel = new FakeTrapChannel();
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(300));
|
|
|
|
await Should.ThrowAsync<OperationCanceledException>(() => strategy.RunAsync(channel, cts.Token));
|
|
|
|
channel.Writes.Count.ShouldBeInRange(3, 20);
|
|
channel.Writes.ShouldAllBe(write => write.Length == 1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Writes_first_byte_immediately()
|
|
{
|
|
var strategy = CreateStrategy(TimeSpan.FromMinutes(1));
|
|
var channel = new FakeTrapChannel();
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
|
|
|
|
await Should.ThrowAsync<OperationCanceledException>(() => strategy.RunAsync(channel, cts.Token));
|
|
|
|
channel.Writes.Count.ShouldBe(1);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(TrapProtocol.Http, true)]
|
|
[InlineData(TrapProtocol.Ssh, false)]
|
|
public void Supports_only_http(TrapProtocol protocol, bool expected) =>
|
|
CreateStrategy(TimeSpan.FromSeconds(1)).Supports(protocol).ShouldBe(expected);
|
|
|
|
private static DripStrategy CreateStrategy(TimeSpan interval) =>
|
|
new(TestOptions.Monitor(new DripOptions { Interval = interval }), TimeProvider.System);
|
|
}
|