public void testWritePartialMsg() throws Exception {
    assertEquals(0, SENT.size());
    assertEquals(0, STATS.getSent());

    Message m = q("reaalllllllllly long query");
    SINK.resize(m.getTotalLength() - 20);

    WRITER.send(m);
    assertEquals(1, STATS.getSent());
    assertTrue(WRITER.handleWrite()); // still stuff left to write.
    assertTrue(SINK.interested());
    assertEquals(
        1, SENT.size()); // it's sent, even though the other side didn't receive it fully yet.
    assertEquals(buffer(m), buffer(SENT.next()));

    ByteBuffer buffer = ByteBuffer.allocate(m.getTotalLength());
    buffer.put(SINK.getBuffer());
    SINK.resize(100000);

    assertFalse(WRITER.handleWrite());
    assertFalse(SINK.interested());
    buffer.put(SINK.getBuffer());
    Message in = read((ByteBuffer) buffer.flip());
    assertEquals(buffer(m), buffer(in));
  }
  public void testWritePartialAndMore() throws Exception {
    Message out1 = q("first long query");
    Message out2 = q("second long query");
    Message out3 = q("third long query");
    assertEquals(0, STATS.getSent());

    SINK.resize(out1.getTotalLength() + 20);
    WRITER.send(out1);
    WRITER.send(out2);
    assertEquals(2, STATS.getSent());

    assertEquals(0, SENT.size());
    assertTrue(WRITER.handleWrite());
    assertTrue(SINK.interested());
    assertEquals(2, SENT.size()); // two were sent, one was received.
    assertEquals(buffer(out1), buffer(SENT.next()));
    assertEquals(buffer(out2), buffer(SENT.next()));

    ByteBuffer buffer = ByteBuffer.allocate(1000);
    buffer.put(SINK.getBuffer()).flip();
    SINK.resize(10000);

    Message in1 = read(buffer);
    assertTrue(buffer.hasRemaining());
    assertEquals(20, buffer.remaining());
    buffer.compact();

    WRITER.send(out3);
    assertEquals(3, STATS.getSent());
    assertFalse(WRITER.handleWrite());
    assertEquals(1, SENT.size());
    assertEquals(buffer(out3), buffer(SENT.next()));
    assertFalse(SINK.interested());
    buffer.put(SINK.getBuffer()).flip();

    Message in2 = read(buffer);
    Message in3 = read(buffer);
    assertTrue(!buffer.hasRemaining());
    assertEquals(buffer(out2), buffer(in2));
    assertEquals(buffer(out3), buffer(in3));
  }