コード例 #1
0
 public void Unlocked_PermitsMultipleReaderLocks()
 {
     Test.Async(async () =>
     {
         var rwl = new AsyncReaderWriterLock();
         await rwl.ReaderLockAsync();
         await rwl.ReaderLockAsync();
     });
 }
コード例 #2
0
 public void WriteLocked_PreventsReaderLock()
 {
     Test.Async(async () =>
     {
         var rwl = new AsyncReaderWriterLock();
         await rwl.WriterLockAsync();
         var task = rwl.ReaderLockAsync().AsTask();
         await AssertEx.NeverCompletesAsync(task);
     });
 }
コード例 #3
0
 public void ReaderKey_MultiDispose_DoesNothing()
 {
     AsyncContext.Run(async () =>
     {
         var rwl = new AsyncReaderWriterLock();
         var key = await rwl.ReaderLockAsync();
         key.Dispose();
         key.Dispose();
         await rwl.ReaderLockAsync();
     });
 }
コード例 #4
0
        public void ReaderLocked_UpgradableReaderReleased_UpgradableReaderWaiting_ReleasesUpgrableReader()
        {
            Test.Async(async () =>
            {
                var rwl = new AsyncReaderWriterLock();
                Task upgradableReadLock;
                using (await rwl.ReaderLockAsync())
                using (await rwl.UpgradeableReaderLockAsync())
                {
                    upgradableReadLock = rwl.UpgradeableReaderLockAsync().AsTask();
                    Assert.IsFalse(upgradableReadLock.IsCompleted);
                }

                await upgradableReadLock;
            });
        }
コード例 #5
0
 public void UpgradeableKey_MultiUpgradeWhenFirstUpgradeIsIncomplete_ThrowsSynchronousException()
 {
     Test.Async(async () =>
     {
         var rwl = new AsyncReaderWriterLock();
         await rwl.ReaderLockAsync();
         var key = await rwl.UpgradeableReaderLockAsync();
         var _ = key.UpgradeAsync();
         AssertEx.ThrowsException<InvalidOperationException>(() => key.UpgradeAsync());
     });
 }
コード例 #6
0
        public void ReaderLocked_ReaderReleased_ReaderAndWriterWaiting_DoesNotReleaseReaderOrWriter()
        {
            Test.Async(async () =>
            {
                var rwl = new AsyncReaderWriterLock();
                Task readLock, writeLock;
                await rwl.ReaderLockAsync();
                using (await rwl.ReaderLockAsync())
                {
                    writeLock = rwl.WriterLockAsync().AsTask();
                    readLock = rwl.ReaderLockAsync().AsTask();
                }

                await TaskShim.WhenAll(AssertEx.NeverCompletesAsync(writeLock),
                    AssertEx.NeverCompletesAsync(readLock));
            });
        }
コード例 #7
0
        public void ReaderLocked_ReaderReleased_ReaderAndUpgradingReaderWaiting_DoesNotReleaseReaderOrUpgradingReader()
        {
            Test.Async(async () =>
            {
                var rwl = new AsyncReaderWriterLock();
                Task readLock, upgradingReadLock;
                await rwl.ReaderLockAsync();
                using (await rwl.ReaderLockAsync())
                {
                    var upgradeableReadLock = await rwl.UpgradeableReaderLockAsync();
                    upgradingReadLock = upgradeableReadLock.UpgradeAsync().AsTask();
                    readLock = rwl.ReaderLockAsync().AsTask();
                }

                await TaskShim.WhenAll(AssertEx.NeverCompletesAsync(upgradingReadLock),
                    AssertEx.NeverCompletesAsync(readLock));
            });
        }
コード例 #8
0
        public void LockReleased_WriteTakesPriorityOverRead()
        {
            Test.Async(async () =>
            {
                var rwl = new AsyncReaderWriterLock();
                Task writeLock, readLock;
                using (await rwl.WriterLockAsync())
                {
                    readLock = rwl.ReaderLockAsync().AsTask();
                    writeLock = rwl.WriterLockAsync().AsTask();
                }

                await writeLock;
                await AssertEx.NeverCompletesAsync(readLock);
            });
        }
コード例 #9
0
        public void LockReleased_AllowsUpgradeableReadAndMultipleReaders()
        {
            Test.Async(async () =>
            {
                var rwl = new AsyncReaderWriterLock();
                Task upgradeableReadLock, readLock1, readLock2;
                using (await rwl.WriterLockAsync())
                {
                    upgradeableReadLock = rwl.UpgradeableReaderLockAsync().AsTask();
                    readLock1 = rwl.ReaderLockAsync().AsTask();
                    readLock2 = rwl.ReaderLockAsync().AsTask();
                }

                await TaskShim.WhenAll(upgradeableReadLock, readLock1, readLock2);
            });
        }
コード例 #10
0
 public void ReadLocked_PreventsWriterLock()
 {
     AsyncContext.Run(async () =>
     {
         var rwl = new AsyncReaderWriterLock();
         await rwl.ReaderLockAsync();
         var task = rwl.WriterLockAsync();
         await AssertEx.NeverCompletesAsync(task);
     });
 }
コード例 #11
0
        public void WriteLocked_ReaderLockCancelled_DoesNotTakeLockWhenUnlocked()
        {
            Test.Async(async () =>
            {
                var rwl = new AsyncReaderWriterLock();
                using (await rwl.WriterLockAsync())
                {
                    var cts = new CancellationTokenSource();
                    var task = rwl.ReaderLockAsync(cts.Token).AsTask();
                    cts.Cancel();
                    await AssertEx.ThrowsExceptionAsync<OperationCanceledException>(() => task);
                }

                await rwl.ReaderLockAsync();
            });
        }
コード例 #12
0
        public void ReaderLock_PreCancelled_LockAvailable_SynchronouslyTakesLock()
        {
            var rwl = new AsyncReaderWriterLock();
            var token = new CancellationToken(true);

            var task = rwl.ReaderLockAsync(token).AsTask();

            Assert.IsTrue(task.IsCompleted);
            Assert.IsFalse(task.IsCanceled);
            Assert.IsFalse(task.IsFaulted);
        }
コード例 #13
0
        public void Upgrade_PreCancelled_LockNotAvailable_SynchronouslyCancels()
        {
            var rwl = new AsyncReaderWriterLock();
            var token = new CancellationToken(true);
            var key = rwl.UpgradeableReaderLockAsync().AsTask().Result;
            rwl.ReaderLockAsync();

            var task = key.UpgradeAsync(token).AsTask();

            Assert.IsTrue(task.IsCompleted);
            Assert.IsTrue(task.IsCanceled);
            Assert.IsFalse(task.IsFaulted);
        }
コード例 #14
0
 public void UpgradeableReadLocked_Downgrade_AllowsReaderLock()
 {
     Test.Async(async () =>
     {
         var rwl = new AsyncReaderWriterLock();
         var key = await rwl.UpgradeableReaderLockAsync();
         var upgradeKey = await key.UpgradeAsync();
         upgradeKey.Dispose();
         await rwl.ReaderLockAsync();
     });
 }
コード例 #15
0
        public void UpgradeableReadAndReadLocked_UpgradeAborted_CompletesAsCanceled()
        {
            Test.Async(async () =>
            {
                var rwl = new AsyncReaderWriterLock();
                var readLockTaken = new TaskCompletionSource();
                var task = TaskShim.Run(async () =>
                {
                    using (await rwl.ReaderLockAsync())
                    {
                        readLockTaken.SetResult();
                        await TaskConstants.Never;
                    }
                });
                await readLockTaken.Task;
                Task upgradeTask;
                using (var key = await rwl.UpgradeableReaderLockAsync())
                {
                    upgradeTask = key.UpgradeAsync().AsTask();
                    Assert.IsFalse(key.Upgraded);
                }

                await AssertEx.ThrowsExceptionAsync<OperationCanceledException>(upgradeTask);
            });
        }
コード例 #16
0
 public void UpgradeableReadAndReadLocked_Upgrade_WaitsForReaderLockToUnlock()
 {
     Test.Async(async () =>
     {
         var rwl = new AsyncReaderWriterLock();
         var readLockTaken = new TaskCompletionSource();
         var releaseReadLock = new TaskCompletionSource();
         var task = TaskShim.Run(async () =>
         {
             using (await rwl.ReaderLockAsync())
             {
                 readLockTaken.SetResult();
                 await releaseReadLock.Task;
             }
         });
         await readLockTaken.Task;
         using (var key = await rwl.UpgradeableReaderLockAsync())
         {
             Assert.IsFalse(key.Upgraded);
             var lockTask = key.UpgradeAsync().AsTask();
             Assert.IsFalse(lockTask.IsCompleted);
             releaseReadLock.SetResult();
             await lockTask;
             Assert.IsTrue(key.Upgraded);
         }
     });
 }
コード例 #17
0
 public void UpgradeableReadLocked_AllowsMultipleReaderLocks()
 {
     Test.Async(async () =>
     {
         var rwl = new AsyncReaderWriterLock();
         var key = await rwl.UpgradeableReaderLockAsync();
         Assert.IsFalse(key.Upgraded);
         await rwl.ReaderLockAsync();
         await rwl.ReaderLockAsync();
     });
 }
コード例 #18
0
 public void ReadLocked_AllowsUpgradeableReaderLock()
 {
     Test.Async(async () =>
     {
         var rwl = new AsyncReaderWriterLock();
         await rwl.ReaderLockAsync();
         await rwl.UpgradeableReaderLockAsync();
     });
 }