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); } }
/// <summary> /// Add a pessimistic lock to the database context /// </summary> /// <param name="correlationId"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task Lock(Guid correlationId, CancellationToken cancellationToken) { if (_lock != null) { throw new InvalidOperationException("The database context is already locked"); } var sagaLock = new SagaLock(_database, _options, correlationId); _lock = await sagaLock.Lock(cancellationToken).ConfigureAwait(false); }
public void CannotReleaseLockIfLockNotAquired() { var sagaId = GuidStrategy.NewGuid(); using (var sagaLock = new SagaLock(typeof(Saga), sagaId)) { var ex = Assert.Throws <InvalidOperationException>(() => sagaLock.Release()); Assert.Equal(Exceptions.SagaLockNotHeld.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); } }
Task <IAsyncDisposable> Lock(TSaga instance, CancellationToken cancellationToken) { var sagaLock = new SagaLock(_database, _options, instance.CorrelationId); return(sagaLock.Lock(cancellationToken)); }
public void CanDisposeIfLockNotAquired() { var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid()); sagaLock.Dispose(); }