public void UnlockAll_WhenDoesNotHaveThreadedTransaction_ShouldStillUnlock( ALock aLock) { dbManager.GetThreadTransaction(true).Returns((DbTransaction)null); locker.Lock(new ILockable[] { aLock }); locker.UnlockAll(); exitObjects.Should().Equal(aLock.Lock); }
public void Do_Generic_ShouldReturnValueBlockReturns( ALock lck, DbTransaction transaction, string blockResult) { dbManager.GetThreadTransaction(Arg.Any <bool>()).Returns(transaction); var actualResult = locker.Lock(new[] { lck }).Do(() => blockResult); actualResult.Should().Be(blockResult); }
public void Do_Generic_CommitsTransactionAndUnlocksAll( ALock lck, DbTransaction transaction) { dbManager.GetThreadTransaction(Arg.Any <bool>()).Returns(transaction); locker.Lock(new[] { lck }).Do(() => "Hello"); transaction.Received(1).Dispose(); exitObjects.Should().Equal(lck.Lock); }
public void Do_Generic_WhenBlockThrowsException_ShouldNotUnlockAndShouldRollback( ALock lck, DbTransaction transaction) { dbManager.GetThreadTransaction(Arg.Any <bool>()).Returns(transaction); Action act = () => { locker.Lock(new[] { lck }).Do <string>(() => { throw new Exception("Test"); }); }; act.ShouldThrow <Exception>().WithMessage("Test"); transaction.Received(1).Rollback(); transaction.Received(1).Dispose(); exitObjects.Should().BeEmpty(); }
public void Lock_WhenCalledWithCollidingHashes_ShouldUseTypeNameToSort(ALock aLock, BLock bLock, CLock cLock) { aLock.Lock = new object(); bLock.Lock = new object(); cLock.Lock = new object(); aLock.Hash = 100; bLock.Hash = 100; cLock.Hash = 100; locker.Lock(new ILockable[] { aLock, cLock, bLock }); enterObjects.Should().Equal(aLock.Lock, bLock.Lock, cLock.Lock); }
public void UnlockAll_WhenCalledMultipleTimes_ShouldNotUnlockObjectsTwice(ALock aLock, BLock bLock, CLock cLock) { aLock.Lock = new object(); bLock.Lock = new object(); cLock.Lock = new object(); aLock.Hash = 100; bLock.Hash = 100; cLock.Hash = 100; locker.Lock(new ILockable[] { aLock, cLock, bLock }); locker.UnlockAll(); locker.UnlockAll(); exitObjects.Should().Equal(cLock.Lock, bLock.Lock, aLock.Lock); }
public void UnlockAll_WhenCalled_ShouldUnlockObjectsInReverseOrder(ALock aLock, BLock bLock, CLock cLock) { aLock.Lock = new object(); bLock.Lock = new object(); cLock.Lock = new object(); aLock.Hash = 100; bLock.Hash = 100; cLock.Hash = 100; locker.Lock(new ILockable[] { aLock, cLock, bLock }); exitObjects.Should().BeEmpty(); locker.UnlockAll(); exitObjects.Should().Equal(cLock.Lock, bLock.Lock, aLock.Lock); }
public void Lock_Integration(ALock aLock, BLock bLock, CLock cLock, ALock nullLock) { nullLock.Lock = null; nullLock.Hash = 0; aLock.Lock = new object(); aLock.Hash = 1; bLock.Lock = new object(); bLock.Hash = 2; cLock.Lock = new object(); cLock.Hash = 3; Monitor.Enter(cLock.Lock); bool locked = false; bool hasException = false; var thread = new Thread(() => { try { locker.Lock(new ILockable[] { cLock, nullLock, bLock, aLock }).Do(() => { locked = true; }); } catch (Exception) { hasException = true; } }); thread.Start(); Thread.Sleep(200); locked.Should().BeFalse(); enterObjects.Should().Equal(aLock.Lock, bLock.Lock); Monitor.Exit(cLock.Lock); thread.Join(1000); hasException.Should().BeFalse(); enterObjects.Should().Equal(aLock.Lock, bLock.Lock, cLock.Lock); exitObjects.Should().Equal(cLock.Lock, bLock.Lock, aLock.Lock); }
public void UnlockAll_WhenCalled_ShouldAllowToLockAgain(ALock aLock, BLock bLock, CLock cLock) { aLock.Lock = new object(); bLock.Lock = new object(); cLock.Lock = new object(); aLock.Hash = 100; bLock.Hash = 100; cLock.Hash = 100; locker.Lock(new ILockable[] { aLock, cLock, bLock }); locker.UnlockAll(); locker.Lock(new ILockable[] { aLock, cLock, bLock }); locker.UnlockAll(); enterObjects.Should().Equal(aLock.Lock, bLock.Lock, cLock.Lock, aLock.Lock, bLock.Lock, cLock.Lock); exitObjects.Should().Equal(cLock.Lock, bLock.Lock, aLock.Lock, cLock.Lock, bLock.Lock, aLock.Lock); }