2026-10-02 22:05:12 +08:00
|
|
|
using YKanBan.Storage;
|
|
|
|
|
using YKanBan.Storage.Workspace;
|
|
|
|
|
using YKanBan.Tests.TestUtilities;
|
|
|
|
|
|
|
|
|
|
namespace YKanBan.Tests.Storage.Workspace;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-10-04 09:58:09 +08:00
|
|
|
/// Tests for exclusive lock acquisition and release.
|
2026-10-02 22:05:12 +08:00
|
|
|
/// </summary>
|
|
|
|
|
[TestClass]
|
2026-10-04 10:22:02 +08:00
|
|
|
public class WorkspaceLockTests {
|
2026-10-02 22:05:12 +08:00
|
|
|
[TestMethod]
|
2026-10-04 10:22:02 +08:00
|
|
|
public void AcquireThenDisposeThenAcquireAgainSucceeds() {
|
2026-10-02 22:05:12 +08:00
|
|
|
using var directory = new TempDirectory();
|
|
|
|
|
WorkspaceInitializer.Initialize(directory.FullPath);
|
|
|
|
|
|
|
|
|
|
WorkspaceLock.Acquire(directory.FullPath).Dispose();
|
|
|
|
|
// Re-acquiring after release works: no stale lock residue survives.
|
|
|
|
|
WorkspaceLock.Acquire(directory.FullPath).Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2026-10-04 10:22:02 +08:00
|
|
|
public void SecondAcquireWhileHeldThrows() {
|
2026-10-02 22:05:12 +08:00
|
|
|
using var directory = new TempDirectory();
|
|
|
|
|
WorkspaceInitializer.Initialize(directory.FullPath);
|
|
|
|
|
|
|
|
|
|
using WorkspaceLock first = WorkspaceLock.Acquire(directory.FullPath);
|
|
|
|
|
WorkspaceLockException exception = Assert.ThrowsExactly<WorkspaceLockException>(
|
|
|
|
|
() => WorkspaceLock.Acquire(directory.FullPath));
|
|
|
|
|
Assert.AreEqual(WorkspacePaths.LockFile(directory.FullPath), exception.LockFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2026-10-04 10:22:02 +08:00
|
|
|
public void AcquireWithoutYkanbanThrowsNotInitialized() {
|
2026-10-02 22:05:12 +08:00
|
|
|
using var directory = new TempDirectory();
|
|
|
|
|
Assert.ThrowsExactly<WorkspaceNotInitializedException>(
|
|
|
|
|
() => WorkspaceLock.Acquire(directory.FullPath));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2026-10-04 10:22:02 +08:00
|
|
|
public void LockFileIsEmptyAfterRelease() {
|
2026-10-02 22:05:12 +08:00
|
|
|
using var directory = new TempDirectory();
|
|
|
|
|
WorkspaceInitializer.Initialize(directory.FullPath);
|
|
|
|
|
|
2026-10-04 10:22:02 +08:00
|
|
|
using (WorkspaceLock.Acquire(directory.FullPath)) {
|
2026-10-02 22:05:12 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-04 09:58:09 +08:00
|
|
|
// The lock file survives release but carries no content; only the OS handle matters.
|
|
|
|
|
Assert.AreEqual(0L, new FileInfo(WorkspacePaths.LockFile(directory.FullPath)).Length);
|
2026-10-02 22:05:12 +08:00
|
|
|
}
|
|
|
|
|
}
|