Skip to content

Commit

Permalink
Align per-test and per-class disposal
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed Jul 19, 2024
1 parent 9e18877 commit 484ae4e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 29 deletions.
23 changes: 4 additions & 19 deletions lib/PuppeteerSharp.Tests/PuppeteerBrowserBaseTest.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,27 @@
using System;
using System.Threading.Tasks;
using NUnit.Framework;

namespace PuppeteerSharp.Tests
{
public class PuppeteerBrowserBaseTest : PuppeteerBaseTest, IDisposable, IAsyncDisposable
public class PuppeteerBrowserBaseTest : PuppeteerBaseTest
{
protected IBrowser Browser { get; set; }

protected LaunchOptions DefaultOptions { get; set; }

[SetUp]
public virtual async Task InitializeAsync()
public async Task InitializeAsync()
=> Browser = await Puppeteer.LaunchAsync(
DefaultOptions ?? TestConstants.DefaultBrowserOptions(),
TestConstants.LoggerFactory);

[TearDown]
public virtual async Task DisposeAsync()
public async Task TearDownAsync()
{
if (Browser is not null)
{
await Browser.CloseAsync();
await Browser.DisposeAsync();
}
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing) => _ = DisposeAsync();

ValueTask IAsyncDisposable.DisposeAsync()
{
GC.SuppressFinalize(this);
return Browser.DisposeAsync();
}
}
}
6 changes: 2 additions & 4 deletions lib/PuppeteerSharp.Tests/TracingTests/TracingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace PuppeteerSharp.Tests.TracingTests
{
public class TracingTests : PuppeteerPageBaseTest
public sealed class TracingTests : PuppeteerPageBaseTest, IAsyncDisposable
{
private readonly string _file;

Expand All @@ -18,10 +18,8 @@ public TracingTests() : base()
_file = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
}

public override async Task DisposeAsync()
public async ValueTask DisposeAsync()
{
await base.DisposeAsync();

var attempts = 0;
const int maxAttempts = 5;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace PuppeteerSharp.Tests.WaitTaskTests
{
public sealed class FrameWaitForFunctionTests : PuppeteerPageBaseTest
public sealed class FrameWaitForFunctionTests : PuppeteerPageBaseTest, IDisposable
{
private PollerInterceptor _pollerInterceptor;

Expand All @@ -27,9 +27,8 @@ public FrameWaitForFunctionTests() : base()
};
}

protected override void Dispose(bool disposing)
public void Dispose()
{
base.Dispose(disposing);
_pollerInterceptor.Dispose();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
Expand All @@ -7,7 +8,7 @@

namespace PuppeteerSharp.Tests.WaitTaskTests
{
public class FrameWaitForSelectorTests : PuppeteerPageBaseTest
public sealed class FrameWaitForSelectorTests : PuppeteerPageBaseTest, IDisposable
{
private const string AddElement = "tag => document.body.appendChild(document.createElement(tag))";
private PollerInterceptor _pollerInterceptor;
Expand All @@ -29,9 +30,8 @@ public FrameWaitForSelectorTests()
};
}

protected override void Dispose(bool disposing)
public void Dispose()
{
base.Dispose(disposing);
_pollerInterceptor.Dispose();
}

Expand Down

0 comments on commit 484ae4e

Please sign in to comment.