Skip to content

Commit

Permalink
Tran test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dasatomic committed Dec 13, 2021
1 parent 87990ac commit 0c0c694
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion PageManager/ITransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface ITransaction : IAsyncDisposable, IDisposable
TransactionState GetTransactionState();
Task<Releaser> AcquireLock(ulong pageId, LockTypeEnum lockType);
Task<Releaser> AcquireLockWithCallerOwnership(ulong pageId, LockTypeEnum lockType);
void VerifyLock(ulong pageId, LockTypeEnum expectedLock);
bool VerifyLock(ulong pageId, LockTypeEnum expectedLock);
public bool AmIHoldingALock(ulong pageId, out LockTypeEnum lockType);
public void RegisterTempFolder(DirectoryInfo tempFolder);
}
Expand Down
4 changes: 1 addition & 3 deletions PageManager/LogManager/NotLoggedTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public Task Rollback()

public ulong TranscationId() => 0;

public void VerifyLock(ulong pageId, LockTypeEnum expectedLock)
{
}
public bool VerifyLock(ulong pageId, LockTypeEnum expectedLock) => true;
}
}
11 changes: 8 additions & 3 deletions PageManager/LogManager/ReadonlyTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using PageManager.Exceptions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -111,20 +112,24 @@ public Task Rollback()

public ulong TranscationId() => this.transactionId;

public void VerifyLock(ulong pageId, LockTypeEnum expectedLock)
public bool VerifyLock(ulong pageId, LockTypeEnum expectedLock)
{
if (expectedLock == LockTypeEnum.Exclusive)
{
throw new TranNotHoldingLock();
Debug.Assert(false);
return false;
}

lock (lck)
{
if (!this.locksHeld.ContainsKey(this.lockManager.LockIdForPage(pageId)))
{
throw new TranNotHoldingLock();
Debug.Assert(false);
return false;
}
}

return true;
}

public bool AmIHoldingALock(ulong pageId, out LockTypeEnum lockType)
Expand Down
20 changes: 15 additions & 5 deletions PageManager/LogManager/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public bool AmIHoldingALock(ulong pageId, out LockTypeEnum lockType)
return false;
}

public void VerifyLock(ulong pageId, LockTypeEnum expectedLock)
public bool VerifyLock(ulong pageId, LockTypeEnum expectedLock)
{
int lockId = this.pageManager.GetLockManager().LockIdForPage(pageId);

Expand All @@ -246,15 +246,21 @@ public void VerifyLock(ulong pageId, LockTypeEnum expectedLock)
{
if ((int)lockHeld < (int)expectedLock)
{
System.Diagnostics.Debug.Assert(false);
throw new TranNotHoldingLock();
#if DEBUG
System.Diagnostics.Debug.Assert(AssertOnNoLock);
#endif
return false;
}
}
else
{
System.Diagnostics.Debug.Assert(false);
throw new TranNotHoldingLock();
#if DEBUG
System.Diagnostics.Debug.Assert(AssertOnNoLock);
#endif
return false;
}

return true;
}
}

Expand All @@ -267,5 +273,9 @@ public void RegisterTempFolder(DirectoryInfo tempFolder)
{
this.tempDirectoriesToCleanUp.Enqueue(tempFolder);
}

#if DEBUG
public bool AssertOnNoLock = true;
#endif
}
}
10 changes: 6 additions & 4 deletions tests/LogManagerTests/TranLockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,26 @@ public async Task LockCheck()
{
await using ITransaction tran1 = logManager.CreateTransaction(pageManager);
using var releaser = await tran1.AcquireLock(1, LockTypeEnum.Shared);
tran1.VerifyLock(1, LockTypeEnum.Shared);
Assert.IsTrue(tran1.VerifyLock(1, LockTypeEnum.Shared));
}

[Test]
public async Task LockCheckDowngrade()
{
await using ITransaction tran1 = logManager.CreateTransaction(pageManager);
using var releaser = await tran1.AcquireLock(1, LockTypeEnum.Exclusive);
tran1.VerifyLock(1, LockTypeEnum.Shared);
Assert.IsTrue(tran1.VerifyLock(1, LockTypeEnum.Shared));
}

#if DEBUG
[Test]
public async Task LockCheckUpgrade()
{
await using ITransaction tran1 = logManager.CreateTransaction(pageManager);
using var releaser = await tran1.AcquireLock(1, LockTypeEnum.Shared);
Assert.Throws<TranNotHoldingLock>(() => tran1.VerifyLock(1, LockTypeEnum.Exclusive));
Assert.IsFalse(tran1.VerifyLock(1, LockTypeEnum.Exclusive));
}
#endif

[Test]
public async Task LockNotReleased()
Expand All @@ -63,7 +65,7 @@ public async Task AcquireLoop()
for (int i = 0; i < 1000; i++)
{
using var releaser = await tran1.AcquireLock((ulong)i, LockTypeEnum.Shared);
tran1.VerifyLock((ulong)i, LockTypeEnum.Shared);
Assert.IsTrue(tran1.VerifyLock((ulong)i, LockTypeEnum.Shared));
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions tests/Test.Common/DummyTran.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ public ulong TranscationId()
return 42;
}

public void VerifyLock(ulong pageId, LockTypeEnum expectedLock)
{
}
public bool VerifyLock(ulong pageId, LockTypeEnum expectedLock) => true;
}
}

0 comments on commit 0c0c694

Please sign in to comment.