public void NextInStreamTest() { IObjectPool <CachedMessageBlock <TestCachedMessage> > pool = new MyTestPooled(); ICacheDataAdapter <TestQueueMessage, TestCachedMessage> dataAdapter = new TestCacheDataAdapter(); CachedMessageBlock <TestCachedMessage> block = pool.Allocate(); int last = 0; int sequenceNumber = 0; // define 2 streams var streams = new[] { new StreamIdentity(Guid.NewGuid(), null), new StreamIdentity(Guid.NewGuid(), null) }; // add both streams interleaved, until lock is full while (block.HasCapacity) { var stream = streams[last % 2]; var message = new TestQueueMessage { StreamGuid = stream.Guid, SequenceToken = new EventSequenceToken(sequenceNumber) }; // add message to end of block AddAndCheck(block, dataAdapter, message, 0, last - 1); last++; sequenceNumber += 2; } // get index of first stream int streamIndex; Assert.True(block.TryFindFirstMessage(streams[0], TestCacheDataComparer.Instance, out streamIndex)); Assert.Equal(0, streamIndex); Assert.Equal(0, (block.GetSequenceToken(streamIndex, dataAdapter) as EventSequenceToken).SequenceNumber); // find stream1 messages int iteration = 1; while (block.TryFindNextMessage(streamIndex + 1, streams[0], TestCacheDataComparer.Instance, out streamIndex)) { Assert.Equal(iteration * 2, streamIndex); Assert.Equal(iteration * 4, (block.GetSequenceToken(streamIndex, dataAdapter) as EventSequenceToken).SequenceNumber); iteration++; } Assert.Equal(iteration, TestBlockSize / 2); // get index of first stream Assert.True(block.TryFindFirstMessage(streams[1], TestCacheDataComparer.Instance, out streamIndex)); Assert.Equal(1, streamIndex); Assert.Equal(2, (block.GetSequenceToken(streamIndex, dataAdapter) as EventSequenceToken).SequenceNumber); // find stream1 messages iteration = 1; while (block.TryFindNextMessage(streamIndex + 1, streams[1], TestCacheDataComparer.Instance, out streamIndex)) { Assert.Equal(iteration * 2 + 1, streamIndex); Assert.Equal(iteration * 4 + 2, (block.GetSequenceToken(streamIndex, dataAdapter) as EventSequenceToken).SequenceNumber); iteration++; } Assert.Equal(iteration, TestBlockSize / 2); }
private void AddAndCheck(CachedMessageBlock <TestCachedMessage> block, ICacheDataAdapter <TestQueueMessage, TestCachedMessage> dataAdapter, TestQueueMessage message, int first, int last) { Assert.Equal(first, block.OldestMessageIndex); Assert.Equal(last, block.NewestMessageIndex); Assert.True(block.HasCapacity); block.Add(message, DateTime.UtcNow, dataAdapter); last++; Assert.Equal(first > last, block.IsEmpty); Assert.Equal(last + 1 < TestBlockSize, block.HasCapacity); Assert.Equal(first, block.OldestMessageIndex); Assert.Equal(last, block.NewestMessageIndex); Assert.True(block.GetSequenceToken(last, dataAdapter).Equals(message.SequenceToken)); }
private void AddAndCheck(CachedMessageBlock <TestQueueMessage, TestCachedMessage> block, TestQueueMessage message, int first, int last) { Assert.AreEqual(first, block.OldestMessageIndex); Assert.AreEqual(last, block.NewestMessageIndex); Assert.IsTrue(block.HasCapacity); block.Add(message); last++; Assert.AreEqual(first > last, block.IsEmpty); Assert.AreEqual(last + 1 < TestBlockSize, block.HasCapacity); Assert.AreEqual(first, block.OldestMessageIndex); Assert.AreEqual(last, block.NewestMessageIndex); Assert.IsTrue(block.GetSequenceToken(last).Equals(message.SequenceToken)); }