public async Task Calling_Dispose_Should_Release_Read_Write_Lock() { AsyncReadWriteLock asyncLock = new AsyncReadWriteLock(); var isReadLockReleasedByPreviousCode = false; using (await asyncLock.ReadLockAsync()) { Thread.Sleep(100); } using (await asyncLock.ReadLockAsync()) { isReadLockReleasedByPreviousCode = true; } var isWriteLockReleasedByPreviousCode = false; using (await asyncLock.WriteLockAsync()) { Thread.Sleep(100); } using (await asyncLock.WriteLockAsync()) { isWriteLockReleasedByPreviousCode = true; } Assert.IsTrue(isReadLockReleasedByPreviousCode); Assert.IsTrue(isWriteLockReleasedByPreviousCode); }
public async Task <FirehoseClient> CreateAsync(FirehoseSettings settings) { using (await Lock.ReadLockAsync()) { FirehoseClient client; if (Cache.TryGetValue(settings.Signature, out client) && client.IsValid()) { return(client); } } using (await Lock.WriteLockAsync()) { FirehoseClient client; if (Cache.TryGetValue(settings.Signature, out client) && client.IsValid()) { return(client); } if (client != null && client.IsValid() == false) { client.Dispose(); Cache.Remove(settings.Signature); } var newClient = await CreateNewAsync(settings); Cache[settings.Signature] = newClient; return(newClient); } }
public async Task Should_Be_Able_To_Acquire_Read_And_Write_Lock_In_Async_Code() { AsyncReadWriteLock asyncLock = new AsyncReadWriteLock(); var isReadLockAcquired = false; var isWriteLockAcquired = false; using (await asyncLock.ReadLockAsync()) { isReadLockAcquired = true; } using (await asyncLock.WriteLockAsync()) { isWriteLockAcquired = true; } Assert.IsTrue(isReadLockAcquired); Assert.IsTrue(isWriteLockAcquired); }