Skip to content

Commit

Permalink
Add timeouts to ShouldWorkWithBothDomcontentloadedAndLoad (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed Dec 24, 2018
1 parent 1a6b865 commit f11be1f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lib/PuppeteerSharp.Tests/PageTests/WaitForNavigationTests.cs
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using PuppeteerSharp.Helpers;

namespace PuppeteerSharp.Tests.PageTests
{
Expand Down Expand Up @@ -34,6 +35,8 @@ public async Task ShouldWorkWithBothDomcontentloadedAndLoad()
{
return responseCompleted.Task;
});

var waitForRequestTask = Server.WaitForRequest("/one-style.css");
var navigationTask = Page.GoToAsync(TestConstants.ServerUrl + "/one-style.html");
var domContentLoadedTask = Page.WaitForNavigationAsync(new NavigationOptions
{
Expand All @@ -50,12 +53,12 @@ public async Task ShouldWorkWithBothDomcontentloadedAndLoad()
}
}).ContinueWith(_ => bothFired = true);

await Server.WaitForRequest("/one-style.css");
await domContentLoadedTask;
await waitForRequestTask.WithTimeout();
await domContentLoadedTask.WithTimeout();
Assert.False(bothFired);
responseCompleted.SetResult(true);
await bothFiredTask;
await navigationTask;
await bothFiredTask.WithTimeout();
await navigationTask.WithTimeout();
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp.Tests/WorkerTests/WorkerTests.cs
Expand Up @@ -30,7 +30,7 @@ public async Task PageWorkers()
Assert.Equal("worker function result", await worker.EvaluateExpressionAsync<string>("self.workerFunction()"));

await Page.GoToAsync(TestConstants.EmptyPage);
await workerDestroyedTcs.Task.WithTimeout(1000);
await workerDestroyedTcs.Task.WithTimeout();
Assert.Empty(Page.Workers);
}

Expand Down
28 changes: 28 additions & 0 deletions lib/PuppeteerSharp/Helpers/TaskHelper.cs
Expand Up @@ -9,6 +9,34 @@ namespace PuppeteerSharp.Helpers
/// </summary>
public static class TaskHelper
{
//Recipe from https://blogs.msdn.microsoft.com/pfxteam/2012/10/05/how-do-i-cancel-non-cancelable-async-operations/
/// <summary>
/// Cancels the <paramref name="task"/> after <paramref name="milliseconds"/> milliseconds
/// </summary>
/// <returns>The task result.</returns>
/// <param name="task">Task to wait for.</param>
/// <param name="milliseconds">Milliseconds timeout.</param>
public static async Task WithTimeout(this Task task, int milliseconds = 1_000)
{
var tcs = new TaskCompletionSource<bool>();
var cancellationToken = new CancellationTokenSource();

if (milliseconds > 0)
{
cancellationToken.CancelAfter(milliseconds);
}

using (cancellationToken.Token.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
{
if (task != await Task.WhenAny(task, tcs.Task))
{
throw new TimeoutException($"Timeout Exceeded: {milliseconds}ms exceeded");
}
}

await task;
}

//Recipe from https://blogs.msdn.microsoft.com/pfxteam/2012/10/05/how-do-i-cancel-non-cancelable-async-operations/
/// <summary>
/// Cancels the <paramref name="task"/> after <paramref name="milliseconds"/> milliseconds
Expand Down

0 comments on commit f11be1f

Please sign in to comment.