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 testDroppingMessagesWhileSending() throws Exception {
    assertEquals(0, STATS.getSent());
    assertEquals(0, STATS.getSentDropped());

    Message m[] = new Message[10];
    for (int i = 0; i < m.length; i++) m[i] = g(i + 1);

    // Set queue to drop msgs (5 of'm) after the 3rd is added.
    for (int i = 0; i < m.length; i++) WRITER.send(m[i]);
    assertEquals(0, STATS.getSentDropped());
    assertEquals(10, STATS.getSent());

    QUEUE.setNumToDrop(4);
    QUEUE.setStartDropIn(3);
    assertFalse(WRITER.handleWrite());
    assertEquals(4, STATS.getSentDropped());

    ByteBuffer buffer = SINK.getBuffer();
    Message in[] = read(buffer, 6);
    assertFalse(buffer.hasRemaining());
    assertEquals(6, SENT.size());
    assertEquals(buffer(m[0]), buffer(in[0]));
    assertEquals(buffer(m[1]), buffer(in[1]));
    assertEquals(buffer(m[2]), buffer(in[2])); // started dropping now.
    assertEquals(buffer(m[7]), buffer(in[3])); // finished dropping here.
    assertEquals(buffer(m[8]), buffer(in[4]));
    assertEquals(buffer(m[9]), buffer(in[5]));
  }
  /** {@inheritDoc} */
  @Override
  public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
    writer.setBuffer(buf);

    if (!super.writeTo(buf, writer)) return false;

    if (!writer.isHeaderWritten()) {
      if (!writer.writeHeader(directType(), fieldsCount())) return false;

      writer.onHeaderWritten();
    }

    switch (writer.state()) {
      case 0:
        if (!writer.writeIgniteUuid("fileId", fileId)) return false;

        writer.incrementState();

      case 1:
        if (!writer.writeCollection(
            "fragmentRanges", fragmentRanges, MessageCollectionItemType.MSG)) return false;

        writer.incrementState();
    }

    return true;
  }
  public void testSimpleWrite() throws Exception {
    Message one, two, three;
    one = q("query one");
    two = g(7123);
    three = s(8134);

    assertEquals(0, STATS.getSent());
    assertFalse(SINK.interested());

    WRITER.send(one);
    WRITER.send(two);
    WRITER.send(three);
    assertEquals(3, STATS.getSent());

    assertTrue(SINK.interested());

    assertEquals(0, SENT.size());

    assertFalse(WRITER.handleWrite()); // nothing left to write.
    assertEquals(3, SENT.size());

    ByteBuffer buffer = SINK.getBuffer();
    assertEquals(
        one.getTotalLength() + two.getTotalLength() + three.getTotalLength(), buffer.limit());
    ByteArrayInputStream in = new ByteArrayInputStream(buffer.array(), 0, buffer.limit());
    Message in1, in2, in3;
    in1 = read(in);
    in2 = read(in);
    in3 = read(in);
    assertEquals(-1, in.read());

    assertEquals(buffer(one), buffer(in1));
    assertEquals(buffer(two), buffer(in2));
    assertEquals(buffer(three), buffer(in3));
    assertEquals(buffer(one), buffer(SENT.next()));
    assertEquals(buffer(two), buffer(SENT.next()));
    assertEquals(buffer(three), buffer(SENT.next()));

    assertFalse(SINK.interested());
    assertEquals(3, STATS.getSent());
  }
  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));
  }
  /** {@inheritDoc} */
  @Override
  public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
    writer.setBuffer(buf);

    if (!writer.isHeaderWritten()) {
      if (!writer.writeHeader(directType(), fieldsCount())) return false;

      writer.onHeaderWritten();
    }

    switch (writer.state()) {
      case 0:
        if (!writer.writeLong("topVer", topVer)) return false;

        writer.incrementState();

      case 1:
        if (!writer.writeLong("ver", ver)) return false;

        writer.incrementState();
    }

    return true;
  }
  /** {@inheritDoc} */
  @Override
  public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
    writer.setBuffer(buf);

    if (!super.writeTo(buf, writer)) return false;

    if (!writer.isHeaderWritten()) {
      if (!writer.writeHeader(directType(), fieldsCount())) return false;

      writer.onHeaderWritten();
    }

    switch (writer.state()) {
      case 10:
        if (!writer.writeMessage("dhtVer", dhtVer)) return false;

        writer.incrementState();

      case 11:
        if (!writer.writeCollection(
            "filterFailedKeys", filterFailedKeys, MessageCollectionItemType.MSG)) return false;

        writer.incrementState();

      case 12:
        if (!writer.writeIgniteUuid("futId", futId)) return false;

        writer.incrementState();

      case 13:
        if (!writer.writeCollection("invalidParts", invalidParts, MessageCollectionItemType.INT))
          return false;

        writer.incrementState();

      case 14:
        if (!writer.writeIgniteUuid("miniId", miniId)) return false;

        writer.incrementState();

      case 15:
        if (!writer.writeCollection("ownedValKeys", ownedValKeys, MessageCollectionItemType.MSG))
          return false;

        writer.incrementState();

      case 16:
        if (!writer.writeCollection("ownedValVals", ownedValVals, MessageCollectionItemType.MSG))
          return false;

        writer.incrementState();

      case 17:
        if (!writer.writeCollection("pending", pending, MessageCollectionItemType.MSG))
          return false;

        writer.incrementState();

      case 18:
        if (!writer.writeMessage("retVal", retVal)) return false;

        writer.incrementState();
    }

    return true;
  }
  /** {@inheritDoc} */
  @Override
  public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
    writer.setBuffer(buf);

    if (!writer.isHeaderWritten()) {
      if (!writer.writeHeader(directType(), fieldsCount())) return false;

      writer.onHeaderWritten();
    }

    switch (writer.state()) {
      case 0:
        if (!writer.writeString("cpSpi", cpSpi)) return false;

        writer.incrementState();

      case 1:
        if (!writer.writeString("key", key)) return false;

        writer.incrementState();

      case 2:
        if (!writer.writeIgniteUuid("sesId", sesId)) return false;

        writer.incrementState();
    }

    return true;
  }
  /** {@inheritDoc} */
  @Override
  public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
    writer.setBuffer(buf);

    if (!super.writeTo(buf, writer)) return false;

    if (!writer.isHeaderWritten()) {
      if (!writer.writeHeader(directType(), fieldsCount())) return false;

      writer.onHeaderWritten();
    }

    switch (writer.state()) {
      case 3:
        if (!writer.writeMessage("conflictExpireTimes", conflictExpireTimes)) return false;

        writer.incrementState();

      case 4:
        if (!writer.writeMessage("conflictTtls", conflictTtls)) return false;

        writer.incrementState();

      case 5:
        if (!writer.writeCollection("conflictVers", conflictVers, MessageCollectionItemType.MSG))
          return false;

        writer.incrementState();

      case 6:
        if (!writer.writeCollection(
            "entryProcessorsBytes", entryProcessorsBytes, MessageCollectionItemType.BYTE_ARR))
          return false;

        writer.incrementState();

      case 7:
        if (!writer.writeByteArray("expiryPlcBytes", expiryPlcBytes)) return false;

        writer.incrementState();

      case 8:
        if (!writer.writeBoolean("fastMap", fastMap)) return false;

        writer.incrementState();

      case 9:
        if (!writer.writeObjectArray("filter", filter, MessageCollectionItemType.MSG)) return false;

        writer.incrementState();

      case 10:
        if (!writer.writeMessage("futVer", futVer)) return false;

        writer.incrementState();

      case 11:
        if (!writer.writeBoolean("hasPrimary", hasPrimary)) return false;

        writer.incrementState();

      case 12:
        if (!writer.writeObjectArray(
            "invokeArgsBytes", invokeArgsBytes, MessageCollectionItemType.BYTE_ARR)) return false;

        writer.incrementState();

      case 13:
        if (!writer.writeCollection("keys", keys, MessageCollectionItemType.MSG)) return false;

        writer.incrementState();

      case 14:
        if (!writer.writeByte("op", op != null ? (byte) op.ordinal() : -1)) return false;

        writer.incrementState();

      case 15:
        if (!writer.writeBoolean("retval", retval)) return false;

        writer.incrementState();

      case 16:
        if (!writer.writeBoolean("skipStore", skipStore)) return false;

        writer.incrementState();

      case 17:
        if (!writer.writeUuid("subjId", subjId)) return false;

        writer.incrementState();

      case 18:
        if (!writer.writeByte("syncMode", syncMode != null ? (byte) syncMode.ordinal() : -1))
          return false;

        writer.incrementState();

      case 19:
        if (!writer.writeInt("taskNameHash", taskNameHash)) return false;

        writer.incrementState();

      case 20:
        if (!writer.writeMessage("topVer", topVer)) return false;

        writer.incrementState();

      case 21:
        if (!writer.writeMessage("updateVer", updateVer)) return false;

        writer.incrementState();

      case 22:
        if (!writer.writeCollection("vals", vals, MessageCollectionItemType.MSG)) return false;

        writer.incrementState();
    }

    return true;
  }
  /** {@inheritDoc} */
  @Override
  public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
    writer.setBuffer(buf);

    if (!super.writeTo(buf, writer)) return false;

    if (!writer.isHeaderWritten()) {
      if (!writer.writeHeader(directType(), fieldsCount())) return false;

      writer.onHeaderWritten();
    }

    switch (writer.state()) {
      case 8:
        if (!writer.writeByte(
            "concurrency", concurrency != null ? (byte) concurrency.ordinal() : -1)) return false;

        writer.incrementState();

      case 9:
        if (!writer.writeCollection("dhtVerKeys", dhtVerKeys, MessageCollectionItemType.MSG))
          return false;

        writer.incrementState();

      case 10:
        if (!writer.writeCollection("dhtVerVals", dhtVerVals, MessageCollectionItemType.MSG))
          return false;

        writer.incrementState();

      case 11:
        if (!writer.writeByteArray("grpLockKeyBytes", grpLockKeyBytes)) return false;

        writer.incrementState();

      case 12:
        if (!writer.writeBoolean("invalidate", invalidate)) return false;

        writer.incrementState();

      case 13:
        if (!writer.writeByte("isolation", isolation != null ? (byte) isolation.ordinal() : -1))
          return false;

        writer.incrementState();

      case 14:
        if (!writer.writeBoolean("onePhaseCommit", onePhaseCommit)) return false;

        writer.incrementState();

      case 15:
        if (!writer.writeBoolean("partLock", partLock)) return false;

        writer.incrementState();

      case 16:
        if (!writer.writeByte("plc", plc != null ? (byte) plc.ordinal() : -1)) return false;

        writer.incrementState();

      case 17:
        if (!writer.writeCollection("reads", reads, MessageCollectionItemType.MSG)) return false;

        writer.incrementState();

      case 18:
        if (!writer.writeBoolean("sys", sys)) return false;

        writer.incrementState();

      case 19:
        if (!writer.writeLong("threadId", threadId)) return false;

        writer.incrementState();

      case 20:
        if (!writer.writeLong("timeout", timeout)) return false;

        writer.incrementState();

      case 21:
        if (!writer.writeByteArray("txNodesBytes", txNodesBytes)) return false;

        writer.incrementState();

      case 22:
        if (!writer.writeInt("txSize", txSize)) return false;

        writer.incrementState();

      case 23:
        if (!writer.writeMessage("writeVer", writeVer)) return false;

        writer.incrementState();

      case 24:
        if (!writer.writeCollection("writes", writes, MessageCollectionItemType.MSG)) return false;

        writer.incrementState();
    }

    return true;
  }