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)); }
/// <summary> /// Write test thread procedure. /// </summary> /// <param name="data">AutoResetEvent instance to signal when complete.</param> internal void WriteThreadProc(object data) { AutoResetEvent evt = (AutoResetEvent)data; 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 { // Create a list of items to add. List <T> addItems = new List <T>(); for (int i = 0; i < _batchSize; i++) { addItems.Add(_createInstance()); } int queue = RandomThreadSafe.Instance.Next(0, _priorityCount); //Console.WriteLine($"WriteTest.WriteThreadProc Thread:{System.Threading.Thread.CurrentThread.ManagedThreadId}, Queue{queue}"); Stopwatch swCall = Stopwatch.StartNew(); //TODO: change to specify queue. Note can also specify lease time and expiration here as well! //IEnumerable<QueueItem<T>> items = qc.EnqueueAsync(addItems, cancellationToken: _cancellationToken).GetAwaiter().GetResult(); IEnumerable <QueueItem <T> > items = qc.EnqueueAsync(addItems, queue, cancellationToken: _cancellationToken).GetAwaiter().GetResult(); swCall.Stop(); // Add the number of items added successfully. Interlocked.Add(ref _successfulRequests, items.Count()); // Track the call time. TrackCallData(swCall.ElapsedMilliseconds); } catch (TimeoutException) { Interlocked.Increment(ref _timedoutRequests); } catch (HttpResponseException ex) { Console.WriteLine($"WriteTest.WriteThreadProc exception {ex.GetType().Name}: {ex.Message}, {ex.Response.StatusCode}, {ex.Response.ReasonPhrase}, {ex.Response.RequestMessage.RequestUri.AbsoluteUri}, {ex.StackTrace}"); } catch (Exception ex) { Console.WriteLine($"WriteTest.WriteThreadProc exception {ex.GetType().Name}: {ex.Message}, {ex.StackTrace}"); } // Increment the request count. Interlocked.Increment(ref _requestCount); if (0 == _requestCount % 100) { Console.Write("\rTotal written: {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 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); }