public void CannotAquireLockIfLockAlreadyAquired() { var sagaId = GuidStrategy.NewGuid(); using (var sagaLock = new SagaLock(typeof(Saga), sagaId)) { sagaLock.Aquire(); var ex = Assert.Throws<InvalidOperationException>(() => sagaLock.Aquire()); Assert.Equal(Exceptions.SagaLockAlreadyHeld.FormatWith(typeof(Saga), sagaId), ex.Message); } }
public void CannotAquireLockIfLockAlreadyAquired() { var sagaId = GuidStrategy.NewGuid(); using (var sagaLock = new SagaLock(typeof(Saga), sagaId)) { sagaLock.Aquire(); var ex = Assert.Throws <InvalidOperationException>(() => sagaLock.Aquire()); Assert.Equal(Exceptions.SagaLockAlreadyHeld.FormatWith(typeof(Saga), sagaId), ex.Message); } }
public void AquireWillNotBlockIfAnotherLockAlreadyAquiredOnAnotherSaga() { var firstLockAquired = new ManualResetEvent(initialState: false); var secondLockAquired = new ManualResetEvent(initialState: false); var blockedTime = TimeSpan.Zero; Task.Factory.StartNew(() => { firstLockAquired.WaitOne(); using (var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid())) { var timer = Stopwatch.StartNew(); sagaLock.Aquire(); timer.Stop(); blockedTime = timer.Elapsed; secondLockAquired.Set(); } }); using (var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid())) { sagaLock.Aquire(); firstLockAquired.Set(); Thread.Sleep(100); } secondLockAquired.WaitOne(); Assert.InRange(blockedTime, TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(50)); }
public void AquireWillBlockIfAnotherLockAlreadyAquiredOnSameSaga() { var correlationId = GuidStrategy.NewGuid(); var firstLockAquired = new ManualResetEvent(initialState: false); var secondLockAquired = new ManualResetEvent(initialState: false); var blockedTime = TimeSpan.Zero; Task.Factory.StartNew(() => { firstLockAquired.WaitOne(); using (var sagaLock = new SagaLock(typeof(Saga), correlationId)) { var timer = Stopwatch.StartNew(); sagaLock.Aquire(); timer.Stop(); blockedTime = timer.Elapsed; secondLockAquired.Set(); } }); using (var sagaLock = new SagaLock(typeof(Saga), correlationId)) { sagaLock.Aquire(); firstLockAquired.Set(); Thread.Sleep(100); } secondLockAquired.WaitOne(); Assert.InRange(blockedTime, TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(150)); }
public void CanDisposeIfLockAquired() { var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid()); sagaLock.Aquire(); sagaLock.Dispose(); }
public void CanAquireLockIfNoLockAquired() { using (var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid())) { sagaLock.Aquire(); Assert.True(sagaLock.Aquired); } }
public void CanDisposeMoreThanOnce() { using (var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid())) { sagaLock.Aquire(); sagaLock.Dispose(); sagaLock.Dispose(); } }
public void CanReleaseLockIfLockAquired() { using (var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid())) { sagaLock.Aquire(); sagaLock.Release(); Assert.False(sagaLock.Aquired); } }