public void TestHuge() { using (var b = new ThreadSafeWriteQueue(1024)) { var pos = b.Allocate(1024 - 2); b.Commit(pos); var a = b.ReadMessage(); Assert.AreEqual(pos, a); Assert.AreEqual(-1, b.ReadMessage()); pos = b.Allocate(1024 - 2); b.Commit(pos); a = b.ReadMessage(); Assert.AreEqual(pos, a); Assert.AreEqual(-1, b.ReadMessage()); Assert.Throws<OverflowException>(() => b.Allocate(1024 - 1)); } }
public void TestMultiThreading() { var threads = new Thread[4]; int messageLength = 2; int maxNumMessages = 1024 * 1024 - 1; using (ThreadSafeWriteQueue b = new ThreadSafeWriteQueue(threads.Length * messageLength * maxNumMessages)) { using (Semaphore semaphore = new Semaphore(0, threads.Length)) { var messagesPerThread = maxNumMessages; for (int index = 0; index < threads.Length; index++) { var thread = threads[index] = new Thread(this.WriteProc); thread.Start(new Context { Buffer = b, Semaphore = semaphore, Count = messagesPerThread, Id = index }); } semaphore.Release(threads.Length); var t1 = DateTime.Now; foreach (var thread in threads) { thread.Join(); } var t2 = DateTime.Now; Console.WriteLine( "{0} sec ({1} per sec)", t2.Subtract(t1).TotalSeconds, messagesPerThread * threads.Length / t2.Subtract(t1).TotalSeconds); int count = 0; int[] countsPerThread = new int[threads.Length]; int pos; while ((pos = b.ReadMessage()) >= 0) { ++countsPerThread[b.ReadInt32(pos)]; ++count; } for (int index = 0; index < countsPerThread.Length; index++) { Console.WriteLine("Thread {0} sent {1} messages", index, countsPerThread[index]); } Assert.AreEqual(messagesPerThread * threads.Length, count); } } }
public void TestSafeRead() { using (var b = new ThreadSafeWriteQueue(8)) { b.Commit(b.Allocate(2)); b.Commit(b.Allocate(2)); b.ReadMessage(); Assert.Throws<OverflowException>(() => b.Allocate(2)); b.ReadMessage(); b.Commit(b.Allocate(2)); } }