public void ShouldWaitForWorkCompleteWhereAllWorkersAreBlockedOnRingBuffer() { long expectedNumberMessages = 10; fillRingBuffer(expectedNumberMessages); var workers = new StubConsumer[3]; for (int i = 0, size = workers.Length; i < size; i++) { workers[i] = new StubConsumer(0); workers[i].Sequence = (expectedNumberMessages - 1); } IConsumerBarrier consumerBarrier = ringBuffer.CreateConsumerBarrier(workers); ThreadStart runnable = () => { StubEntry entry = producerBarrier.NextEntry(); entry.Value = ((int)entry.Sequence); producerBarrier.Commit(entry); foreach (StubConsumer stubWorker in workers) { stubWorker.Sequence = (entry.Sequence); } }; new Thread(runnable).Start(); long expectedWorkSequence = expectedNumberMessages; long completedWorkSequence = consumerBarrier.WaitFor(expectedNumberMessages); Assert.IsTrue(completedWorkSequence >= expectedWorkSequence); }
public void ShouldClaimAndGetMultipleMessages() { //We need this so that the sequence and values for the entries //aren't the same. This prevents simple errors mixing up //.Value and .Sequence int entryValueOffset = 3; int numMessages = ringBuffer.Capacity; for (int i = 0; i < numMessages; i++) { StubEntry entry = producerBarrier.NextEntry(); entry.Value = i + entryValueOffset; producerBarrier.Commit(entry); } int expectedSequence = numMessages - 1; long available = consumerBarrier.WaitFor(expectedSequence); Assert.AreEqual(expectedSequence, available); for (int i = 0; i < numMessages; i++) { Assert.AreEqual(i + entryValueOffset, ringBuffer.GetEntry(i).Value); } }
private void fillRingBuffer(long expectedNumberMessages) // throws InterruptedException { for (long i = 0; i < expectedNumberMessages; i++) { StubEntry entry = producerBarrier.NextEntry(); entry.Value = (int)i; producerBarrier.Commit(entry); } }
public void ShouldClaimAndGetInSeparateThread() { Task<List<StubEntry>> messages = GetMessages(0, 0); StubEntry expectedEntry = new StubEntry(2701); StubEntry oldEntry = producerBarrier.NextEntry(); oldEntry.copy(expectedEntry); producerBarrier.Commit(oldEntry); Assert.AreEqual(expectedEntry, messages.Result[0]); }
public void ShouldClaimAndGetInSeparateThread() { Task <List <StubEntry> > messages = GetMessages(0, 0); StubEntry expectedEntry = new StubEntry(2701); StubEntry oldEntry = producerBarrier.NextEntry(); oldEntry.copy(expectedEntry); producerBarrier.Commit(oldEntry); Assert.AreEqual(expectedEntry, messages.Result[0]); }
public void ShouldClaimAndGetWithTimeout() { Assert.AreEqual(-1L, ringBuffer.Cursor); var expectedEntry = new StubEntry(2701); StubEntry oldEntry = producerBarrier.NextEntry(); oldEntry.copy(expectedEntry); producerBarrier.Commit(oldEntry); long sequence = consumerBarrier.WaitFor(0, 5); Assert.AreEqual(0, sequence); StubEntry entry = ringBuffer.GetEntry(sequence); Assert.AreEqual(expectedEntry, entry); Assert.AreEqual(0L, ringBuffer.Cursor); }
public void ShouldClaimAndGetWithTimeout() { Assert.AreEqual(-1L, ringBuffer.Cursor); var expectedEntry = new StubEntry(2701); StubEntry oldEntry = producerBarrier.NextEntry(); oldEntry.copy(expectedEntry); producerBarrier.Commit(oldEntry); long sequence = consumerBarrier.WaitFor(0, 5); Assert.AreEqual(0, sequence); StubEntry entry = ringBuffer.GetEntry(sequence); Assert.AreEqual(expectedEntry, entry); Assert.AreEqual(0L, ringBuffer.Cursor); }
public void ShouldSetAtSpecificSequence() { long expectedSequence = 5; IForceFillProducerBarrier <StubEntry> forceFillProducerBarrier = ringBuffer.CreateForceFillProducerBarrier(new NoOpConsumer(ringBuffer)); StubEntry expectedEntry = forceFillProducerBarrier.ClaimEntry(expectedSequence); expectedEntry.Value = (int)expectedSequence; forceFillProducerBarrier.Commit(expectedEntry); long sequence = consumerBarrier.WaitFor(expectedSequence); Assert.AreEqual(expectedSequence, sequence); StubEntry entry = ringBuffer.GetEntry(sequence); Assert.AreEqual(expectedEntry, entry); Assert.AreEqual(expectedSequence, ringBuffer.Cursor); }