Beispiel #1
0
        public static async Task InvalidExits()
        {
            using var rwLock = new AsyncReaderWriterLock();
            Throws <SynchronizationLockException>(rwLock.ExitReadLock);
            Throws <SynchronizationLockException>(rwLock.ExitUpgradeableReadLock);
            Throws <SynchronizationLockException>(rwLock.ExitWriteLock);

            await rwLock.EnterReadLockAsync(TimeSpan.FromMilliseconds(10));

            Throws <SynchronizationLockException>(rwLock.ExitUpgradeableReadLock);
            Throws <SynchronizationLockException>(rwLock.ExitWriteLock);
            rwLock.ExitReadLock();

            await rwLock.EnterUpgradeableReadLockAsync(TimeSpan.FromMilliseconds(10));

            Throws <SynchronizationLockException>(rwLock.ExitReadLock);
            Throws <SynchronizationLockException>(rwLock.ExitWriteLock);
            rwLock.ExitUpgradeableReadLock();

            await rwLock.EnterWriteLockAsync(TimeSpan.FromMilliseconds(10));

            Throws <SynchronizationLockException>(rwLock.ExitReadLock);
            Throws <SynchronizationLockException>(rwLock.ExitUpgradeableReadLock);
            rwLock.ExitWriteLock();
        }
        public static void GracefulShutdown2()
        {
            using var @lock = new AsyncReaderWriterLock();
            True(@lock.TryEnterReadLock());
            var task = @lock.DisposeAsync();

            False(task.IsCompleted);
            var acquisition = @lock.EnterWriteLockAsync(CancellationToken.None);

            False(acquisition.IsCompleted);
            @lock.ExitReadLock();
            True(task.IsCompletedSuccessfully);
            True(acquisition.IsFaulted);
            Throws <ObjectDisposedException>(acquisition.GetAwaiter().GetResult);
        }
Beispiel #3
0
        public static async Task WriterToReaderChain()
        {
            using var are    = new AutoResetEvent(false);
            using var rwLock = new AsyncReaderWriterLock();
            await rwLock.EnterWriteLockAsync(DefaultTimeout);

            var task = new TaskCompletionSource <bool>();

            ThreadPool.QueueUserWorkItem(async state =>
            {
                False(await rwLock.TryEnterReadLockAsync(TimeSpan.FromMilliseconds(10)));
                True(ThreadPool.QueueUserWorkItem(static ev => ev.Set(), are, false));
                await rwLock.EnterReadLockAsync(DefaultTimeout);
                rwLock.ExitReadLock();
                task.SetResult(true);
            });