Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Fix race condition in E2E tests caused by worker status razor component (#240)

Leendert de Borst5c619d9553c3
diff --git a/src/AliasVault.Admin/Main/Components/WorkerStatus/Services.razor b/src/AliasVault.Admin/Main/Components/WorkerStatus/Services.razor
index df040bbaf..2eb87568a 100644
--- a/src/AliasVault.Admin/Main/Components/WorkerStatus/Services.razor
+++ b/src/AliasVault.Admin/Main/Components/WorkerStatus/Services.razor
@@ -21,7 +21,6 @@
private bool SmtpStatus;
private bool SmtpPending;
private DateTime LastHeartbeat;
- private readonly SemaphoreSlim InitLock = new(1, 1);
/// <summary>
/// The interval in milliseconds for refreshing the service status.
@@ -29,9 +28,14 @@
private readonly int AutoRefreshInterval = 5000;
/// <summary>
- /// The timer for refreshing the service status.
+ /// SemaphoreSlim to prevent concurrent execution of InitPage
/// </summary>
- private Timer? Timer;
+ private SemaphoreSlim InitLock = new(1, 1);
+
+ /// <summary>
+ /// CancellationTokenSource for the timer.
+ /// </summary>
+ private CancellationTokenSource? _timerCancellationTokenSource;
/// <inheritdoc />
protected override async Task OnAfterRenderAsync(bool firstRender)
@@ -40,10 +44,17 @@
if (firstRender)
{
- Timer = new Timer(async _ =>
- {
- await InitPage();
- }, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(AutoRefreshInterval));
+ _timerCancellationTokenSource = new CancellationTokenSource();
+ _ = RunPeriodicRefreshAsync(_timerCancellationTokenSource.Token);
+ }
+ }
+
+ private async Task RunPeriodicRefreshAsync(CancellationToken cancellationToken)
+ {
+ while (!cancellationToken.IsCancellationRequested)
+ {
+ await InitPage();
+ await Task.Delay(AutoRefreshInterval, cancellationToken);
}
}
@@ -54,8 +65,9 @@
if (disposing)
{
- // Dispose of the timer if it exists.
- Timer?.Dispose();
+ _timerCancellationTokenSource?.Cancel();
+ _timerCancellationTokenSource?.Dispose();
+ InitLock.Dispose();
}
}
@@ -126,51 +138,33 @@
/// </summary>
private async Task InitPage()
{
- if (InitInProgress)
- {
- return;
- }
-
- try
+ if (await InitLock.WaitAsync(0))
{
- await InitLock.WaitAsync();
-
- if (InitInProgress)
+ try
{
- return;
- }
+ if (InitInProgress || SmtpPending)
+ {
+ return;
+ }
- if (!SmtpPending)
- {
InitInProgress = true;
+ var dbContext = await DbContextFactory.CreateDbContextAsync();
+ ServiceStatus = await dbContext.WorkerServiceStatuses.ToListAsync();
- try
- {
- InitInProgress = true;
- var dbContext = await DbContextFactory.CreateDbContextAsync();
- ServiceStatus = await dbContext.WorkerServiceStatuses.ToListAsync();
- // Service status checks if the status is "Started" and was lastAlive
- // (so actually reported itself) in the last 5 minutes.
- var smtpEntry = ServiceStatus.Find(x => x.ServiceName == "AliasVault.SmtpService");
- if (smtpEntry != null)
- {
- LastHeartbeat = smtpEntry.Heartbeat;
- SmtpStatus = IsHeartbeatValid() && smtpEntry.CurrentStatus == "Started";
- }
-
- InitInProgress = false;
-
- await InvokeAsync(() => StateHasChanged());
- }
- finally
+ var smtpEntry = ServiceStatus.Find(x => x.ServiceName == "AliasVault.SmtpService");
+ if (smtpEntry != null)
{
- InitInProgress = false;
+ LastHeartbeat = smtpEntry.Heartbeat;
+ SmtpStatus = IsHeartbeatValid() && smtpEntry.CurrentStatus == "Started";
}
+
+ await InvokeAsync(StateHasChanged);
+ }
+ finally
+ {
+ InitInProgress = false;
+ InitLock.Release();
}
- }
- finally
- {
- InitLock.Release();
}
}
diff --git a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationAdminFactoryFixture.cs b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationAdminFactoryFixture.cs
index f8124dd50..e390940b1 100644
--- a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationAdminFactoryFixture.cs
+++ b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationAdminFactoryFixture.cs
@@ -151,14 +151,10 @@ public class WebApplicationAdminFactoryFixture<TEntryPoint> : WebApplicationFact
/// <param name="services">The <see cref="IServiceCollection"/> to modify.</param>
private void AddNewRegistrations(IServiceCollection services)
{
- // Add the DbConnection as a singleton
- services.AddSingleton(_dbConnection);
-
// Add the DbContextFactory
- services.AddDbContextFactory<AliasServerDbContext>((container, options) =>
+ services.AddDbContextFactory<AliasServerDbContext>(options =>
{
- var connection = container.GetRequiredService<DbConnection>();
- options.UseSqlite(connection).UseLazyLoadingProxies();
+ options.UseSqlite(_dbConnection).UseLazyLoadingProxies();
});
// Add the VersionedContentService
diff --git a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
index c8d0e86ea..6a57df006 100644
--- a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
+++ b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
@@ -155,14 +155,10 @@ public class WebApplicationApiFactoryFixture<TEntryPoint> : WebApplicationFactor
/// <param name="services">The <see cref="IServiceCollection"/> to modify.</param>
private void AddNewRegistrations(IServiceCollection services)
{
- // Add the DbConnection as a singleton
- services.AddSingleton(_dbConnection);
-
// Add the DbContextFactory
- services.AddDbContextFactory<AliasServerDbContext>((container, options) =>
+ services.AddDbContextFactory<AliasServerDbContext>(options =>
{
- var connection = container.GetRequiredService<DbConnection>();
- options.UseSqlite(connection).UseLazyLoadingProxies();
+ options.UseSqlite(_dbConnection).UseLazyLoadingProxies();
});
// Add TestTimeProvider