/// <summary> /// Clears all of the items from a queue partition. /// </summary> private async Task ClearQueueAsync() { HttpQueueClient <Item> qc = new HttpQueueClient <Item>(new Uri(serviceUri), c_ListenerName); Assert.IsNotNull(qc); long queueCount = await qc.CountAsync().ConfigureAwait(false); long itemCount = await qc.CountAsync(QueueType.ItemQueue); // Visit each partition looking for items. for (long partition = 0; partition < qc.ServicePartitionCount; partition++) { // Get the items in this partition. IEnumerable <QueueItem <Item> > results = await qc.GetItemsAsync(partition).ConfigureAwait(false); foreach (QueueItem <Item> qi in results) { var deleteResults = await qc.DeleteItemAsync(qi.Key, CancellationToken.None).ConfigureAwait(false); } } int attempt = qc.ServicePartitionCount; // Get the count, there may be items hanging out in the queues, which won't be removed until a dequeue operation for that partition is attempted // and the corresponding item is not found in the list of items. while ((await qc.CountAsync().ConfigureAwait(false) > 0) && (attempt-- > 0)) { IEnumerable <QueueItem <Item> > results = await qc.DequeueAsync().ConfigureAwait(false); } }
/// <summary> /// Gets a set of items. /// </summary> private static void GetItems() { int temp; Console.WriteLine($"Enter the partition number to retrieve items from. Current value is {_itemPartition}: "); _itemPartition = int.TryParse(Console.ReadLine(), out temp) ? temp : _itemPartition; Console.WriteLine($"Enter the number of items to retrieve. Current value is {_itemCount}: "); _itemCount = int.TryParse(Console.ReadLine(), out temp) ? temp : _itemCount; Console.WriteLine($"Enter the number of items to skip. Current value is {_itemSkip}: "); _itemSkip = int.TryParse(Console.ReadLine(), out temp) ? temp : _itemSkip; HttpQueueClient <Item> qc = new HttpQueueClient <Item>(_serviceUri, c_ListenerName); var items = qc.GetItemsAsync(_itemPartition, _itemCount, _itemSkip).GetAwaiter().GetResult(); if (null != items) { foreach (var item in items) { Console.WriteLine(item.ToString()); } } }
/// <summary> /// Empties a partition of all items. /// </summary> private static async Task EmptyAsync() { int partition = 0, temp; Console.WriteLine($"Enter the partition number to retrieve items from. Current value is {partition}: "); partition = int.TryParse(Console.ReadLine(), out temp) ? temp : partition; HttpQueueClient <Item> qc = new HttpQueueClient <Item>(_serviceUri, c_ListenerName); int count = 0; do { var items = await qc.GetItemsAsync(_itemPartition, _itemCount, _itemSkip); foreach (var item in items) { count++; var i = await qc.DeleteItemAsync(item.Key, CancellationToken.None); } } while (count > 0); Console.WriteLine("Items removed from partition."); }
public async Task Component_Test() { Item i1 = CreateRandomInstance(); Item i2 = CreateRandomInstance(); Item i3 = CreateRandomInstance(); HttpQueueClient <Item> qc = new HttpQueueClient <Item>(new Uri(serviceUri), c_ListenerName); Assert.IsNotNull(qc); long allBase = await qc.CountAsync(QueueType.AllQueues).ConfigureAwait(false); long leaseBase = await qc.CountAsync(QueueType.LeaseQueue).ConfigureAwait(false); long expiredBase = await qc.CountAsync(QueueType.ExpiredQueue).ConfigureAwait(false); long itemsBase = await qc.CountAsync(QueueType.ItemQueue).ConfigureAwait(false); // Add an single item to the queue and validate the count has changed. await qc.EnqueueAsync(new[] { i1 }, QueueType.FirstQueue, TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(5), CancellationToken.None).ConfigureAwait(false); long count = await qc.CountAsync(QueueType.AllQueues).ConfigureAwait(false); Assert.AreEqual(1 + allBase, count); count = await qc.CountAsync(QueueType.ItemQueue).ConfigureAwait(false); Assert.AreEqual(1 + itemsBase, count); // Add a batch of items to the queue and validate the count has changed. await qc.EnqueueAsync(new[] { i1, i2, i3 }, QueueType.FirstQueue, TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(5), CancellationToken.None).ConfigureAwait(false); count = await qc.CountAsync(QueueType.AllQueues, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(4 + allBase, count); count = await qc.CountAsync(QueueType.ItemQueue, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(4 + itemsBase, count); // Enqueue a single item to the second queue and check the counts. await qc.EnqueueAsync(new[] { i1 }, 1, TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(5), CancellationToken.None).ConfigureAwait(false); count = await qc.CountAsync(QueueType.AllQueues).ConfigureAwait(false); Assert.AreEqual(5 + allBase, count); count = await qc.CountAsync(QueueType.ItemQueue, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(5 + itemsBase, count); // Dequeue a single item from the first queue and check the counts. IEnumerable <QueueItem <Item> > items = await qc.DequeueAsync(1, QueueType.FirstQueue, QueueType.LastQueue, CancellationToken.None).ConfigureAwait(false); count = await qc.CountAsync(QueueType.AllQueues, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(4 + allBase, count); count = await qc.CountAsync(QueueType.LeaseQueue, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(1 + leaseBase, count); count = await qc.CountAsync(QueueType.ItemQueue, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(5 + itemsBase, count); // Peek the next item. QueueItem <Item> item = await qc.PeekItemAsync(QueueType.FirstQueue, QueueType.LastQueue, CancellationToken.None).ConfigureAwait(false); Assert.IsNotNull(item); Console.WriteLine(item.ToString()); // Get a list of the items. items = await qc.GetItemsAsync(0, 1000, 0).ConfigureAwait(false); foreach (var qi in items) { Console.WriteLine(qi.Item.Name); } // Wait for the lease to expire and check the counts. Thread.Sleep(TimeSpan.FromSeconds(125)); count = await qc.CountAsync(QueueType.AllQueues, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(4 + allBase, count); count = await qc.CountAsync(QueueType.LeaseQueue, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(0 + leaseBase, count); count = await qc.CountAsync(QueueType.ItemQueue, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(5 + itemsBase, count); // Dequeue again, release the lease and check the counts. items = await qc.DequeueAsync(1, QueueType.FirstQueue, QueueType.LastQueue, CancellationToken.None).ConfigureAwait(false); await ReleaseLeasesAsync(qc, items); count = await qc.CountAsync(QueueType.AllQueues, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(4 + allBase, count); count = await qc.CountAsync(QueueType.LeaseQueue, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(0 + leaseBase, count); count = await qc.CountAsync(QueueType.ItemQueue, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(4 + itemsBase, count); // Dequeue two, release the lease and check the counts. items = await qc.DequeueAsync(2, QueueType.FirstQueue, QueueType.LastQueue, CancellationToken.None).ConfigureAwait(false); await ReleaseLeasesAsync(qc, items); count = await qc.CountAsync(QueueType.AllQueues, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(2 + allBase, count); count = await qc.CountAsync(QueueType.LeaseQueue, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(0 + leaseBase, count); count = await qc.CountAsync(QueueType.ItemQueue, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(2 + itemsBase, count); // Dequeue two, release the lease and check the counts. items = await qc.DequeueAsync(2, QueueType.FirstQueue, QueueType.LastQueue, CancellationToken.None).ConfigureAwait(false); await ReleaseLeasesAsync(qc, items); count = await qc.CountAsync(QueueType.AllQueues, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(0 + allBase, count); count = await qc.CountAsync(QueueType.LeaseQueue, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(0 + leaseBase, count); count = await qc.CountAsync(QueueType.ItemQueue, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(0 + itemsBase, count); }