示例#1
0
        /// <summary>
        /// Pooled cache for generator stream provider
        /// </summary>
        /// <param name="bufferPool"></param>
        /// <param name="logger"></param>
        /// <param name="serializationManager"></param>
        public GeneratorPooledCache(IObjectPool <FixedSizeBuffer> bufferPool, Logger logger, SerializationManager serializationManager)
        {
            var dataAdapter = new CacheDataAdapter(bufferPool, serializationManager);

            cache = new PooledQueueCache <GeneratedBatchContainer, CachedMessage>(dataAdapter, CacheDataComparer.Instance, logger);
            var evictionStrategy = new ExplicitEvictionStrategy();

            evictionStrategy.PurgeObservable = cache;
            dataAdapter.OnBlockAllocated     = evictionStrategy.OnBlockAllocated;
        }
示例#2
0
        /// <summary>
        /// Pooled cache for memory stream provider
        /// </summary>
        /// <param name="bufferPool"></param>
        /// <param name="logger"></param>
        /// <param name="serializer"></param>
        public MemoryPooledCache(IObjectPool <FixedSizeBuffer> bufferPool, Logger logger, TSerializer serializer)
        {
            var dataAdapter = new CacheDataAdapter(bufferPool, serializer);

            cache = new PooledQueueCache <MemoryMessageData, MemoryMessageData>(dataAdapter, CacheDataComparer.Instance, logger);
            var evictionStrategy = new ExplicitEvictionStrategy();

            evictionStrategy.PurgeObservable = cache;
            dataAdapter.OnBlockAllocated     = evictionStrategy.OnBlockAllocated;
        }
        public void GoldenPathTest()
        {
            var bufferPool       = new TestBlockPool();
            var dataAdapter      = new TestCacheDataAdapter(bufferPool);
            var cache            = new PooledQueueCache <TestQueueMessage, TestCachedMessage>(dataAdapter, TestCacheDataComparer.Instance, NoOpTestLogger.Instance);
            var evictionStrategy = new ExplicitEvictionStrategy();

            evictionStrategy.PurgeObservable = cache;
            dataAdapter.OnBlockAllocated     = evictionStrategy.OnBlockAllocated;

            RunGoldenPath(cache, 111);
        }
        public void QueueCacheMissTest()
        {
            var bufferPool       = new TestBlockPool();
            var dataAdapter      = new TestCacheDataAdapter(bufferPool);
            var cache            = new PooledQueueCache <TestQueueMessage, TestCachedMessage>(dataAdapter, TestCacheDataComparer.Instance, NoOpTestLogger.Instance);
            var evictionStrategy = new ExplicitEvictionStrategy();

            evictionStrategy.PurgeObservable = cache;
            dataAdapter.OnBlockAllocated     = evictionStrategy.OnBlockAllocated;

            int             sequenceNumber = 10;
            IBatchContainer batch;

            IStreamIdentity streamId = new StreamIdentity(Guid.NewGuid(), TestStreamNamespace);

            // No data in cache, cursors should not throw.
            object cursor = cache.GetCursor(streamId, new EventSequenceTokenV2(sequenceNumber++));

            Assert.NotNull(cursor);

            // try to iterate, should throw
            bool gotNext = cache.TryGetNextMessage(cursor, out batch);

            Assert.NotNull(cursor);
            Assert.False(gotNext);

            // now add messages into cache newer than cursor
            // Adding enough to fill the pool
            for (int i = 0; i < MessagesPerBuffer * PooledBufferCount; i++)
            {
                cache.Add(new TestQueueMessage
                {
                    StreamGuid      = streamId.Guid,
                    StreamNamespace = TestStreamNamespace,
                    SequenceNumber  = sequenceNumber++,
                }, DateTime.UtcNow);
            }

            // now that there is data, and the cursor should point to data older than in the cache, using cursor should throw
            Exception ex = null;

            try
            {
                cache.TryGetNextMessage(cursor, out batch);
            }
            catch (QueueCacheMissException cacheMissException)
            {
                ex = cacheMissException;
            }
            Assert.NotNull(ex);

            // Try getting new cursor into cache from data before the cache.  Should throw
            ex = null;
            try
            {
                cache.GetCursor(streamId, new EventSequenceTokenV2(10));
            }
            catch (QueueCacheMissException cacheMissException)
            {
                ex = cacheMissException;
            }
            Assert.NotNull(ex);

            // Get valid cursor into cache
            cursor = cache.GetCursor(streamId, new EventSequenceTokenV2(13));
            // query once, to make sure cursor is good
            gotNext = cache.TryGetNextMessage(cursor, out batch);
            Assert.NotNull(cursor);
            Assert.True(gotNext);
            // Since pool should be full, adding one more message should trigger the cache to purge.
            cache.Add(new TestQueueMessage
            {
                StreamGuid      = streamId.Guid,
                StreamNamespace = TestStreamNamespace,
                SequenceNumber  = sequenceNumber,
            }, DateTime.UtcNow);
            // After purge, use of cursor should throw.
            ex = null;
            try
            {
                cache.TryGetNextMessage(cursor, out batch);
            }
            catch (QueueCacheMissException cacheMissException)
            {
                ex = cacheMissException;
            }
            Assert.NotNull(ex);
        }