public virtual void TestFull() { SinkQueue <int> q = new SinkQueue <int>(1); q.Enqueue(1); Assert.True("should drop", !q.Enqueue(2)); Assert.Equal("element", 1, (int)q.Dequeue()); q.Enqueue(3); q.Consume(new _Consumer_114()); Assert.Equal("queue size", 0, q.Size()); }
public virtual void TestCommon() { SinkQueue <int> q = new SinkQueue <int>(2); q.Enqueue(1); Assert.Equal("queue front", 1, (int)q.Front()); Assert.Equal("queue back", 1, (int)q.Back()); Assert.Equal("element", 1, (int)q.Dequeue()); Assert.True("should enqueue", q.Enqueue(2)); q.Consume(new _Consumer_50()); Assert.True("should enqueue", q.Enqueue(3)); Assert.Equal("element", 3, (int)q.Dequeue()); Assert.Equal("queue size", 0, q.Size()); Assert.Equal("queue front", null, q.Front()); Assert.Equal("queue back", null, q.Back()); }
public virtual void TestConsumeAll() { int capacity = 64; // arbitrary SinkQueue <int> q = new SinkQueue <int>(capacity); for (int i = 0; i < capacity; ++i) { Assert.True("should enqueue", q.Enqueue(i)); } Assert.True("should not enqueue", !q.Enqueue(capacity)); Runnable trigger = Org.Mockito.Mockito.Mock <Runnable>(); q.ConsumeAll(new _Consumer_136(trigger)); Org.Mockito.Mockito.Verify(trigger, Org.Mockito.Mockito.Times(capacity)).Run(); }
public virtual void TestConcurrentConsumers() { SinkQueue <int> q = NewSleepingConsumerQueue(2, 1); Assert.True("should enqueue", q.Enqueue(2)); Assert.Equal("queue back", 2, (int)q.Back()); Assert.True("should drop", !q.Enqueue(3)); // should not block ShouldThrowCME(new _Fun_206(q)); ShouldThrowCME(new _Fun_211(q)); ShouldThrowCME(new _Fun_216(q)); ShouldThrowCME(new _Fun_221(q)); // The queue should still be in consistent state after all the exceptions Assert.Equal("queue size", 2, q.Size()); Assert.Equal("queue front", 1, (int)q.Front()); Assert.Equal("queue back", 2, (int)q.Back()); }
/// <exception cref="System.Exception"/> private void TestEmptyBlocking(int awhile) { SinkQueue <int> q = new SinkQueue <int>(2); Runnable trigger = Org.Mockito.Mockito.Mock <Runnable>(); // try consuming emtpy equeue and blocking Thread t = new _Thread_75(q, trigger); t.Start(); // Should work with or without sleep if (awhile > 0) { Thread.Sleep(awhile); } q.Enqueue(1); q.Enqueue(2); t.Join(); Org.Mockito.Mockito.Verify(trigger).Run(); }
public virtual void TestHangingConsumer() { SinkQueue <int> q = NewSleepingConsumerQueue(2, 1, 2); Assert.Equal("queue back", 2, (int)q.Back()); Assert.True("should drop", !q.Enqueue(3)); // should not block Assert.Equal("queue size", 2, q.Size()); Assert.Equal("queue head", 1, (int)q.Front()); Assert.Equal("queue back", 2, (int)q.Back()); }
public virtual void TestClear() { SinkQueue <int> q = new SinkQueue <int>(128); for (int i = 0; i < q.Capacity() + 97; ++i) { q.Enqueue(i); } Assert.Equal("queue size", q.Capacity(), q.Size()); q.Clear(); Assert.Equal("queue size", 0, q.Size()); }
public virtual void TestConsumerException() { SinkQueue <int> q = new SinkQueue <int>(1); RuntimeException ex = new RuntimeException("expected"); q.Enqueue(1); try { q.Consume(new _Consumer_157(ex)); } catch (Exception expected) { NUnit.Framework.Assert.AreSame("consumer exception", ex, expected); } // The queue should be in consistent state after exception Assert.Equal("queue size", 1, q.Size()); Assert.Equal("element", 1, (int)q.Dequeue()); }
/// <exception cref="System.Exception"/> private SinkQueue <int> NewSleepingConsumerQueue(int capacity, params int[] values ) { SinkQueue <int> q = new SinkQueue <int>(capacity); foreach (int i in values) { q.Enqueue(i); } CountDownLatch barrier = new CountDownLatch(1); Thread t = new _Thread_251(q, barrier); // causes failure without barrier // a long time t.SetName("Sleeping consumer"); t.SetDaemon(true); // so jvm can exit t.Start(); barrier.Await(); Log.Debug("Returning new sleeping consumer queue"); return(q); }