@Test
public void testAppend() throws IOException {

    File file = new File(dir, "append-from-channel.qdb");
    file.delete();
    MessageFile mf = new MessageFile(file, 1000L, 1000000);
    long ts0 = System.currentTimeMillis();
    String key0 = "foo";
    byte[] payload0 = "piggy".getBytes("UTF8");
    int length0 = 1/*type*/ + 8/*timestamp*/ + 2/*key size*/ + 4/* payload size*/ + key0.length() + payload0.length;
    long ts1 = ts0 + 1;
    String key1 = "foobar";
    byte[] payload1 = "oink".getBytes("UTF8");
    int length1 = 1/*type*/ + 8/*timestamp*/ + 2/*key size*/ + 4/* payload size*/ + key1.length() + payload1.length;
    assertEquals(1000L, mf.append(ts0, key0, toChannel(payload0), payload0.length));
    assertEquals(1000L + length0, mf.append(ts1, key1, toChannel(payload1), payload1.length));
    int expectedLength = 4096/*file header*/ + length0 + length1;
    assertEquals(expectedLength, mf.length());
    mf.close();
    assertEquals(expectedLength, file.length());
    DataInputStream ins = new DataInputStream(new FileInputStream(file));
    assertEquals((short)0xBE01, ins.readShort());   // magic
    assertEquals((short)0, ins.readShort());        // reserved
    assertEquals(1000000, ins.readInt());           // max file size
    assertEquals(expectedLength, ins.readInt());    // checkpoint
    assertEquals(0, ins.readInt());                 // reserved
    assertEquals(0, ins.readInt());                 // bucket first message id (relative to file)
    assertEquals(ts0, ins.readLong());              // bucket timestamp
    assertEquals(2, ins.readInt());                 // bucket count
    for (int i = 16 + 16; i < 4096; i += 16) {
        assertEquals(-1, ins.readInt());
        assertEquals(0L, ins.readLong());
        assertEquals(0, ins.readInt());
    }
    assertEquals((byte)0xA1, ins.readByte());   // type
    assertEquals(ts0, ins.readLong());
    assertEquals(key0.length(), (int)ins.readShort());
    assertEquals(payload0.length, ins.readInt());
    assertEquals(key0, readUTF8(ins, key0.length()));
    assertEquals(new String(payload0, "UTF8"), readUTF8(ins, payload0.length));
    assertEquals((byte)0xA1, ins.readByte());   // type
    assertEquals(ts1, ins.readLong());
    assertEquals(key1.length(), (int)ins.readShort());
    assertEquals(payload1.length, ins.readInt());
    assertEquals(key1, readUTF8(ins, key1.length()));
    assertEquals(new String(payload1, "UTF8"), readUTF8(ins, payload1.length));
    ins.close();
}