Пример #1
0
        public async Task GetObjectSubClassAsync()
        {
            var lockBase = new LockBase();
            await _col.InsertOneAsync(lockBase);

            var dataStore = new SharpLockMongoDataStore <LockBase, InnerLock>(_col, _logger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, InnerLock, ObjectId>(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");
        }
Пример #2
0
        public async Task AcquireManySingularSubClassAsync()
        {
            var lockBase = new LockBase();
            await _col.InsertOneAsync(lockBase);

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

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

            Log.Logger.Information(locks.Count.ToString());
            var lockedObjects = await Task.WhenAll(locks.Select(x => x.AcquireLockAsync(lockBase, lockBase.SingularInnerLock, 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");
        }
Пример #3
0
        public async Task AcquireOneBaseClassAfterLossAsync()
        {
            var lockBase = new LockBase();
            await _col.InsertOneAsync(lockBase);

            var dataStore = new SharpLockMongoDataStore <LockBase>(_col, _logger, TimeSpan.FromSeconds(5));
            var lck       = new DistributedLock <LockBase, ObjectId>(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, ObjectId>(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");
        }
        public async Task DisposeSubClassAsync()
        {
            var lockBase = new LockBase();
            await _col.InsertOneAsync(lockBase);

            var dataStore = new SharpLockMongoDataStore <LockBase>(_col, _sharpLockLogger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, ObjectId>(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.");

            lck.Dispose();
            Assert.IsTrue(lck.Disposed, "Failed to mark object as disposed");
        }
        public async Task AcquireOneEnumerableSubClassAsync()
        {
            var lockBase = new LockBase();
            await _col.InsertOneAsync(lockBase);

            var dataStore = new SharpLockMongoDataStore <LockBase, InnerLock>(_col, _sharpLockLogger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, InnerLock, ObjectId>(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.");

            lck.Dispose();
            Assert.IsTrue(lck.Disposed, "Failed to mark object as disposed");
        }
        public async Task ToStringSubClassAsync()
        {
            var lockBase = new LockBase();
            await _col.InsertOneAsync(lockBase);

            var dataStore = new SharpLockMongoDataStore <LockBase, InnerLock>(_col, _sharpLockLogger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, InnerLock, ObjectId>(dataStore, x => x.SingularInnerLock);

            Assert.IsNotNull(await lck.AcquireLockAsync(lockBase, lockBase.SingularInnerLock), "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.");

            lck.Dispose();
            Assert.IsTrue(lck.Disposed, "Failed to mark object as disposed");
        }
        public async Task AcquireOneBaseClassAsync()
        {
            var lockBase = new LockBase();
            await _col.InsertOneAsync(lockBase);

            var dataStore = new SharpLockMongoDataStore <LockBase>(_col, _sharpLockLogger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, ObjectId>(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.IsTrue(await lck.RefreshLockAsync(), "Failed to refresh lock.");

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

            lck.Dispose();
            Assert.IsTrue(lck.Disposed, "Failed to mark object as disposed");
        }
        public async Task AcquireOneSingularSubClassAndGetLockedObjectAsync()
        {
            var lockBase = new LockBase();
            await _col.InsertOneAsync(lockBase);

            var dataStore = new SharpLockMongoDataStore <LockBase, InnerLock>(_col, _sharpLockLogger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, InnerLock, ObjectId>(dataStore, x => x.SingularInnerLock);

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

            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.");

            lck.Dispose();
            Assert.IsTrue(lck.Disposed, "Failed to mark object as disposed");
        }
        public async Task RefreshAlreadyReleasedBaseClassAsync()
        {
            var lockBase = new LockBase();
            await _col.InsertOneAsync(lockBase);

            var dataStore = new SharpLockMongoDataStore <LockBase>(_col, _sharpLockLogger, TimeSpan.FromSeconds(30));
            var lck       = new DistributedLock <LockBase, ObjectId>(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.");

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