/// <summary> /// Gets the number of items in the cache. /// </summary> private static async Task GetItemCountAsync() { HttpQueueClient <Item> qc = new HttpQueueClient <Item>(_serviceUri, c_ListenerName); long priorityCount = await qc.PriorityCountAsync().ConfigureAwait(false); Stopwatch sw = Stopwatch.StartNew(); Task[] tasks = new Task[priorityCount + 4]; long itemCount = 0, allQueueCount = 0, leasedCount = 0, expiredCount = 0; long itemTimes = 0, allQueueTimes = 0, leasedTimes = 0, expiredTimes = 0; tasks[0] = qc.CountAsync(QueueType.ItemQueue).ContinueWith((t) => { itemCount = t.Result; itemTimes = sw.ElapsedMilliseconds; }); tasks[1] = qc.CountAsync(QueueType.AllQueues).ContinueWith((t) => { allQueueCount = t.Result; allQueueTimes = sw.ElapsedMilliseconds; }); tasks[2] = qc.CountAsync(QueueType.LeaseQueue).ContinueWith((t) => { leasedCount = t.Result; leasedTimes = sw.ElapsedMilliseconds; }); tasks[3] = qc.CountAsync(QueueType.ExpiredQueue).ContinueWith((t) => { expiredCount = t.Result; expiredTimes = sw.ElapsedMilliseconds; }); long[] queues = new long[priorityCount]; for (int i = 0; i < priorityCount; i++) { var index = i; tasks[i + 4] = qc.CountAsync(i).ContinueWith((t) => { queues[index] = t.Result; }); } // Wait for all queue calls to complete. Task.WaitAll(tasks); Console.WriteLine($" Item count: {itemCount:N0} in {itemTimes:N0}ms"); Console.WriteLine($" Leased item count: {leasedCount:N0} in {allQueueTimes:N0}ms"); Console.WriteLine($"Expired item count: {expiredCount:N0} in {leasedTimes:N0}ms"); Console.WriteLine($" All queues count: {allQueueCount:N0} in {expiredTimes:N0}ms"); for (int i = 0; i < queues.Length; i++) { Console.WriteLine($" Queue {i,2:N0}: {queues[i]:N0}"); } }
/// <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); } }
public async Task DeleteItems_Test() { HttpQueueClient <Item> qc = new HttpQueueClient <Item>(new Uri(serviceUri), c_ListenerName); Assert.IsNotNull(qc); // Check that the count is zero. long beginQueueCount = await qc.CountAsync().ConfigureAwait(false); long beginItemCount = await qc.CountAsync(QueueType.ItemQueue).ConfigureAwait(false); // Add three items to the queue. Item[] items = new Item[] { i1, i2, i3 }; var enqueueResult = await qc.EnqueueAsync(items).ConfigureAwait(false); Assert.IsNotNull(enqueueResult); foreach (QueueItem <Item> item in enqueueResult) { var deletedItem = await qc.DeleteItemAsync(item.Key, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(item.Key, deletedItem.Key); Assert.AreEqual(item.DequeueCount, deletedItem.DequeueCount); Assert.AreEqual(item.EnqueueTime, deletedItem.EnqueueTime); Assert.AreEqual(item.ExpirationTime, deletedItem.ExpirationTime); Assert.IsTrue(item.Item.Equals(deletedItem.Item)); Assert.AreEqual(item.LeasedUntil, deletedItem.LeasedUntil); Assert.AreEqual(item.LeaseDuration, deletedItem.LeaseDuration); Assert.AreEqual(item.Queue, deletedItem.Queue); } Assert.AreEqual(beginQueueCount + items.Count(), await qc.CountAsync().ConfigureAwait(false)); Assert.AreEqual(beginItemCount, await qc.CountAsync(QueueType.ItemQueue).ConfigureAwait(false)); }
public async Task GetCount_Test() { long queueLength = -1; // Get the queue length for the cluster using the load APIs. FabricClient sfc = new FabricClient(); var li = await sfc.QueryManager.GetClusterLoadInformationAsync(); foreach (var lmi in li.LoadMetricInformationList) { if ("QueueLength" == lmi.Name) { queueLength = lmi.ClusterLoad; break; } } Assert.IsTrue(queueLength >= 0); // Get the cluster count based on the count of each queue partition. HttpQueueClient <string> qc = new HttpQueueClient <string>(new Uri(serviceUri), c_ListenerName); Assert.IsNotNull(qc); long count = await qc.CountAsync().ConfigureAwait(false); Assert.AreEqual(queueLength, count); }
public async Task Dequeue_Test() { HttpQueueClient <Item> qc = new HttpQueueClient <Item>(new Uri(serviceUri), c_ListenerName); Assert.IsNotNull(qc); Item[] items = new Item[] { i1, i2, i3 }; // Clear the existing queue. await ClearQueueAsync(); // Check that the count is zero. long count = await qc.CountAsync().ConfigureAwait(false); Assert.AreEqual(0, count); // Enqueue the items. var response = await qc.EnqueueAsync(items).ConfigureAwait(false); Assert.IsNotNull(response); // Check that all items were queued. count = await qc.CountAsync().ConfigureAwait(false); Assert.AreEqual(items.Length, count); // Dequeue items. response = await qc.DequeueAsync().ConfigureAwait(false); Assert.IsNotNull(response); QueueItem <Item> item = response.First(); Assert.IsNotNull(item); Assert.IsTrue(i1.Equals(item.Item)); Assert.AreEqual(0, item.Queue); Assert.AreEqual(1, item.DequeueCount); Assert.AreEqual(TimeSpan.FromMinutes(10), item.LeaseDuration); Assert.AreEqual(DateTimeOffset.MaxValue, item.ExpirationTime); // Get the queue count at the end. The queue must be started empty for this test to pass correctly. Assert.AreEqual(3, await qc.CountAsync(QueueType.ItemQueue).ConfigureAwait(false)); Assert.AreEqual(1, await qc.CountAsync(QueueType.LeaseQueue).ConfigureAwait(false)); Assert.AreEqual(2, await qc.CountAsync(QueueType.AllQueues).ConfigureAwait(false)); }
public async Task ClearQueue_Test() { HttpQueueClient <Item> qc = new HttpQueueClient <Item>(new Uri(serviceUri), c_ListenerName); long queueCount = await qc.CountAsync().ConfigureAwait(false); long itemCount = await qc.CountAsync(QueueType.ItemQueue).ConfigureAwait(false); // If there are no items, add some so there is something to clear. if (0 == (queueCount + itemCount)) { Item[] items = new Item[] { i1, i2, i3 }; var response = await qc.EnqueueAsync(items).ConfigureAwait(false); Assert.IsNotNull(response); queueCount = items.Length; } await ClearQueueAsync(); Assert.AreEqual(0, await qc.CountAsync().ConfigureAwait(false)); Assert.AreEqual(0, await qc.CountAsync(QueueType.ItemQueue).ConfigureAwait(false)); }
public async Task Enqueue_Test() { HttpQueueClient <Item> qc = new HttpQueueClient <Item>(new Uri(serviceUri), c_ListenerName); Assert.IsNotNull(qc); Item[] items = new Item[] { i1, i2, i3 }; // Clear the existing queue. await ClearQueueAsync(); // Get the queue count at the beginning. long beginCount = await qc.CountAsync().ConfigureAwait(false); // Enqueue a single item. var response = await qc.EnqueueAsync(items).ConfigureAwait(false); Assert.IsNotNull(response); List <QueueItem <Item> > responseItems = new List <QueueItem <Item> >(response); Assert.AreEqual(items.Length, responseItems.Count); Assert.AreEqual(0, responseItems[0].Queue); Assert.AreEqual(0, responseItems[0].DequeueCount); Assert.AreEqual(TimeSpan.FromMinutes(10), responseItems[0].LeaseDuration); Assert.AreEqual(DateTimeOffset.MaxValue, responseItems[0].ExpirationTime); for (int i = 0; i < items.Length; i++) { Assert.IsTrue(items[i].Equals(responseItems[i].Item)); } // Get the queue count at the end. long endCount = await qc.CountAsync(); Assert.AreEqual(beginCount + items.Length, endCount); }
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); }