56 lines
1.9 KiB
C#
56 lines
1.9 KiB
C#
using System.Text;
|
|
using Flypaper.Core.Strategies;
|
|
using Flypaper.Tests.Fakes;
|
|
using Shouldly;
|
|
|
|
namespace Flypaper.Tests.Core;
|
|
|
|
public sealed class DripScheduleTests
|
|
{
|
|
private static readonly DripOptions Options = new()
|
|
{
|
|
MinDelay = TimeSpan.FromMilliseconds(50),
|
|
MaxDelay = TimeSpan.FromSeconds(1),
|
|
MinChunkSize = 1,
|
|
MaxChunkSize = 64
|
|
};
|
|
|
|
[Fact]
|
|
public void Uses_random_chunk_sizes_and_delays_within_bounds()
|
|
{
|
|
var random = new SeededRandomSource(11);
|
|
var steps = DripSchedule.Create(HtmlContent.Generate(random), random, Options).Take(2000).ToList();
|
|
|
|
steps.ShouldAllBe(step => step.Chunk.Length >= 1 && step.Chunk.Length <= 64);
|
|
steps.ShouldAllBe(step => step.Delay >= TimeSpan.FromMilliseconds(50) && step.Delay <= TimeSpan.FromSeconds(1));
|
|
steps.Select(step => step.Chunk.Length).Distinct().Count().ShouldBeGreaterThan(40);
|
|
steps.Select(step => step.Delay).Distinct().Count().ShouldBeGreaterThan(500);
|
|
}
|
|
|
|
[Fact]
|
|
public void Delivers_the_content_unchanged_and_in_order()
|
|
{
|
|
var content = new[] { "<!doctype html>", "<p>hällo ", "world</p>", "\n" };
|
|
var steps = DripSchedule.Create(content, new SeededRandomSource(12), Options).ToList();
|
|
|
|
Encoding.UTF8.GetString(steps.SelectMany(step => step.Chunk.ToArray()).ToArray()).ShouldBe(string.Concat(content));
|
|
}
|
|
|
|
[Fact]
|
|
public void Ends_when_content_ends()
|
|
{
|
|
DripSchedule.Create([], new SeededRandomSource(13), Options).ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void Tolerates_inverted_bounds()
|
|
{
|
|
var options = new DripOptions { MinDelay = TimeSpan.FromSeconds(2), MaxDelay = TimeSpan.FromSeconds(1), MinChunkSize = 10, MaxChunkSize = 5 };
|
|
|
|
var step = DripSchedule.Create(["0123456789abcdef"], new SeededRandomSource(14), options).First();
|
|
|
|
step.Chunk.Length.ShouldBe(10);
|
|
step.Delay.ShouldBe(TimeSpan.FromSeconds(2));
|
|
}
|
|
}
|