Beispiel #1
0
        public void TryAdd_IfFull_Failed()
        {
            var queue      = new ConcurrentQueue <int>(new[] { 1, 2, 3 });
            var collection = new AsyncBlockingCollection <int>(queue, queue.Count);

            Assert.IsFalse(collection.TryAdd(1));
        }
Beispiel #2
0
        public void TryTake_InCaseOfAdequate_OK()
        {
            var queue      = new ConcurrentQueue <int>(new[] { 1, 2, 3 });
            var collection = new AsyncBlockingCollection <int>(queue, queue.Count);

            Assert.IsTrue(collection.TryTake(out _));
        }
Beispiel #3
0
        public async Task AddAsync_IfNotFull_OK()
        {
            var collection = new AsyncBlockingCollection <int>(1);
            await collection.AddAsync(1);

            Assert.Pass();
        }
Beispiel #4
0
        static async Task Main(string[] args)
        {
            Random r = new Random();

            using (var collection = new AsyncBlockingCollection <int>())
            {
                var addTask = Task.Run(async() =>
                {
                    foreach (var item in Enumerable.Range(1, 20))
                    {
                        await Task.Delay(r.Next(2000));
                        Console.WriteLine("Add:" + item);
                        collection.Add(item);
                    }
                    collection.CompleteAdding();
                });

                var takeTask1 = collection.TakeListAsync(2, results =>
                {
                    Console.WriteLine("Take1:" + string.Join(",", results.Select(r => r.ToString())));
                    return(Task.CompletedTask);
                });

                var takeTask2 = collection.TakeListAsync(3, results =>
                {
                    Console.WriteLine("Take2:" + string.Join(",", results.Select(r => r.ToString())));
                    return(Task.CompletedTask);
                });

                await Task.WhenAll(addTask, takeTask1, takeTask2);
            }
        }
Beispiel #5
0
        public async Task TakeAsync_InCaseOfAdequate_OK()
        {
            var queue      = new ConcurrentQueue <int>(new[] { 1, 2, 3 });
            var collection = new AsyncBlockingCollection <int>(queue, queue.Count);
            await collection.TakeAsync();

            Assert.Pass();
        }
Beispiel #6
0
        public void TakeAsync_InCaseOfEmpty_Timeout()
        {
            var collection = new AsyncBlockingCollection <int>();

            using var tokenSrc = new CancellationTokenSource(1000);
            Assert.CatchAsync <OperationCanceledException>(async() => await collection.TakeAsync(tokenSrc.Token));
            Assert.Pass();
        }
Beispiel #7
0
        public void AddAsync_IfFull_Blocked()
        {
            var queue      = new ConcurrentQueue <int>(new[] { 1, 2, 3 });
            var collection = new AsyncBlockingCollection <int>(queue, queue.Count);

            using var tokenSrc = new CancellationTokenSource(1000);
            Assert.CatchAsync <OperationCanceledException>(async() => await collection.AddAsync(0, tokenSrc.Token));
            Assert.Pass();
        }
        /// <summary>
        /// 初始化消息队列
        /// </summary>
        /// <param name="capacity">队列最大容量, 当队列满载时, 新消息的插入将导致旧消息被丢弃</param>
        /// <param name="behaviour"></param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/>小于1</exception>
        public MessageQueue(int capacity, QueueFullBehaviour behaviour)
        {
            if (capacity < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(capacity), capacity, "Should be greater than 0");
            }

            m_messageCollection = new AsyncBlockingCollection <MessageArgs <TMessage> >(capacity);
            FullBehaviour       = behaviour;
        }
Beispiel #9
0
        public async Task GetConsumingEnumerableTest()
        {
            var queue      = new ConcurrentQueue <int>(new[] { 1, 2, 3 });
            var collection = new AsyncBlockingCollection <int>(queue, queue.Count);
            var enumerable = collection.GetConsumingEnumerable();

            using var tokenSrc         = new CancellationTokenSource(1000);
            await using var enumerator = enumerable.GetAsyncEnumerator(tokenSrc.Token);

            for (int i = 0; i < 3; i++)
            {
                await enumerator.MoveNextAsync();
            }

            Assert.CatchAsync <OperationCanceledException>(async() => await enumerator.MoveNextAsync());
        }
Beispiel #10
0
        public async Task TestTakeListAsync()
        {
            var bag = new ConcurrentBag <int>();

            using (var collection = new AsyncBlockingCollection <int>())
            {
                var addTask = Task.Run(async() =>
                {
                    Random r = new Random();
                    foreach (var item in Enumerable.Range(1, 20))
                    {
                        await Task.Delay(r.Next(100));
                        collection.Add(item);
                    }
                    collection.CompleteAdding();
                });

                var takeTask1 = collection.TakeListAsync(2, results =>
                {
                    foreach (var item in results)
                    {
                        bag.Add(item);
                    }
                    return(Task.CompletedTask);
                });

                var takeTask2 = collection.TakeListAsync(3, results =>
                {
                    foreach (var item in results)
                    {
                        bag.Add(item);
                    }
                    return(Task.CompletedTask);
                });

                await Task.WhenAll(addTask, takeTask1, takeTask2);

                Assert.True(Enumerable.Range(1, 20).SequenceEqual(bag.OrderBy(o => o)));
            }
        }
 /// <summary>
 ///     Returns the specified queue if it already exists but does not create one if it does not.
 /// </summary>
 /// <returns>True if the queue exists; false otherwise.</returns>
 /// <remarks>
 ///     Senders should use this so that we don't fill up in-memory queues with messages for which there are no handlers.
 ///     Receivers should use GetOrCreateMessageQueue.
 /// </remarks>
 public bool TryGetExistingMessageQueue(string path, out AsyncBlockingCollection<NimbusMessage> queue)
 {
     return _messageQueues.TryGetValue(path, out queue);
 }
Beispiel #12
0
        public void TryAdd_IfNotFull_OK()
        {
            var collection = new AsyncBlockingCollection <int>(1);

            Assert.IsTrue(collection.TryAdd(1));
        }
Beispiel #13
0
        public void AsyncBlockingCollectionTest_WithNoBoundarySetting_MaxCapacity()
        {
            var collection = new AsyncBlockingCollection <int>();

            Assert.AreEqual(int.MaxValue, collection.BoundaryCapacity);
        }
Beispiel #14
0
        public void AsyncBlockingCollectionTest_WithDefaultCollection_InitiallyZeroCount()
        {
            var collection = new AsyncBlockingCollection <int>();

            Assert.AreEqual(0, collection.Count);
        }
Beispiel #15
0
        public void TryTake_InCaseOfEmpty_Failed()
        {
            var collection = new AsyncBlockingCollection <int>();

            Assert.IsFalse(collection.TryTake(out _));
        }
Beispiel #16
0
 /// <summary>
 ///     Returns the specified queue if it already exists but does not create one if it does not.
 /// </summary>
 /// <returns>True if the queue exists; false otherwise.</returns>
 /// <remarks>
 ///     Senders should use this so that we don't fill up in-memory queues with messages for which there are no handlers.
 ///     Receivers should use GetOrCreateMessageQueue.
 /// </remarks>
 public bool TryGetExistingMessageQueue(string path, out AsyncBlockingCollection <NimbusMessage> queue)
 {
     return(_messageQueues.TryGetValue(path, out queue));
 }