public void CloseBehviour() { var queue = new SharedQueue(); queue.Enqueue("Hello World"); //queue.Close(); var item = queue.Dequeue().ToString(); Console.Out.WriteLine("item = {0}", item); ThreadPool.QueueUserWorkItem(state => { // expect the queue to block here Console.WriteLine("Dequeue in thread"); try { var item2 = queue.Dequeue().ToString(); } catch (Exception e) { Console.WriteLine("Exception in thread\n{0}", e.ToString()); } }); Thread.Sleep(100); Console.WriteLine("Closing queue"); queue.Close(); Console.WriteLine("Done"); }
public void TestTimeoutShort() { SharedQueue q = new SharedQueue(); q.Enqueue(123); ResetTimer(); object v; bool r = q.Dequeue(TimingInterval, out v); Assert.Greater(SafetyMargin, ElapsedMs()); Assert.IsTrue(r); Assert.AreEqual(123, v); }
public void TestEnumerator() { SharedQueue q = new SharedQueue(); VolatileInt c1 = new VolatileInt(); VolatileInt c2 = new VolatileInt(); Thread t1 = new Thread(delegate() { foreach (int v in q) c1.v+=v; }); Thread t2 = new Thread(delegate() { foreach (int v in q) c2.v+=v; }); t1.Start(); t2.Start(); q.Enqueue(1); q.Enqueue(2); q.Enqueue(3); q.Close(); t1.Join(); t2.Join(); Assert.AreEqual(6, c1.v + c2.v); }
public void TestDequeueNoWait2() { SharedQueue q = new SharedQueue(); q.Enqueue(1); Assert.AreEqual(1, q.Dequeue()); Assert.AreEqual(0, q.DequeueNoWait(0)); }
public void TestCloseWhenFull() { SharedQueue q = new SharedQueue(); object v; q.Enqueue(1); q.Enqueue(2); q.Enqueue(3); q.Close(); DelayedEnqueuer de = new DelayedEnqueuer(q, 0, 4); ExpectEof(new Thunk(de.EnqueueValue)); Assert.AreEqual(1, q.Dequeue()); Assert.AreEqual(2, q.DequeueNoWait(0)); bool r = q.Dequeue(1, out v); Assert.IsTrue(r); Assert.AreEqual(3, v); ExpectEof(new Thunk(de.Dequeue)); }
public void TestEnumerator() { var queue = new SharedQueue(); var c1 = new VolatileInt(); var c2 = new VolatileInt(); var thread1 = new Thread(() => { foreach (int v in queue) c1.v += v; }); var thread2 = new Thread(() => { foreach (int v in queue) c2.v += v; }); thread1.Start(); thread2.Start(); queue.Enqueue(1); queue.Enqueue(2); queue.Enqueue(3); queue.Close(); thread1.Join(); thread2.Join(); Assert.AreEqual(6, c1.v + c2.v); }
public void TestDequeueNoWait1() { var queue = new SharedQueue(); queue.Enqueue(1); Assert.AreEqual(1, queue.DequeueNoWait(0)); Assert.AreEqual(0, queue.DequeueNoWait(0)); }