예제 #1
0
            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));
            }
예제 #2
0
            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));
            }
예제 #3
0
            public void CanDisposeIfLockAquired()
            {
                var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid());

                sagaLock.Aquire();

                sagaLock.Dispose();
            }
예제 #4
0
            public void CanAquireLockIfNoLockAquired()
            {
                using (var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid()))
                {
                    sagaLock.Aquire();

                    Assert.True(sagaLock.Aquired);
                }
            }
예제 #5
0
 public void CanDisposeMoreThanOnce()
 {
     using (var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid()))
     {
         sagaLock.Aquire();
         sagaLock.Dispose();
         sagaLock.Dispose();
     }
 }
예제 #6
0
            public void CanAquireLockIfNoLockAquired()
            {
                using (var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid()))
                {
                    sagaLock.Aquire();

                    Assert.True(sagaLock.Aquired);
                }
            }
예제 #7
0
            public void CanReleaseLockIfLockAquired()
            {
                using (var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid()))
                {
                    sagaLock.Aquire();
                    sagaLock.Release();

                    Assert.False(sagaLock.Aquired);
                }
            }
예제 #8
0
        /// <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);
        }
예제 #9
0
            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);
                }
            }
예제 #10
0
            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);
                }
            }
예제 #11
0
        Task <IAsyncDisposable> Lock(TSaga instance, CancellationToken cancellationToken)
        {
            var sagaLock = new SagaLock(_database, _options, instance.CorrelationId);

            return(sagaLock.Lock(cancellationToken));
        }
예제 #12
0
 public void CanDisposeMoreThanOnce()
 {
     using (var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid()))
     {
         sagaLock.Aquire();
         sagaLock.Dispose();
         sagaLock.Dispose();
     }
 }
예제 #13
0
            public void CanDisposeIfLockNotAquired()
            {
                var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid());

                sagaLock.Dispose();
            }
예제 #14
0
            public void CanReleaseLockIfLockAquired()
            {
                using (var sagaLock = new SagaLock(typeof(Saga), GuidStrategy.NewGuid()))
                {
                    sagaLock.Aquire();
                    sagaLock.Release();

                    Assert.False(sagaLock.Aquired);
                }
            }