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

  assumeTrue(NativeIO.isAvailable());
  assumeTrue(Shell.LINUX);
  final File TEST_FILE = new File(new File(
      System.getProperty("test.build.data","build/test/data")),
      "testMlockFile");
  final int BUF_LEN = 12289;
  byte buf[] = new byte[BUF_LEN];
  int bufSum = 0;
  for (int i = 0; i < buf.length; i++) {
    buf[i] = (byte)(i % 60);
    bufSum += buf[i];
  }
  FileOutputStream fos = new FileOutputStream(TEST_FILE);
  try {
    fos.write(buf);
    fos.getChannel().force(true);
  } finally {
    fos.close();
  }
  
  FileInputStream fis = null;
  FileChannel channel = null;
  try {
    // Map file into memory
    fis = new FileInputStream(TEST_FILE);
    channel = fis.getChannel();
    long fileSize = channel.size();
    MappedByteBuffer mapbuf = channel.map(MapMode.READ_ONLY, 0, fileSize);
    // mlock the buffer
    NativeIO.POSIX.mlock(mapbuf, fileSize);
    // Read the buffer
    int sum = 0;
    for (int i=0; i<fileSize; i++) {
      sum += mapbuf.get(i);
    }
    assertEquals("Expected sums to be equal", bufSum, sum);
    // munlock the buffer
    NativeIO.POSIX.munlock(mapbuf, fileSize);
  } finally {
    if (channel != null) {
      channel.close();
    }
    if (fis != null) {
      fis.close();
    }
  }
}