Skip to content

Made the test non parallelizable #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
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
40 changes: 39 additions & 1 deletion Chirp/test/PlaywrightTests/CustomWebApplicationFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Data.Common;
using System.Net.Sockets;
using Chirp.Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
Expand All @@ -15,7 +18,7 @@ namespace PlaywrightTests;
//https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-8.0#customize-webapplicationfactory
//Further Adapted from this Medium posts:
//https://medium.com/younited-tech-blog/end-to-end-test-a-blazor-app-with-playwright-part-3-48c0edeff4b6
public class CustomWebApplicationFactory
public class CustomWebApplicationFactory(String baseUrl)
: InMemoryCostumeWebApplicationFactory<Chirp.Web.Program>
{
protected override IHost CreateHost(IHostBuilder builder)
Expand All @@ -29,10 +32,45 @@ protected override IHost CreateHost(IHostBuilder builder)

var host = builder.Build();
host.Start();

// Wait until the server is listening
WaitUntilServerIsAvailable(baseUrl);

// In order to cleanup and properly dispose HTTP server
// resources we return a composite host object that is
// actually just a way to intercept the StopAsync and Dispose
// call and relay to our HTTP host.
return new CompositeHost(testHost, host);
}

private void WaitUntilServerIsAvailable(string url)
{
var uri = new Uri(url);
using var client = new TcpClient();

var maxAttempts = 5;
var attempt = 0;

while (attempt < maxAttempts)
{
try
{
client.Connect(uri.Host, uri.Port);
if (client.Connected)
{
break;
}
}
catch
{
attempt++;
Thread.Sleep(1000);
}
}

if (attempt == maxAttempts)
{
throw new Exception("Server did not start in time.");
}
}
}
3 changes: 2 additions & 1 deletion Chirp/test/PlaywrightTests/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

namespace PlaywrightTests;

[Parallelizable(ParallelScope.Self)]
//[Parallelizable(ParallelScope.Self)]
[TestFixture]
[NonParallelizable]
public class EndToEndTests : PageTestWithCustomWebApplicationFactory
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override BrowserNewContextOptions ContextOptions()
}

[OneTimeSetUp]
public void OneTimeSetUp() => _factory = new CustomWebApplicationFactory();
public void OneTimeSetUp() => _factory = new CustomWebApplicationFactory(BaseUrl);

[SetUp]
public void Setup()
Expand Down
1 change: 1 addition & 0 deletions Chirp/test/PlaywrightTests/UITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace PlaywrightTests;

[NonParallelizable]
public class UITest : PageTestWithCustomWebApplicationFactory
{
[Test]
Expand Down
Loading