netty5/transport-native-unix-common-tests/src/main/java/io/netty/channel/unix/tests/UnixChannelUtilTest.java
2017-06-20 07:01:13 +02:00

87 lines
3.2 KiB
Java

/*
* Copyright 2017 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel.unix.tests;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.UnpooledByteBufAllocator;
import org.junit.Test;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static io.netty.channel.unix.Limits.IOV_MAX;
import static io.netty.channel.unix.UnixChannelUtil.isBufferCopyNeededForWrite;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class UnixChannelUtilTest {
@Test
public void testPooledAllocatorIsBufferCopyNeededForWrite() {
testIsBufferCopyNeededForWrite(PooledByteBufAllocator.DEFAULT);
}
@Test
public void testUnPooledAllocatorIsBufferCopyNeededForWrite() {
testIsBufferCopyNeededForWrite(UnpooledByteBufAllocator.DEFAULT);
}
private static void testIsBufferCopyNeededForWrite(ByteBufAllocator alloc) {
ByteBuf byteBuf = alloc.directBuffer();
assertFalse(isBufferCopyNeededForWrite(byteBuf));
assertFalse(isBufferCopyNeededForWrite(byteBuf.asReadOnly()));
assertTrue(byteBuf.release());
byteBuf = alloc.heapBuffer();
assertTrue(isBufferCopyNeededForWrite(byteBuf));
assertTrue(isBufferCopyNeededForWrite(byteBuf.asReadOnly()));
assertTrue(byteBuf.release());
assertCompositeByteBufIsBufferCopyNeededForWrite(alloc, 2, 0, true);
assertCompositeByteBufIsBufferCopyNeededForWrite(alloc, IOV_MAX + 1, 0, true);
assertCompositeByteBufIsBufferCopyNeededForWrite(alloc, 0, 2, true);
assertCompositeByteBufIsBufferCopyNeededForWrite(alloc, 1, 1, true);
}
private static void assertCompositeByteBufIsBufferCopyNeededForWrite(ByteBufAllocator alloc, int numDirect,
int numHeap, boolean expected) {
CompositeByteBuf comp = alloc.compositeBuffer(numDirect + numHeap);
List<ByteBuf> byteBufs = new LinkedList<ByteBuf>();
while (numDirect > 0) {
byteBufs.add(alloc.directBuffer(1));
numDirect--;
}
while (numHeap > 0) {
byteBufs.add(alloc.heapBuffer(1));
numHeap--;
}
Collections.shuffle(byteBufs);
for (ByteBuf byteBuf : byteBufs) {
comp.addComponent(byteBuf);
}
assertEquals(byteBufs.toString(), expected, isBufferCopyNeededForWrite(comp));
assertTrue(comp.release());
}
}