Skip to content

Commit 9f4ca45

Browse files
authored
Add support for await using by implementing IAsyncDisposable for Page and Browser. Fix #1456 (#1458)
1 parent 9ea9110 commit 9f4ca45

File tree

5 files changed

+60
-16
lines changed

5 files changed

+60
-16
lines changed

lib/PuppeteerSharp.Tests/PageTests/CloseTests.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
22
using Xunit;
33
using Xunit.Abstractions;
44

@@ -24,11 +24,21 @@ await Task.WhenAll(
2424
Assert.Equal("Target.detachedFromTarget", exception.CloseReason);
2525
}
2626

27-
[Fact]
28-
public async Task ShouldNotBeVisibleInBrowserPages()
27+
[Theory]
28+
[InlineData(false)]
29+
[InlineData(true)]
30+
public async Task ShouldNotBeVisibleInBrowserPages(bool useDisposeAsync)
2931
{
3032
Assert.Contains(Page, await Browser.PagesAsync());
31-
await Page.CloseAsync();
33+
if (useDisposeAsync)
34+
{
35+
// emulates what would happen in a C#8 await using block
36+
await Page.DisposeAsync();
37+
}
38+
else
39+
{
40+
await Page.CloseAsync();
41+
}
3242
Assert.DoesNotContain(Page, await Browser.PagesAsync());
3343
}
3444

@@ -103,4 +113,4 @@ public async Task ShouldCloseWhenConnectionBreaksPrematurely()
103113
await Page.CloseAsync();
104114
}
105115
}
106-
}
116+
}

lib/PuppeteerSharp.Tests/PuppeteerTests/PuppeteerLaunchTests.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Diagnostics;
33
using System.IO;
44
using System.Linq;
@@ -184,8 +184,10 @@ public void ShouldReturnTheDefaultChromeArguments()
184184
}));
185185
}
186186

187-
[Fact]
188-
public async Task ChromeShouldBeClosed()
187+
[Theory]
188+
[InlineData(false)]
189+
[InlineData(true)]
190+
public async Task ChromeShouldBeClosed(bool useDisposeAsync)
189191
{
190192
var options = TestConstants.DefaultBrowserOptions();
191193
var launcher = new Launcher(TestConstants.LoggerFactory);
@@ -196,7 +198,15 @@ public async Task ChromeShouldBeClosed()
196198
var response = await page.GoToAsync(TestConstants.EmptyPage);
197199
Assert.Equal(HttpStatusCode.OK, response.Status);
198200

199-
await browser.CloseAsync();
201+
if (useDisposeAsync)
202+
{
203+
// emulates what would happen in a C#8 await using block
204+
await browser.DisposeAsync();
205+
}
206+
else
207+
{
208+
await browser.CloseAsync();
209+
}
200210

201211
Assert.True(launcher.Process.HasExited);
202212
}
@@ -366,4 +376,4 @@ public async Task ShouldSupportCustomTransport()
366376
}
367377
}
368378
}
369-
}
379+
}

lib/PuppeteerSharp/Browser.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Diagnostics;
@@ -35,7 +35,7 @@ namespace PuppeteerSharp
3535
/// ]]>
3636
/// </code>
3737
/// </example>
38-
public class Browser : IDisposable
38+
public class Browser : IDisposable, IAsyncDisposable
3939
{
4040
/// <summary>
4141
/// Time in milliseconds for chromium process to exit gracefully.
@@ -537,5 +537,16 @@ internal static async Task<Browser> CreateAsync(
537537
public void Dispose() => _ = CloseAsync();
538538

539539
#endregion
540+
541+
#region IAsyncDisposable
542+
543+
/// <summary>
544+
/// Closes <see cref="Connection"/> and any Chromium <see cref="Process"/> that was
545+
/// created by Puppeteer.
546+
/// </summary>
547+
/// <returns>ValueTask</returns>
548+
public ValueTask DisposeAsync() => new ValueTask(CloseAsync());
549+
550+
#endregion
540551
}
541552
}

lib/PuppeteerSharp/Page.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Diagnostics;
@@ -35,7 +35,7 @@ namespace PuppeteerSharp
3535
/// </code>
3636
/// </example>
3737
[DebuggerDisplay("Page {Url}")]
38-
public class Page : IDisposable
38+
public class Page : IDisposable, IAsyncDisposable
3939
{
4040
private readonly TaskQueue _screenshotTaskQueue;
4141
private readonly EmulationManager _emulationManager;
@@ -2430,5 +2430,17 @@ string SerializeArgument(object arg)
24302430
/// the garbage collector can reclaim the memory that the <see cref="Page"/> was occupying.</remarks>
24312431
public void Dispose() => CloseAsync();
24322432
#endregion
2433+
2434+
#region IAsyncDisposable
2435+
/// <summary>
2436+
/// Releases all resource used by the <see cref="Page"/> object by calling the <see cref="CloseAsync"/> method.
2437+
/// </summary>
2438+
/// <remarks>Call <see cref="DisposeAsync"/> when you are finished using the <see cref="Page"/>. The
2439+
/// <see cref="DisposeAsync"/> method leaves the <see cref="Page"/> in an unusable state. After
2440+
/// calling <see cref="DisposeAsync"/>, you must release all references to the <see cref="Page"/> so
2441+
/// the garbage collector can reclaim the memory that the <see cref="Page"/> was occupying.</remarks>
2442+
/// <returns>ValueTask</returns>
2443+
public ValueTask DisposeAsync() => new ValueTask(CloseAsync());
2444+
#endregion
24332445
}
24342446
}

lib/PuppeteerSharp/PuppeteerSharp.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<PackOnBuild>true</PackOnBuild>
@@ -34,7 +34,8 @@
3434
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
3535
<PackageReference Include="System.Net.Http" Version="4.3.3" />
3636
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.0.2" />
37-
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.1" />
37+
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.1.0" />
38+
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.2" />
3839
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
3940
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4041
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)