Path: redb/redb.Tests.Integration/Tests/Base/LockForUpdateTestsBase.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Base/LockForUpdateTestsBase.cs ---- using redb.Core; using redb.Core.Exceptions; using redb.Core.Models.Entities; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// BR-9 (Tsak report, fixed 2026-09-02): LockForUpdateAsync on a deleted id used to be a silent /// no-op - zero rows locked, no signal - and with the default MissingObjectStrategy.AutoSwitchToInsert /// a CAS pattern (lock -> re-read -> save) could run on an unlocked row or resurrect a deleted /// object. The lenient form now reports how many of the requested rows exist (and are locked); the /// Required form throws naming the missing ids and refuses to run outside a transaction. /// public abstract class LockForUpdateTestsBase { protected readonly IRedbService Redb; protected LockForUpdateTestsBase(IRedbService redb) => Redb = redb; private async Task NewObjectAsync() { await Redb.SyncSchemeAsync(); return await Redb.SaveAsync(new RedbObject { name = "lock-probe", Props = new SimpleProps { Title = "lock" } }); } [Fact] public async Task LockForUpdate_ReportsHowManyRequestedRowsExist() { var id = await NewObjectAsync(); await Redb.Context.ExecuteAtomicAsync(async () => { (await Redb.LockForUpdateAsync(id)).Should().Be(1); (await Redb.LockForUpdateAsync(id, -987654321L)).Should().Be(1, "a deleted or never-created id locks nothing - the count is the caller's signal"); (await Redb.LockForUpdateAsync(id, id)).Should().Be(1, "the count is of DISTINCT existing ids, not of the argument list"); (await Redb.LockForUpdateAsync()).Should().Be(0); }); } [Fact] public async Task LockForUpdateRequired_NamesTheMissingIds() { var id = await NewObjectAsync(); await Redb.Context.ExecuteAtomicAsync(async () => { await Redb.LockForUpdateRequiredAsync(id); // every id exists - no throw var act = async () => await Redb.LockForUpdateRequiredAsync(id, -987654321L, -987654322L); (await act.Should().ThrowAsync( "a CAS must not proceed on an unlocked row")) .Which.MissingIds.Should().Equal(-987654322L, -987654321L); }); } [Fact] public async Task LockForUpdateRequired_RefusesToRunOutsideATransaction() { var id = await NewObjectAsync(); var act = async () => await Redb.LockForUpdateRequiredAsync(id); await act.Should().ThrowAsync( "a lock outside a transaction releases immediately and protects nothing"); } }