public void StreamCanHoldLongData()
        {
            const long dataSize            = 4000L * Constants.MB;
            const int  bufferPartitionSize = 512 * Constants.MB;
            var        originalStream      = new PredictableStream();
            var        arrayPoolStream     = PooledMemoryStream.BufferStreamPartitionInternal(originalStream, dataSize, dataSize, 0, _pool, bufferPartitionSize, false, default).EnsureCompleted();

            originalStream.Position = 0;


            // assert it holds the correct amount of data. other tests assert data validity and it's so expensive to do that here.
            // test without blowing up memory
            const int testSize           = 256 * Constants.MB;
            var       pooledStreamBuffer = new byte[testSize];
            long      totalRead          = 0;
            int       read;

            do
            {
                // both these streams are backed in memory and will always read what is asked until the pooled stream hits the end
                read       = arrayPoolStream.Read(pooledStreamBuffer, 0, testSize);
                totalRead += read;
            } while (read != 0);

            Assert.AreEqual(dataSize, totalRead);
        }
Пример #2
0
        [TestCase(Constants.KB + 11, 256)]         // content doesn't line up with buffers (extremely unlikely any array pool implementation will add exactly 11 bytes more than requested across 4 buffers)
        public async Task ReadStream(int dataSize, int bufferPartitionSize)
        {
            PredictableStream  originalStream  = new PredictableStream();
            PooledMemoryStream arrayPoolStream = await PooledMemoryStream.BufferStreamPartitionInternal(originalStream, dataSize, dataSize, 0, _pool, bufferPartitionSize, true, default);

            originalStream.Position = 0;

            byte[] originalStreamData = new byte[dataSize];
            byte[] poolStreamData     = new byte[dataSize];
            originalStream.Read(originalStreamData, 0, dataSize);
            arrayPoolStream.Read(poolStreamData, 0, dataSize);

            CollectionAssert.AreEqual(originalStreamData, poolStreamData);
        }
        [TestCase(Constants.KB + 11, 256)]         // content doesn't line up with buffers (extremely unlikely any array pool implementation will add exactly 11 bytes more than requested across 4 buffers)
        public void ReadStream(int dataSize, int bufferPartitionSize)
        {
            var originalStream  = new PredictableStream();
            var arrayPoolStream = PooledMemoryStream.BufferStreamPartitionInternal(originalStream, dataSize, dataSize, 0, _pool, bufferPartitionSize, false, default).EnsureCompleted();

            originalStream.Position = 0;

            var originalStreamData = new byte[dataSize];
            var poolStreamData     = new byte[dataSize];

            originalStream.Read(originalStreamData, 0, dataSize);
            arrayPoolStream.Read(poolStreamData, 0, dataSize);

            CollectionAssert.AreEqual(originalStreamData, poolStreamData);
        }
        public void PredictableStreamIsPredictable()
        {
            const int dataSize = 257;

            var buffer            = new byte[dataSize];
            var predictableStream = new PredictableStream();

            predictableStream.Read(buffer, 0, dataSize);
            Assert.AreEqual(dataSize, predictableStream.Position);

            var expected = Enumerable.Range(0, dataSize).Select(val => (byte)(val % byte.MaxValue)).ToArray();

            for (int i = 0; i < dataSize; i++)
            {
                Assert.AreEqual(buffer[i], expected[i]);
            }
        }