Fix Critical: IndexSystem lock races and TrackRepository.Update silent-create
- Replace object lock with SemaphoreSlim(1,1) on both DirectoryIndexDirectory and VaultIndexDirectory; SaveIndexAsync now executes inside the semaphore so mutate+persist is atomic - ReloadIndexAsync acquires the semaphore before LoadIndexAsync so disk load and in-memory swap are atomic with respect to concurrent writes - HasIndexEntry and GetEntryMetadata converted to async Task with WaitAsync; MediaVault.GetEntryAsync call sites updated accordingly - TrackRepository.Update throws InvalidOperationException when Id not found instead of silently calling Create; service layer catches and wraps as fail result
This commit is contained in:
@@ -92,7 +92,7 @@ public class FileDatabaseTests
|
||||
// Assert
|
||||
var vault = _fileDatabase.GetVault(TestData.TestKeys.ImageVaultKey);
|
||||
Assert.That(vault, Is.Not.Null, "Vault should not be null");
|
||||
Assert.That(vault!.HasIndexEntry(TestData.TestKeys.TestImageEntry), Is.True,
|
||||
Assert.That(await vault!.HasIndexEntry(TestData.TestKeys.TestImageEntry), Is.True,
|
||||
"Added image should be in the index");
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ public class FileDatabaseTests
|
||||
Assert.That(reloadedDatabase.GetIndexSize(), Is.EqualTo(1), "Index count should be 1");
|
||||
|
||||
// Verify vault exists
|
||||
Assert.That(reloadedDatabase.HasIndexEntry(TestData.TestKeys.ImageVaultKey), Is.True,
|
||||
Assert.That(await reloadedDatabase.HasIndexEntry(TestData.TestKeys.ImageVaultKey), Is.True,
|
||||
"Vault should be present in index");
|
||||
Assert.That(reloadedDatabase.HasVault(TestData.TestKeys.ImageVaultKey), Is.True,
|
||||
"Vault should be present in vault collection");
|
||||
|
||||
@@ -492,13 +492,13 @@ public class IndexSystemTests
|
||||
await database!.CreateVaultAsync(testVaultKey, MediaVaultType.Image);
|
||||
|
||||
// Assert
|
||||
Assert.That(database.HasIndexEntry(testVaultKey), Is.True, "Should contain added entry");
|
||||
Assert.That(await database.HasIndexEntry(testVaultKey), Is.True, "Should contain added entry");
|
||||
Assert.That(database.GetIndexSize(), Is.EqualTo(1), "Should have one entry");
|
||||
Assert.That(File.Exists(IndexPath), Is.True, "Should persist to file");
|
||||
|
||||
// Verify persistence by reloading database
|
||||
var reloadedDatabase = await FileDatabase.FromAsync(TestDirectory);
|
||||
Assert.That(reloadedDatabase!.HasIndexEntry(testVaultKey), Is.True, "Should persist entry across restarts");
|
||||
Assert.That(await reloadedDatabase!.HasIndexEntry(testVaultKey), Is.True, "Should persist entry across restarts");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -516,13 +516,13 @@ public class IndexSystemTests
|
||||
await vault!.AddEntryAsync(testKey, testImage);
|
||||
|
||||
// Assert
|
||||
Assert.That(vault.HasIndexEntry(testKey), Is.True, "Should contain added entry");
|
||||
Assert.That(await vault.HasIndexEntry(testKey), Is.True, "Should contain added entry");
|
||||
Assert.That(vault.GetIndexSize(), Is.EqualTo(1), "Should have one entry");
|
||||
Assert.That(File.Exists(IndexPath), Is.True, "Should persist to file");
|
||||
|
||||
// Verify persistence by reloading vault
|
||||
var reloadedVault = await ImageVault.FromAsync(TestDirectory);
|
||||
Assert.That(reloadedVault!.HasIndexEntry(testKey), Is.True, "Should persist entry across restarts");
|
||||
Assert.That(await reloadedVault!.HasIndexEntry(testKey), Is.True, "Should persist entry across restarts");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ public class MediaVaultTests
|
||||
await _imageVault.AddEntryAsync(entryKey, imageBinary);
|
||||
|
||||
// Assert
|
||||
Assert.That(_imageVault.HasIndexEntry(entryKey), Is.True, "Should add to index");
|
||||
Assert.That(await _imageVault.HasIndexEntry(entryKey), Is.True, "Should add to index");
|
||||
|
||||
var expectedFilePath = Path.Combine(TestDirectory, "test-image.png");
|
||||
AssertMediaFileExists(expectedFilePath, imageBinary.Buffer);
|
||||
@@ -148,7 +148,7 @@ public class MediaVaultTests
|
||||
|
||||
foreach (var (key, binary) in entries)
|
||||
{
|
||||
Assert.That(_imageVault.HasIndexEntry(key), Is.True, $"Should contain {key} in index");
|
||||
Assert.That(await _imageVault.HasIndexEntry(key), Is.True, $"Should contain {key} in index");
|
||||
var expectedFilePath = Path.Combine(TestDirectory, $"{key}.png");
|
||||
AssertMediaFileExists(expectedFilePath, binary.Buffer);
|
||||
}
|
||||
@@ -264,7 +264,7 @@ public class MediaVaultTests
|
||||
await _audioVault.AddEntryAsync(entryKey, audioBinary);
|
||||
|
||||
// Assert
|
||||
Assert.That(_audioVault.HasIndexEntry(entryKey), Is.True, "Should add to index");
|
||||
Assert.That(await _audioVault.HasIndexEntry(entryKey), Is.True, "Should add to index");
|
||||
|
||||
var expectedFilePath = Path.Combine(TestDirectory, "test-audio.mp3");
|
||||
AssertMediaFileExists(expectedFilePath, audioBinary.Buffer);
|
||||
@@ -337,7 +337,7 @@ public class MediaVaultTests
|
||||
return (string)method!.Invoke(_vault, new object[] { mediaKey })!;
|
||||
}
|
||||
|
||||
public bool HasIndexEntry(string entryId) => _vault.HasIndexEntry(entryId);
|
||||
public Task<bool> HasIndexEntry(string entryId) => _vault.HasIndexEntry(entryId);
|
||||
public Task AddEntryAsync(string entryId, FileBinary media) =>
|
||||
_vault.AddEntryAsync(entryId, media);
|
||||
}
|
||||
@@ -423,7 +423,7 @@ public class MediaVaultTests
|
||||
await vault!.AddEntryAsync(entryKey, imageBinary);
|
||||
|
||||
// Assert
|
||||
Assert.That(vault.HasIndexEntry(entryKey), Is.True, "Should add entry to index");
|
||||
Assert.That(await vault.HasIndexEntry(entryKey), Is.True, "Should add entry to index");
|
||||
|
||||
var expectedFilePath = Path.Combine(TestDirectory, "test-media.png");
|
||||
AssertMediaFileExists(expectedFilePath, imageBinary.Buffer);
|
||||
|
||||
Reference in New Issue
Block a user