-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not reuse Playwright session as this leaks memory
- Loading branch information
Showing
11 changed files
with
161 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Microsoft.Playwright; | ||
|
||
namespace ScreenshotCreator.Logic; | ||
|
||
public interface IPlaywrightFacade : IAsyncDisposable | ||
{ | ||
ValueTask<IPage> GetPlaywrightPageAsync(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
using Microsoft.Playwright; | ||
|
||
namespace ScreenshotCreator.Logic; | ||
namespace ScreenshotCreator.Logic; | ||
|
||
public interface IPlaywrightHelper | ||
{ | ||
ValueTask<IPage> InitializePlaywrightAsync(); | ||
IPlaywrightFacade CreatePlaywrightFacade(); | ||
|
||
Task WaitAsync(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.Playwright; | ||
|
||
namespace ScreenshotCreator.Logic; | ||
|
||
internal class PlaywrightFacade : IPlaywrightFacade | ||
{ | ||
private IBrowser? _browser; | ||
private bool _disposed; | ||
private IPage? _page; | ||
private IPlaywright? _playwright; | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
if (_disposed) return; | ||
|
||
if (_browser != null) await _browser.DisposeAsync(); | ||
_playwright?.Dispose(); | ||
|
||
_disposed = true; | ||
} | ||
|
||
public async ValueTask<IPage> GetPlaywrightPageAsync() | ||
{ | ||
_playwright = await Playwright.CreateAsync(); | ||
_browser = await _playwright.Chromium.LaunchAsync(); | ||
_page = await _browser.NewPageAsync(new BrowserNewPageOptions { TimezoneId = Environment.GetEnvironmentVariable("TZ") }); | ||
|
||
return _page; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using FluentAssertions; | ||
using ScreenshotCreator.Logic; | ||
|
||
namespace Tests.Integration.Logic; | ||
|
||
[TestFixture] | ||
[Category("Integration")] | ||
public class PlaywrightFacadeTests : PlaywrightTests | ||
{ | ||
[Test] | ||
public async Task GetPlaywrightPage_ShouldInitializePlaywrightAndCreateNewPage() | ||
{ | ||
// Arrange | ||
var testee = new PlaywrightFacade(); | ||
|
||
// Act | ||
var result = await testee.GetPlaywrightPageAsync(); | ||
|
||
// Assert | ||
result.Context.Browser.Should().NotBeNull(); | ||
} | ||
|
||
[Test] | ||
public async Task Dispose_ShouldDisposeUnderlyingPlaywrightSession() | ||
{ | ||
// Arrange | ||
var testee = new PlaywrightFacade(); | ||
var result = await testee.GetPlaywrightPageAsync(); | ||
|
||
// Act & Assert | ||
await testee.DisposeAsync(); | ||
|
||
// Assert | ||
var createScreenShotAsync = async () => await result.ScreenshotAsync(); | ||
await createScreenShotAsync.Should().ThrowAsync<Exception>().WithMessage("*disposed*"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Diagnostics; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Options; | ||
using ScreenshotCreator.Logic; | ||
|
||
namespace Tests.Integration.Logic; | ||
|
||
[TestFixture] | ||
[Category("Integration")] | ||
public class PlaywrightHelperTests : PlaywrightTests | ||
{ | ||
[Test] | ||
public void CreatePlaywrightFacade_ShouldCreateNewObjectEveryTime() | ||
{ | ||
// Arrange | ||
var testee = new PlaywrightHelper(Options.Create(new ScreenshotOptions())); | ||
|
||
// Act | ||
var result1 = testee.CreatePlaywrightFacade(); | ||
var result2 = testee.CreatePlaywrightFacade(); | ||
|
||
// Assert | ||
result1.Should().NotBeSameAs(result2); | ||
} | ||
|
||
[Test] | ||
public async Task Wait_ShouldWaitTheConfiguredAmountOfSeconds() | ||
{ | ||
// Arrange | ||
var testee = new PlaywrightHelper(Options.Create(new ScreenshotOptions { TimeBetweenHttpCallsInSeconds = 1 })); | ||
|
||
// Act | ||
var stopwatch = Stopwatch.StartNew(); | ||
await testee.WaitAsync(); | ||
stopwatch.Stop(); | ||
|
||
// Assert | ||
stopwatch.Elapsed.Should().BeCloseTo(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(100)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters