/// <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> /// Read test thread procedure. /// </summary> /// <param name="data">AutoResetEvent instance to signal when complete.</param> internal void ReadThreadProc(object data) { AutoResetEvent evt = (AutoResetEvent)data; // Create a queue client. HttpQueueClient <T> qc = new HttpQueueClient <T>(new Uri(serviceUri), c_ListenerName); // Check that cancellation isn't requested and we are not at the requested count. while ((_requestCount < _maxRequests) && (false == _cancellationToken.IsCancellationRequested)) { try { Stopwatch swCall = Stopwatch.StartNew(); IEnumerable <QueueItem <T> > items = qc.DequeueAsync(_batchSize, cancellationToken: _cancellationToken).GetAwaiter().GetResult(); swCall.Stop(); // Track the call time. TrackCallData(swCall.ElapsedMilliseconds); // Build the list of leases to release. List <PopReceipt> keys = new List <PopReceipt>(_batchSize); foreach (QueueItem <T> item in items) { keys.Add(item.Key); } // Were any items returned? if (0 == keys.Count) { Interlocked.Increment(ref _notFoundOrConflict); } else // Release the held leases for each item. { IEnumerable <bool> results = qc.ReleaseLeaseAsync(keys, _cancellationToken).GetAwaiter().GetResult(); foreach (bool bResult in results) { if (bResult) { Interlocked.Increment(ref _successfulRequests); } } } } catch (TimeoutException) { Interlocked.Increment(ref _timedoutRequests); } catch (Exception ex) { Console.WriteLine($"ReadTest.ReadThreadProc exception {ex.GetType().Name}: {ex.Message}, {ex.StackTrace}"); } // Increment the request count. Interlocked.Increment(ref _requestCount); if (0 == _requestCount % 100) { Console.Write("\rTotal Read: {0,-15:N0}", _requestCount); } } // Signal this thread has completed. evt.Set(); }
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 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); }