public void InsertBeyondCapacityTest() { var queue = new CostBoundedConcurrentQueue<string>(20, new StringLengthCostAssigner()); Assert.True(queue.Enqueue(BuildStringOfSize(9))); Assert.True(queue.Enqueue(BuildStringOfSize(11))); Assert.Equal(2, queue.Count); Assert.False(queue.Enqueue(BuildStringOfSize(1))); Assert.Equal(20, queue.Cost); }
public void InsertBeyondCapacityTest() { var queue = new CostBoundedConcurrentQueue <string>(20, new StringLengthCostAssigner()); Assert.True(queue.Enqueue(BuildStringOfSize(9))); Assert.True(queue.Enqueue(BuildStringOfSize(11))); Assert.Equal(2, queue.Count); Assert.False(queue.Enqueue(BuildStringOfSize(1))); Assert.Equal(20, queue.Cost); }
public void DrainToTest() { var queue = new CostBoundedConcurrentQueue<string>(1000, new StringLengthCostAssigner()); queue.Enqueue(BuildStringOfSize(600)); queue.Enqueue(BuildStringOfSize(200)); Assert.Equal(800, queue.Cost); queue.DrainTo(new List<string>()); Assert.Equal(0, queue.Cost); Assert.Equal(0, queue.Count); }
public void DrainToTest() { var queue = new CostBoundedConcurrentQueue <string>(1000, new StringLengthCostAssigner()); queue.Enqueue(BuildStringOfSize(600)); queue.Enqueue(BuildStringOfSize(200)); Assert.Equal(800, queue.Cost); queue.DrainTo(new List <string>()); Assert.Equal(0, queue.Cost); Assert.Equal(0, queue.Count); }
public void InsertElementInQueueTest() { var queue = new CostBoundedConcurrentQueue<string>(100, new StringLengthCostAssigner()); queue.Enqueue("hello"); queue.Enqueue("bye"); Assert.Equal(2, queue.Count); Assert.Equal(8, queue.Cost); Assert.Equal("hello", queue.Dequeue()); Assert.Equal(1, queue.Count); Assert.Equal("bye", queue.Dequeue()); Assert.Equal(0, queue.Count); }
public void InsertElementInQueueTest() { var queue = new CostBoundedConcurrentQueue <string>(100, new StringLengthCostAssigner()); queue.Enqueue("hello"); queue.Enqueue("bye"); Assert.Equal(2, queue.Count); Assert.Equal(8, queue.Cost); Assert.Equal("hello", queue.Dequeue()); Assert.Equal(1, queue.Count); Assert.Equal("bye", queue.Dequeue()); Assert.Equal(0, queue.Count); }
public void DrainToWithWritersTest() { var queue = new CostBoundedConcurrentQueue <string>(10000, new StringLengthCostAssigner()); for (int i = 0; i < 5000; i++) { queue.Enqueue(BuildStringOfSize(2)); } Assert.Equal(10000, queue.Cost); // start background writer that will attempt to enqueue // new messages while we drain var writerThread = StartBackgroundWriter(queue, 1, 7000); Thread.Sleep(100); var drainCollection = new List <string>(); queue.DrainTo(drainCollection); // only the original 5000 messages should be drained, none // of the newly-written messages Assert.Equal(5000, drainCollection.Count); // background writer should finish writing Thread.Sleep(1000); drainCollection.Clear(); queue.DrainTo(drainCollection); Assert.Equal(7000, drainCollection.Count); if (writerThread.IsAlive) { writerThread.Abort(); } }
public void DrainToWithWritersTest() { var queue = new CostBoundedConcurrentQueue<string>(10000, new StringLengthCostAssigner()); for (int i = 0; i < 5000; i++) { queue.Enqueue(BuildStringOfSize(2)); } Assert.Equal(10000, queue.Cost); // start background writer that will attempt to enqueue // new messages while we drain var writerThread = StartBackgroundWriter(queue, 1, 7000); Thread.Sleep(100); var drainCollection = new List<string>(); queue.DrainTo(drainCollection); // only the original 5000 messages should be drained, none // of the newly-written messages Assert.Equal(5000, drainCollection.Count); // background writer should finish writing Thread.Sleep(1000); drainCollection.Clear(); queue.DrainTo(drainCollection); Assert.Equal(7000, drainCollection.Count); if (writerThread.IsAlive) { writerThread.Abort(); } }
public void SizeAndCapacityTest() { var queue = new CostBoundedConcurrentQueue<string>(20, new StringLengthCostAssigner()); queue.Enqueue(BuildStringOfSize(9)); queue.Enqueue(BuildStringOfSize(11)); queue.Enqueue(BuildStringOfSize(11)); Assert.Equal(20, queue.Cost); Assert.Equal(2, queue.Count); queue.Dequeue(); Assert.Equal(11, queue.Cost); Assert.Equal(1, queue.Count); queue.Enqueue(BuildStringOfSize(3)); Assert.Equal(2, queue.Count); queue.Enqueue(BuildStringOfSize(3)); Assert.Equal(3, queue.Count); Assert.Equal(17, queue.Cost); }
public void SizeAndCapacityTest() { var queue = new CostBoundedConcurrentQueue <string>(20, new StringLengthCostAssigner()); queue.Enqueue(BuildStringOfSize(9)); queue.Enqueue(BuildStringOfSize(11)); queue.Enqueue(BuildStringOfSize(11)); Assert.Equal(20, queue.Cost); Assert.Equal(2, queue.Count); queue.Dequeue(); Assert.Equal(11, queue.Cost); Assert.Equal(1, queue.Count); queue.Enqueue(BuildStringOfSize(3)); Assert.Equal(2, queue.Count); queue.Enqueue(BuildStringOfSize(3)); Assert.Equal(3, queue.Count); Assert.Equal(17, queue.Cost); }
/// <summary> /// Starts a background thread that will aggressively write elements into the specified queue. /// </summary> /// <param name="queue">Queue into which elements should be written</param> /// <param name="elementCost">Cost of each element</param> /// <param name="howMany">How many total elements should be written</param> /// <returns>The resultant thread. The thread will already be started.</returns> private static Thread StartBackgroundWriter(CostBoundedConcurrentQueue <string> queue, int elementCost, int howMany) { Thread t = new Thread(new ThreadStart(() => { var str = BuildStringOfSize(elementCost); // spin as fast as possible, adding elements as soon as capacity becomes available for (int i = 0; i < howMany; i++) { while (!queue.Enqueue(str)) { } } })); t.Start(); return(t); }
/// <summary> /// Starts a background thread that will aggressively write elements into the specified queue. /// </summary> /// <param name="queue">Queue into which elements should be written</param> /// <param name="elementCost">Cost of each element</param> /// <param name="howMany">How many total elements should be written</param> /// <returns>The resultant thread. The thread will already be started.</returns> private static Thread StartBackgroundWriter(CostBoundedConcurrentQueue<string> queue, int elementCost, int howMany) { Thread t = new Thread(new ThreadStart(() => { var str = BuildStringOfSize(elementCost); // spin as fast as possible, adding elements as soon as capacity becomes available for (int i = 0; i < howMany; i++) { while (!queue.Enqueue(str)) { } } })); t.Start(); return t; }