Fix DbContext concurrency error in parallel company verification

Use IDbContextFactory pattern to create isolated DbContext instances
for each cache operation, making parallel verification thread-safe.

Changes:
- Add IDbContextFactory<ApplicationDbContext> registration
- Update CompanyVerifierService to use factory for cache operations
- Update tests with InMemoryDatabaseRoot for shared test data

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-20 16:54:58 +01:00
parent f1ccd217d8
commit 04a7c3628a
3 changed files with 50 additions and 13 deletions

View File

@@ -3,6 +3,8 @@ using System.Net.Http.Json;
using System.Text.Json;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
@@ -23,6 +25,8 @@ public class CompanyVerifierServiceTests : IDisposable
private readonly ApplicationDbContext _dbContext;
private readonly CompanyVerifierService _sut;
private readonly HttpClient _httpClient;
private readonly DbContextOptions<ApplicationDbContext> _dbOptions;
private readonly InMemoryDatabaseRoot _dbRoot;
private static readonly JsonSerializerOptions JsonOptions = new()
{
@@ -46,13 +50,25 @@ public class CompanyVerifierServiceTests : IDisposable
var client = new CompaniesHouseClient(_httpClient, settings, _mockClientLogger.Object);
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
// Use a shared InMemoryDatabaseRoot so all contexts share the same data store
_dbRoot = new InMemoryDatabaseRoot();
var dbName = Guid.NewGuid().ToString();
_dbOptions = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: dbName, databaseRoot: _dbRoot)
.ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning))
.Options;
_dbContext = new ApplicationDbContext(options);
_dbContext = new ApplicationDbContext(_dbOptions);
_sut = new CompanyVerifierService(client, _dbContext, _mockServiceLogger.Object);
// Create a mock factory that returns a new context with the same in-memory database
var mockFactory = new Mock<IDbContextFactory<ApplicationDbContext>>();
mockFactory.Setup(f => f.CreateDbContext())
.Returns(() => new ApplicationDbContext(_dbOptions));
mockFactory.Setup(f => f.CreateDbContextAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(() => new ApplicationDbContext(_dbOptions));
_sut = new CompanyVerifierService(client, mockFactory.Object, _mockServiceLogger.Object);
}
public void Dispose()
@@ -313,8 +329,9 @@ public class CompanyVerifierServiceTests : IDisposable
// Act
await _sut.VerifyCompanyAsync(companyName, null, null);
// Assert
// Assert - use AsNoTracking to get fresh data from the store
var cachedEntry = await _dbContext.CompanyCache
.AsNoTracking()
.FirstOrDefaultAsync(c => c.CompanyNumber == "77777777");
cachedEntry.Should().NotBeNull();
@@ -348,8 +365,9 @@ public class CompanyVerifierServiceTests : IDisposable
// Act
await _sut.VerifyCompanyAsync(companyName, null, null);
// Assert
// Assert - use AsNoTracking to get fresh data from the store
var cachedEntry = await _dbContext.CompanyCache
.AsNoTracking()
.FirstOrDefaultAsync(c => c.CompanyNumber == "55555555");
cachedEntry.Should().NotBeNull();