Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/AngleSharp.Renderer.Tests/BrowserIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ private sealed class GatedPngRequester(byte[] png) : BaseRequester
{
public ManualResetEventSlim Gate { get; } = new(false);

public ManualResetEventSlim CopyStarted { get; } = new(false);

public override bool SupportsProtocol(string protocol) => true;

protected override Task<IResponse?> PerformRequestAsync(Request request, CancellationToken cancel) =>
Expand All @@ -308,11 +310,24 @@ private sealed class GatedPngRequester(byte[] png) : BaseRequester
Address = request.Address,
StatusCode = HttpStatusCode.OK,
Headers = new Dictionary<string, string> { ["Content-Type"] = "image/png" },
Content = new GatedStream(png, Gate),
Content = new GatedStream(png, Gate, CopyStarted),
});

private sealed class GatedStream(byte[] data, ManualResetEventSlim gate) : MemoryStream(data)
private sealed class GatedStream(byte[] data, ManualResetEventSlim gate, ManualResetEventSlim copyStarted) : MemoryStream(data)
{
public override void CopyTo(Stream destination, int bufferSize)
{
gate.Wait();
base.CopyTo(destination, bufferSize);
}

public override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
{
copyStarted.Set();
await Task.Run(() => gate.Wait(cancellationToken), cancellationToken);
await base.CopyToAsync(destination, bufferSize, cancellationToken);
}

public override int Read(byte[] buffer, int offset, int count)
{
gate.Wait();
Expand Down Expand Up @@ -360,6 +375,7 @@ public async Task FrameBeforeTheImageIsProcessedLeavesItsResponseToTheImageServi
document.Body!.AppendChild(image);
var download = ((ILoadableElement)image).CurrentDownload!;
await download.Task.WaitAsync(TimeSpan.FromSeconds(10));
Assert.True(await Task.Run(() => requester.CopyStarted.Wait(TimeSpan.FromSeconds(10))));

// the image service waits for the body now; the frame must not read it
await Task.Run(() => Render(document)).WaitAsync(TimeSpan.FromSeconds(10));
Expand Down
Loading