Geeks With Blogs

@JReuben1
  • JReuben1 AngularJS Directive templateUrl --> halfway to W3C WebComponents ! about 558 days ago
  • JReuben1 Yeoman AngularJS generator - generate controllers, views, routes, services - NICE! about 559 days ago
  • JReuben1 A comparison of HTML5 Canvas 2D JS libs http://t.co/fcB7jvnhqY KineticJS , EaselJS, fabric.js, Paper.js, processing.js seen as the leaders about 560 days ago

Josh Reuben

I’m leveraging a ConcurrentPriorityQueue – from http://code.msdn.microsoft.com/ParExtSamples.

clip_image002

This class basically is a thread safe IProducerConsumerCollection wrapper for a binary heap that prioritizes smaller values. You use it as you would a dictionary, where the priority is the key, except you can have duplicate keys (ie values with the same priority).

I needed to demonstrate to a customer that it worked.

I set up my queue and my priority enum values:

var q = new ConcurrentPriorityQueue<int, string>();

var priorityValues = Enum.GetValues(typeof(JobPriority));

I then randomly enqueued different priorities from different threads:

var random = new Random();
Parallel.For(0, 1000, i =>
{
    var randomPriority = (JobPriority) priorityValues.GetValue(random.Next(priorityValues.Length));
    Debug.WriteLine("enqueueing:" + randomPriority);
    q.Enqueue((int) randomPriority, randomPriority.ToString());
});
 
and finally I asserted that dequeueing occurs in order:
while (q.TryDequeue(out printJobKVP))
{
                
    var jobPriority = (JobPriority)Enum.Parse(typeof(JobPriority), printJobKVP.Value);
    Assert.IsTrue(jobPriority >= previousJobPriority);
    previousJobPriority = jobPriority;
}
 

Posted on Sunday, December 4, 2011 1:05 PM Parallelism | Back to top


Comments on this post: Unit Testing a ConcurrentPriorityQueue

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © JoshReuben | Powered by: GeeksWithBlogs.net | Join free