public override void MaxFastWritableBytes()
        {
            IByteBuffer buffer = this.NewBuffer(150, 500).SetWriterIndex(100);

            Assert.Equal(50, buffer.WritableBytes);
            Assert.Equal(150, buffer.Capacity);
            Assert.Equal(500, buffer.MaxCapacity);
            Assert.Equal(400, buffer.MaxWritableBytes);

            int chunkSize = PooledByteBuf(buffer).MaxLength;

            Assert.True(chunkSize >= 150);
            int remainingInAlloc = Math.Min(chunkSize - 100, 400);

            Assert.Equal(remainingInAlloc, buffer.MaxFastWritableBytes);

            // write up to max, chunk alloc should not change (same handle)
            long handleBefore = PooledByteBuf(buffer).Handle;

            buffer.WriteBytes(new byte[remainingInAlloc]);
            Assert.Equal(handleBefore, PooledByteBuf(buffer).Handle);

            Assert.Equal(0, buffer.MaxFastWritableBytes);
            // writing one more should trigger a reallocation (new handle)
            buffer.WriteByte(7);
            Assert.NotEqual(handleBefore, PooledByteBuf(buffer).Handle);

            // should not exceed maxCapacity even if chunk alloc does
            buffer.AdjustCapacity(500);
            Assert.Equal(500 - buffer.WriterIndex, buffer.MaxFastWritableBytes);
            buffer.Release();
        }
Exemplo n.º 2
0
        public void IncreaseCapacityWrapped()
        {
            IByteBuffer buffer  = this.NewBuffer(8);
            IByteBuffer wrapped = buffer.Unwrap();

            wrapped.WriteByte(0);
            wrapped.SetReaderIndex(wrapped.ReaderIndex + 1);
            buffer.SetWriterIndex(buffer.WriterIndex + 1);
            wrapped.AdjustCapacity(wrapped.Capacity * 2);

            Assert.Equal((byte)0, buffer.ReadByte());
        }
Exemplo n.º 3
0
        public void UsedHeapMemory()
        {
            IByteBufferAllocator       allocator = this.NewAllocator(true);
            IByteBufferAllocatorMetric metric    = ((IByteBufferAllocatorMetricProvider)allocator).Metric;

            Assert.Equal(0, metric.UsedHeapMemory);
            IByteBuffer buffer   = allocator.HeapBuffer(1024, 4096);
            int         capacity = buffer.Capacity;

            Assert.Equal(this.ExpectedUsedMemory(allocator, capacity), metric.UsedHeapMemory);

            // Double the size of the buffer
            buffer.AdjustCapacity(capacity << 1);
            capacity = buffer.Capacity;
            Assert.Equal(this.ExpectedUsedMemory(allocator, capacity), metric.UsedHeapMemory);

            buffer.Release();
            Assert.Equal(this.ExpectedUsedMemoryAfterRelease(allocator, capacity), metric.UsedHeapMemory);
        }
        protected override IByteBuffer NewBuffer(int length, int maxCapacity)
        {
            this.AssumedMaxCapacity = maxCapacity == int.MaxValue;

            var buffers = new List <IByteBuffer>();

            for (int i = 0; i < length + 45; i += 45)
            {
                buffers.Add(Unpooled.Empty);
                buffers.Add(Unpooled.WrappedBuffer(new byte[1]));
                buffers.Add(Unpooled.Empty);
                buffers.Add(Unpooled.WrappedBuffer(new byte[2]));
                buffers.Add(Unpooled.Empty);
                buffers.Add(Unpooled.WrappedBuffer(new byte[3]));
                buffers.Add(Unpooled.Empty);
                buffers.Add(Unpooled.WrappedBuffer(new byte[4]));
                buffers.Add(Unpooled.Empty);
                buffers.Add(Unpooled.WrappedBuffer(new byte[5]));
                buffers.Add(Unpooled.Empty);
                buffers.Add(Unpooled.WrappedBuffer(new byte[6]));
                buffers.Add(Unpooled.Empty);
                buffers.Add(Unpooled.WrappedBuffer(new byte[7]));
                buffers.Add(Unpooled.Empty);
                buffers.Add(Unpooled.WrappedBuffer(new byte[8]));
                buffers.Add(Unpooled.Empty);
                buffers.Add(Unpooled.WrappedBuffer(new byte[9]));
                buffers.Add(Unpooled.Empty);
            }

            IByteBuffer buffer = Unpooled.WrappedBuffer(int.MaxValue, buffers.ToArray());

            // Truncate to the requested capacity.
            buffer.AdjustCapacity(length);

            Assert.Equal(length, buffer.Capacity);
            Assert.Equal(length, buffer.ReadableBytes);
            Assert.False(buffer.IsWritable());
            buffer.SetWriterIndex(0);
            return(buffer);
        }
Exemplo n.º 5
0
 public virtual IByteBuffer AdjustCapacity(int newCapacity)
 {
     _ = Buf.AdjustCapacity(newCapacity);
     return(this);
 }