Exemplo n.º 1
0
        public async Task AcquireManyEnumerableSubClassAsync()
        {
            var lockBase = new LockBase();

            _col.Add(lockBase);
            var dataStore = new SharpLockInMemoryStringIdDataStore <LockBase, InnerLock>(_col, _logger, TimeSpan.FromSeconds(30));

            var locks = Enumerable.Range(0, 100).Select(x => new DistributedLock <LockBase, InnerLock, string>(dataStore, y => y.EnumerableLockables)).ToList();

            _logger.LogInformation(locks.Count.ToString());
            var lockedObjects = await Task.WhenAll(locks.Select(x => x.AcquireLockAsync(lockBase, lockBase.EnumerableLockables.First(), TimeSpan.FromSeconds(1))));

            Assert.IsFalse(lockedObjects.Count(x => x != null) < 1, "Failed to acquire lock.");
            Assert.IsFalse(lockedObjects.Count(x => x != null) > 1, "Acquired multiple locks.");

            var lockStates = await Task.WhenAll(locks.Select(x => x.RefreshLockAsync()));

            Assert.IsFalse(lockStates.Count(x => x) < 1, "Failed to refresh lock.");
            Assert.IsFalse(lockStates.Count(x => x) > 1, "Acquired multiple locks.");

            lockStates = await Task.WhenAll(locks.Select(x => x.ReleaseLockAsync()));

            Assert.IsTrue(lockStates.Count(x => x) == locks.Count, "Failed to release lock.");
            Assert.IsTrue(locks.Count(x => x.LockAcquired) == 0, "Failed to release lock.");

            await Task.WhenAll(locks.Select(async x => await x.DisposeAsync().ConfigureAwait(false)));

            Assert.IsTrue(locks.Count(x => x.Disposed) == locks.Count, "Failed to mark object as disposed");
        }
Exemplo n.º 2
0
        public async Task GetObjectSubClassAsync()
        {
            var lockBase = new LockBase();

            _col.Add(lockBase);
            var dataStore = new SharpLockInMemoryStringIdDataStore <LockBase, InnerLock>(_col, _logger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, InnerLock, string>(dataStore, x => x.SingularInnerLock);

            Assert.IsTrue(await lck.GetObjectAsync() == null, "await lck.GetObjectAsync() == null");

            await Assert.ThrowsExceptionAsync <DistributedLockException>(() => lck.GetObjectAsync(true), "() => lck.GetObjectAsync(true)");

            Assert.IsNotNull(await lck.AcquireLockAsync(lockBase, lockBase.SingularInnerLock), "await lck.AcquireLockAsync(lockBase, lockBase.SingularInnerLock)");

            Assert.IsTrue(lck.LockAcquired, "Lock should be acquired but it doesn't appear to be.");

            Assert.AreEqual(lck.ToString(), $"LockId: {lck.LockedObjectLockId}, Locked ObjectId: {lck.LockedObjectId}.");

            Assert.IsTrue(await lck.ReleaseLockAsync(), "await lck.ReleaseLockAsync()");

            Assert.AreEqual(lck.ToString(), "No lock acquired.");

            await lck.DisposeAsync().ConfigureAwait(false);

            Assert.IsTrue(lck.Disposed, "Failed to mark object as disposed");
        }
Exemplo n.º 3
0
        public async Task AcquireOneBaseClassAfterLossAsync()
        {
            var lockBase = new LockBase();

            _col.Add(lockBase);
            var dataStore = new SharpLockInMemoryStringIdDataStore <LockBase>(_col, _logger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, string>(dataStore, 2);

            // Acquire the lock
            Assert.IsTrue(await lck.AcquireLockAsync(lockBase) != null, "Failed to acquire lock.");
            Assert.IsTrue(lockBase.Id == lck.LockedObjectId, "Locked Object is not the expected object.");
            Assert.IsTrue(lck.LockAcquired, "Lock should be acquired but it doesn't appear to be.");
            Assert.IsTrue(await lck.RefreshLockAsync(), "Failed to refresh lock.");
            await Task.Delay(5000);

            // Don't bother releasing it, attempt to re-acquire.
            lck = new DistributedLock <LockBase, string>(dataStore, 2);
            Assert.IsTrue(await lck.AcquireLockAsync(lockBase) != null, "Failed to acquire lock.");
            Assert.IsTrue(lockBase.Id == lck.LockedObjectId, "Locked Object is not the expected object.");
            Assert.IsTrue(lck.LockAcquired, "Lock should be acquired but it doesn't appear to be.");
            Assert.IsTrue(await lck.RefreshLockAsync(), "Failed to refresh lock.");
            Assert.IsTrue(await lck.ReleaseLockAsync(), "Failed to release lock.");

            await lck.DisposeAsync().ConfigureAwait(false);

            Assert.IsTrue(lck.Disposed, "Failed to mark object as disposed");
        }
Exemplo n.º 4
0
        public async Task DisposeSubClassAsync()
        {
            var lockBase = new LockBase();

            _col.Add(lockBase);
            var dataStore = new SharpLockInMemoryStringIdDataStore <LockBase>(_col, _logger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, string>(dataStore);

            Assert.IsTrue(await lck.AcquireLockAsync(lockBase) != null, "await lck.AcquireLockAsync(lockBase, lockBase.SingularInnerLock)");

            Assert.IsTrue(lck.LockAcquired, "Lock should be acquired but it doesn't appear to be.");

            await lck.DisposeAsync().ConfigureAwait(false);

            Assert.IsTrue(lck.Disposed, "Failed to mark object as disposed");
        }
Exemplo n.º 5
0
        public async Task AcquireOneEnumerableSubClassAsync()
        {
            var lockBase = new LockBase();

            _col.Add(lockBase);
            var dataStore = new SharpLockInMemoryStringIdDataStore <LockBase, InnerLock>(_col, _logger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, InnerLock, string>(dataStore, x => x.EnumerableLockables);

            Assert.IsNotNull(await lck.AcquireLockAsync(lockBase, lockBase.EnumerableLockables.First()), "Failed to acquire lock.");

            Assert.IsTrue(lck.LockAcquired, "Lock should be acquired but it doesn't appear to be.");

            Assert.IsTrue(await lck.RefreshLockAsync(), "Failed to refresh lock.");

            Assert.IsTrue(await lck.ReleaseLockAsync(), "Failed to release lock.");

            await lck.DisposeAsync().ConfigureAwait(false);

            Assert.IsTrue(lck.Disposed, "Failed to mark object as disposed");
        }
Exemplo n.º 6
0
        public async Task ToStringBaseClassAsync()
        {
            var lockBase = new LockBase();

            _col.Add(lockBase);
            var dataStore = new SharpLockInMemoryStringIdDataStore <LockBase>(_col, _logger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, string>(dataStore);

            Assert.IsNotNull(await lck.AcquireLockAsync(lockBase), "Failed to acquire lock.");

            Assert.IsTrue(lck.LockAcquired, "Lock should be acquired but it doesn't appear to be.");

            Assert.AreEqual(lck.ToString(), $"LockId: {lck.LockedObjectLockId}, Locked ObjectId: {lck.LockedObjectId}.");

            Assert.IsTrue(await lck.ReleaseLockAsync(), "await lck.ReleaseLockAsync()");

            Assert.AreEqual(lck.ToString(), "No lock acquired.");

            await lck.DisposeAsync().ConfigureAwait(false);

            Assert.IsTrue(lck.Disposed, "Failed to mark object as disposed");
        }
Exemplo n.º 7
0
        public async Task RefreshAlreadyReleasedBaseClassAsync()
        {
            var lockBase = new LockBase();

            _col.Add(lockBase);
            var dataStore = new SharpLockInMemoryStringIdDataStore <LockBase>(_col, _logger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, string>(dataStore);

            Assert.IsNotNull(await lck.AcquireLockAsync(lockBase), "Failed to acquire lock.");

            Assert.IsTrue(lck.LockAcquired, "Lock should be acquired but it doesn't appear to be.");

            Assert.IsTrue(await lck.ReleaseLockAsync(), "await lck.ReleaseLockAsync()");

            Assert.IsFalse(await lck.RefreshLockAsync(), "await lck.RefreshLockAsync()");

            await Assert.ThrowsExceptionAsync <RefreshDistributedLockException>(async() => await lck.RefreshLockAsync(true), "async () => await lck.RefreshLockAsync()");

            Assert.AreEqual(lck.ToString(), "No lock acquired.");

            await lck.DisposeAsync().ConfigureAwait(false);

            Assert.IsTrue(lck.Disposed, "Failed to mark object as disposed");
        }
Exemplo n.º 8
0
        public async Task AcquireOneBaseClassAndGetLockedObjectAsync()
        {
            var lockBase = new LockBase();

            _col.Add(lockBase);
            var dataStore = new SharpLockInMemoryStringIdDataStore <LockBase>(_col, _logger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, string>(dataStore);

            Assert.IsTrue(await lck.AcquireLockAsync(lockBase) != null, "Failed to acquire lock.");

            Assert.IsTrue(lockBase.Id == lck.LockedObjectId, "Locked Object is not the expected object.");

            Assert.IsTrue(lck.LockAcquired, "Lock should be acquired but it doesn't appear to be.");

            Assert.IsNotNull(await lck.GetObjectAsync(), "Failed to get a copy of the locked object.");

            Assert.IsTrue(await lck.RefreshLockAsync(), "Failed to refresh lock.");

            Assert.IsTrue(await lck.ReleaseLockAsync(), "Failed to release lock.");

            await lck.DisposeAsync().ConfigureAwait(false);

            Assert.IsTrue(lck.Disposed, "Failed to mark object as disposed");
        }