public async Task DisposeTryDecrementAsync()
        {         //****************************************
            var MyLock = new AsyncCounter();

            //****************************************

            _ = MyLock.DisposeAsync();

            Assert.IsFalse(await MyLock.TryDecrementAsync());

            //****************************************

            Assert.AreEqual(0, MyLock.WaitingCount, "Waiter still registered");
            Assert.AreEqual(0, MyLock.CurrentCount, "Count not zero");
        }
示例#2
0
        /// <summary>
        /// Attempts to take an Item to the collection
        /// </summary>
        /// <param name="timeout">The amount of time to wait for an item to arrive in the collection</param>
        /// <param name="token">A cancellation token to abort the take operation</param>
        /// <returns>A task returning the result of the take operation</returns>
        /// <exception cref="OperationCanceledException">The cancellation token was raised while we were waiting for an item</exception>
        /// <exception cref="InvalidOperationException">The collection was completed and emptied while we were waiting for an item</exception>
        /// <exception cref="TimeoutException">The timeout elapsed</exception>
        public ValueTask <T> Take(TimeSpan timeout, CancellationToken token = default)
        {
            // Is there an item to take?
            var TakeItem = _UsedSlots.TryDecrementAsync(timeout, token);

            if (TakeItem.IsCompleted)
            {
                if (TakeItem.Result)
                {
                    return(new ValueTask <T>(CompleteTake()));
                }

                throw new InvalidOperationException("Collection is empty and adding has been completed");
            }

            // No, wait for one to arrive
            var Instance = CollectionTakeInstance.GetOrCreateFor(this, TakeItem);

            return(new ValueTask <T>(Instance, Instance.Version));
        }