Compare commits

..

1 Commits

Author SHA1 Message Date
cesnimda 2ec595db3b test: qualify concurrent preferences on MariaDB 2026-12-04 09:10:11 +01:00
5 changed files with 145 additions and 52 deletions
+18 -37
View File
@@ -93,45 +93,26 @@ jobs:
# The Debian runner omits pip and python3-venv. Use pip's official
# zipapp and install into the checkout so CI remains isolated without
# sudo or a mutable runner-global Python environment.
python_tmp="$(mktemp -d)"
trap 'rm -rf -- "$python_tmp"' EXIT
curl -fsSLo "$python_tmp/pip.pyz" https://bootstrap.pypa.io/pip/pip.pyz
install_locked() {
local interpreter="$1" destination="$2" attempt scratch
shift 2
for attempt in 1 2 3; do
scratch="$(mktemp -d "$python_tmp/install.XXXXXX")"
if PIP_CACHE_DIR="$scratch/cache" "$interpreter" "$python_tmp/pip.pyz" install \
--target "$scratch/site" --require-hashes "$@"; then
mv "$scratch/site" "$destination"
return
fi
echo "Locked Python dependency install attempt $attempt failed."
done
return 1
curl -fsSLo /tmp/pip.pyz https://bootstrap.pypa.io/pip/pip.pyz
install_parser_dependencies() {
rm -rf .python-ci
parser_pip_cache="$(mktemp -d)"
PIP_CACHE_DIR="$parser_pip_cache" python3 /tmp/pip.pyz install --target .python-ci --require-hashes \
--extra-index-url https://download.pytorch.org/whl/cpu \
-r requirements-linux.lock
}
parser_install_succeeded=false
for attempt in 1 2 3; do
if install_parser_dependencies; then
parser_install_succeeded=true
break
fi
echo "Parser dependency install attempt $attempt failed — retrying with a fresh isolated cache..."
done
test "$parser_install_succeeded" = true
PIP_CACHE_DIR="$parser_pip_cache" python3 /tmp/pip.pyz install --upgrade --target .python-ci pytest==8.3.5 httpx2==2.12.0
python3 --version
install_locked python3 "$python_tmp/sidecar" --extra-index-url https://download.pytorch.org/whl/cpu \
-r requirements-dev-linux.lock
PYTHONFAULTHANDLER=1 PYTHONPATH="$python_tmp/sidecar" AI_SERVICE_SKIP_MODEL_LOAD=1 \
python3 -m pytest -q --ignore=tests/test_document_extractor.py
# The CV parser uses a separate dependency set. Its lock already pins
# pluggy and pygments; add the remaining hashed pytest requirements.
# The Docling lock targets Python 3.12, while the Debian runner uses 3.11.
cv_archive="$python_tmp/cv-python.tar.gz"
curl -fsSL --retry 2 -o "$cv_archive" \
'https://github.com/astral-sh/python-build-standalone/releases/download/20250517/cpython-3.12.10%2B20250517-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz'
echo "0356bd1db292781a20e011d789f0c41ea5ec04731c8236c8185b53a54faf2871 $cv_archive" | sha256sum -c -
tar -xzf "$cv_archive" -C "$python_tmp"
cv_python="$python_tmp/python/bin/python3"
"$cv_python" --version
awk '/^[^[:space:]#]/ { include = /^(iniconfig|packaging|pytest)==/ }
include && !/^[[:space:]]*#/ { print }' requirements-dev-linux.lock > "$python_tmp/cv-test-tools.lock"
test "$(grep -Ec '^(iniconfig|packaging|pytest)==' "$python_tmp/cv-test-tools.lock")" -eq 3
install_locked "$cv_python" "$python_tmp/cv-parser" -r requirements-cv-import.lock -r "$python_tmp/cv-test-tools.lock"
PYTHONFAULTHANDLER=1 PYTHONPATH="$python_tmp/cv-parser" OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 \
"$cv_python" -m pytest -q tests/test_document_extractor.py
PYTHONFAULTHANDLER=1 PYTHONPATH="$PWD/.python-ci" AI_SERVICE_SKIP_MODEL_LOAD=1 python3 -m pytest -q
- name: Restore backend
# The runner/proxy has occasionally supplied checksum-invalid NuGet cache entries (NU3008).
@@ -88,6 +88,7 @@ internal sealed class MariaDbDatabase : IAsyncDisposable
builder.Services.AddSingleton<IStartupReadiness, StartupReadiness>();
builder.Services.AddDbContext<JobTrackerContext>(options => database.Configure(options));
builder.Services.AddIdentityCore<ApplicationUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<JobTrackerContext>();
builder.Services.AddControllers();
database.App = builder.Build();
await database.App.InitializeJobTrackerAsync();
return database;
@@ -0,0 +1,111 @@
using System.Security.Claims;
using JobTrackerApi.Controllers;
using JobTrackerApi.Data;
using JobTrackerApi.Models;
using JobTrackerApi.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;
using Xunit.Abstractions;
namespace JobTrackerApi.MariaDb.Tests;
public sealed class PreferenceConcurrencyTests(ITestOutputHelper output)
{
[Fact]
public async Task Concurrent_language_and_notification_writes_preserve_the_winner_and_allow_a_fresh_retry()
{
await using var database = await MariaDbDatabase.CreateAsync();
await using (var seed = database.Context())
{
seed.Users.AddRange(
new ApplicationUser { Id = "preference-owner", UserName = "owner@example.test", UiLanguage = "en", EmailFollowUpRemindersEnabled = true },
new ApplicationUser { Id = "preference-other", UserName = "other@example.test", UiLanguage = "nb-NO", EmailFollowUpRemindersEnabled = false });
await seed.SaveChangesAsync();
}
await using var languageScope = database.App.Services.CreateAsyncScope();
await using var notificationScope = database.App.Services.CreateAsyncScope();
await using var otherScope = database.App.Services.CreateAsyncScope();
var language = LanguageController(languageScope.ServiceProvider, "preference-owner");
var notification = NotificationController(notificationScope.ServiceProvider, "preference-owner");
var otherLanguage = LanguageController(otherScope.ServiceProvider, "preference-other");
var otherNotification = NotificationController(otherScope.ServiceProvider, "preference-other");
// Model two requests that have read the same Identity version before either writes.
var languageSnapshot = await languageScope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>().GetUserAsync(language.User);
var notificationSnapshot = await notificationScope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>().GetUserAsync(notification.User);
Assert.NotNull(languageSnapshot);
Assert.NotNull(notificationSnapshot);
Assert.NotSame(languageSnapshot, notificationSnapshot);
var originalStamp = languageSnapshot.ConcurrencyStamp;
Assert.Equal(originalStamp, notificationSnapshot.ConcurrencyStamp);
var languageWrite = language.UpdateLanguagePreference(new("nb-NO"));
var notificationWrite = notification.Put(new(false));
var otherWrite = otherLanguage.UpdateLanguagePreference(new("en"));
await Task.WhenAll(languageWrite, notificationWrite, otherWrite);
Assert.IsType<NoContentResult>(await otherWrite);
Assert.IsType<BadRequestObjectResult>(await otherLanguage.UpdateLanguagePreference(new("fr-FR")));
Assert.IsType<OkObjectResult>((await otherNotification.Put(new(true))).Result);
var languageWon = await languageWrite is NoContentResult;
if (languageWon)
{
var error = Assert.IsType<ObjectResult>((await notificationWrite).Result);
Assert.Equal(StatusCodes.Status500InternalServerError, error.StatusCode);
}
else
{
var error = Assert.IsType<BadRequestObjectResult>(await languageWrite);
Assert.Contains(new IdentityErrorDescriber().ConcurrencyFailure().Description, Assert.IsType<string>(error.Value));
Assert.IsType<OkObjectResult>((await notificationWrite).Result);
}
await using (var read = database.Context())
{
var owner = await read.Users.AsNoTracking().SingleAsync(user => user.Id == "preference-owner");
Assert.Equal(languageWon ? "nb-NO" : "en", owner.UiLanguage);
Assert.Equal(languageWon, owner.EmailFollowUpRemindersEnabled);
Assert.NotEqual(originalStamp, owner.ConcurrencyStamp);
}
await using var retryScope = database.App.Services.CreateAsyncScope();
if (languageWon)
Assert.IsType<OkObjectResult>((await NotificationController(retryScope.ServiceProvider, "preference-owner").Put(new(false))).Result);
else
Assert.IsType<NoContentResult>(await LanguageController(retryScope.ServiceProvider, "preference-owner").UpdateLanguagePreference(new("nb-NO")));
await using var reload = database.Context();
var users = await reload.Users.AsNoTracking().Where(user => user.Id.StartsWith("preference-")).OrderBy(user => user.Id).ToListAsync();
Assert.Collection(users,
other => { Assert.Equal("preference-other", other.Id); Assert.Equal("en", other.UiLanguage); Assert.True(other.EmailFollowUpRemindersEnabled); },
owner => { Assert.Equal("preference-owner", owner.Id); Assert.Equal("nb-NO", owner.UiLanguage); Assert.False(owner.EmailFollowUpRemindersEnabled); });
output.WriteLine($"MariaDB {database.ActualVersion}: {(languageWon ? "language" : "notification")} won the overlapping writes; fresh retry preserved both preferences and the other owner's independent writes.");
}
private static AuthController LanguageController(IServiceProvider services, string owner) => new(
services.GetRequiredService<IConfiguration>(), services.GetRequiredService<UserManager<ApplicationUser>>(),
Mock.Of<ITokenService>(), Mock.Of<IAppEmailSender>(), Mock.Of<IGoogleTokenValidator>(), Mock.Of<IMicrosoftTokenValidator>(),
NullLogger<AuthController>.Instance, Mock.Of<ITwoFactorPendingTokenService>(), services.GetRequiredService<JobTrackerContext>(),
externalOrigin: ExternalOrigin.Parse("http://localhost:3300", production: false))
{ ControllerContext = Principal(services, owner) };
private static NotificationSettingsController NotificationController(IServiceProvider services, string owner) => new(
services.GetRequiredService<UserManager<ApplicationUser>>()) { ControllerContext = Principal(services, owner) };
private static ControllerContext Principal(IServiceProvider services, string owner) => new()
{
HttpContext = new DefaultHttpContext
{
RequestServices = services,
User = new ClaimsPrincipal(new ClaimsIdentity([new Claim(ClaimTypes.NameIdentifier, owner)], "test")),
},
};
}
+14 -14
View File
@@ -30,7 +30,7 @@
"react": "^19.2.4",
"react-dom": "^19.2.4",
"react-grid-layout": "2.3.0",
"react-router-dom": "^7.18.5",
"react-router-dom": "^7.18.2",
"typescript": "^5.9.3",
"web-vitals": "^2.1.4"
},
@@ -6285,9 +6285,9 @@
}
},
"node_modules/cross-spawn": {
"version": "7.1.2",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.1.2.tgz",
"integrity": "sha512-8BPK2r+sKWC8sDinRwLwrFMq2lXTBBwzYYEB9LiCznfMurOi4FOvQflq3XOLGG+HPdMSVpe+k/s96+AtDEOgPg==",
"version": "7.0.6",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
"integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==",
"dev": true,
"license": "MIT",
"dependencies": {
@@ -11055,9 +11055,9 @@
}
},
"node_modules/react-router": {
"version": "7.18.5",
"resolved": "https://registry.npmjs.org/react-router/-/react-router-7.18.5.tgz",
"integrity": "sha512-dTgvcXDOrLPrlLHrcZAqWveiK1hMSqN+rGAJzegP8HtmNkfRxOoshGWBNX3ZJ0cZjOHkRdlUJtaIhZMLLyGyPw==",
"version": "7.18.2",
"resolved": "https://registry.npmjs.org/react-router/-/react-router-7.18.2.tgz",
"integrity": "sha512-aUVMjFm3GAPTTZL7oYr5E7ETiqfQCHRLH+B+5afnICvf0r7kkK4eR6SMuwbSTJw/7t+12khT/Kahij49fqOCIg==",
"license": "MIT",
"dependencies": {
"cookie": "^1.0.1",
@@ -11077,12 +11077,12 @@
}
},
"node_modules/react-router-dom": {
"version": "7.18.5",
"resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-7.18.5.tgz",
"integrity": "sha512-9HcfDMU6JJ/fp6gJZYGwNE6RknVGS4kBjH1HkNw959mCJi70TVwdipQWv2FEol9uGpi+SZTFtVpJPtEQfayeOw==",
"version": "7.18.2",
"resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-7.18.2.tgz",
"integrity": "sha512-AIKJ/jgGlFb3EbfCXk5Gzshiwt+l3mqbCrNjmEWMMjqQxNJ3svBa6bgzFyCC2Sw3RA0VWF1kg3uQf2OFhxb8hw==",
"license": "MIT",
"dependencies": {
"react-router": "7.18.5"
"react-router": "7.18.2"
},
"engines": {
"node": ">=20.0.0"
@@ -12749,9 +12749,9 @@
}
},
"node_modules/ws": {
"version": "8.23.1",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.23.1.tgz",
"integrity": "sha512-1rzWDSN0NOi3tpwgHsQExjWRoKUGtjCkJ2gZRaaRtzhYLboMytOI9pj9PU7yCgY37auqdd4OyLofZDbkeuZmPA==",
"version": "8.21.1",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.21.1.tgz",
"integrity": "sha512-+0NTnW77fFN/DjQi6k/Sq/Yvk4Sgajw7urW8V+asjXnRgDs9gyGkdb7EzgfhA4goXsRIZKE28fzIXBHEzhuiWw==",
"dev": true,
"license": "MIT",
"engines": {
+1 -1
View File
@@ -25,7 +25,7 @@
"react": "^19.2.4",
"react-dom": "^19.2.4",
"react-grid-layout": "2.3.0",
"react-router-dom": "^7.18.5",
"react-router-dom": "^7.18.2",
"typescript": "^5.9.3",
"web-vitals": "^2.1.4"
},