Skip to content

Instantly share code, notes, and snippets.

@ar3cka
Last active August 29, 2015 14:04
Show Gist options
  • Save ar3cka/92806ada767a898863a0 to your computer and use it in GitHub Desktop.
Save ar3cka/92806ada767a898863a0 to your computer and use it in GitHub Desktop.
Azure queue concurrency safe testing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
namespace AzureQueue.ConcurrencySafeTesting
{
class Program
{
static void Main(string[] args)
{
var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
var queueClient = storageAccount.CreateCloudQueueClient();
var queue = queueClient.GetQueueReference("concurrency-testing-queue");
queue.CreateIfNotExists();
Parallel.For(0, 1000, index =>
{
queue.AddMessage(new CloudQueueMessage(index.ToString()));
});
var messageCount =
Enumerable.Range(0, 8)
.Select(index => CountMessages(queue))
.AsParallel()
.Aggregate(0, (sum, count) => sum + count);
Console.WriteLine("Received messages count: {0}", messageCount);
}
private static int CountMessages(CloudQueue queue)
{
var hashset = new HashSet<int>();
var emptyQueue = false;
while (emptyQueue != true)
{
emptyQueue = true;
foreach (var message in queue.GetMessages(CloudQueueMessage.MaxNumberOfMessagesToPeek))
{
hashset.Add(int.Parse(message.AsString));
queue.DeleteMessage(message);
emptyQueue = false;
}
}
return hashset.Count;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment