@Test(timeout = 10000)
public void write() throws Exception {

    Path tmpFile = tempFile();
    PipeChannel channel = DefaultPipeChannel.open(tmpFile);
    FileChannel ctrlChannel = FileChannel.open(tmpFile, StandardOpenOption.READ, StandardOpenOption.WRITE);
    ByteBuffer bb = ByteBuffer.wrap(new byte[]{1, 2, 3});
    int n = channel.write(bb).get();
    assertThat(n).isEqualTo(3);
    ByteBuffer resultBuffer = ByteBuffer.allocate(5);
    ctrlChannel.read(resultBuffer);
    assertThat(resultBuffer.limit(n)).as("simple write operation").isEqualTo(bb);
    channel.write(ByteBuffer.wrap(new byte[]{4})).get();
    resultBuffer = ByteBuffer.allocate(5);
    n = ctrlChannel.read(resultBuffer, 0);
    assertThat(n).isEqualTo(3);
    assertThat(resultBuffer.array()).as("writing must always occurs at position 0")
            .containsExactly((byte) 4, (byte) 2, (byte) 3, (byte) 0, (byte) 0);
    ctrlChannel.truncate(0);
    bb.position(1);
    n = channel.write(bb).get();
    assertThat(n).isEqualTo(2);
    resultBuffer = ByteBuffer.allocate(5);
    ctrlChannel.read(resultBuffer, 0);
    assertThat(resultBuffer.array()).as("writing with buffer position set")
            .containsExactly((byte) 2, (byte) 3, (byte) 0, (byte) 0, (byte) 0);
    ctrlChannel.truncate(0);
    bb.position(1);
    bb.limit(2);
    n = channel.write(bb).get();
    assertThat(n).isEqualTo(1);
    resultBuffer = ByteBuffer.allocate(5);
    ctrlChannel.read(resultBuffer, 0);
    assertThat(resultBuffer.array()).as("writing with buffer position and limit set")
            .containsExactly((byte) 2, (byte) 0, (byte) 0, (byte) 0, (byte) 0);
}