@Test
  public void testSetKeyRequestResponse() throws Exception {
    commandProducer.write(channel, Command.SET_KEY_CMD);
    setKeyRequestProducer.write(channel, new IndexedJournalKey(0, quoteWriter.getKey()));
    agent.process(channel);
    charSequenceResponseConsumer.read(channel);
    TestUtils.assertEquals("OK", charSequenceResponseConsumer.getValue());
    hugeBufferConsumer.read(channel);

    commandProducer.write(channel, Command.SET_KEY_CMD);
    setKeyRequestProducer.write(channel, new IndexedJournalKey(0, tradeWriter.getKey()));
    agent.process(channel);
    charSequenceResponseConsumer.read(channel);
    TestUtils.assertEquals(
        "Requested key not exported: JournalKey{id=com.nfsdb.model.Trade, location='null', partitionType=DEFAULT, recordHint=0, ordered=true}",
        charSequenceResponseConsumer.getValue());
  }
  @Test
  public void testJournalIndexCorrectness() throws Exception {
    server.publish(tradeWriter);
    server.start();

    Journal<Quote> quoteClientWriter = factory.writer(Quote.class, "client");

    // send quote journal key
    //        commandProducer.write(channel, Command.SET_KEY_CMD);
    //        setKeyRequestProducer.write(channel, new IndexedJournalKey(3, quoteWriter.getKey()));
    //        agent.process(channel);
    //        charSequenceResponseConsumer.reset();
    //        charSequenceResponseConsumer.read(channel);
    //        Assert.assertTrue(charSequenceResponseConsumer.isComplete());
    //        Assert.assertEquals("Journal index is too large. Max 1",
    // charSequenceResponseConsumer.getValue());

    commandProducer.write(channel, Command.SET_KEY_CMD);
    setKeyRequestProducer.write(channel, new IndexedJournalKey(0, quoteWriter.getKey()));
    agent.process(channel);
    charSequenceResponseConsumer.read(channel);
    TestUtils.assertEquals("OK", charSequenceResponseConsumer.getValue());
    hugeBufferConsumer.read(channel);

    commandProducer.write(channel, Command.DELTA_REQUEST_CMD);
    journalClientStateProducer.write(channel, new IndexedJournal(1, quoteClientWriter));
    agent.process(channel);
    charSequenceResponseConsumer.read(channel);
    TestUtils.assertEquals(
        "Journal index does not match key request", charSequenceResponseConsumer.getValue());

    commandProducer.write(channel, Command.DELTA_REQUEST_CMD);
    journalClientStateProducer.write(channel, new IndexedJournal(0, quoteClientWriter));
    agent.process(channel);
    charSequenceResponseConsumer.read(channel);
    TestUtils.assertEquals("OK", charSequenceResponseConsumer.getValue());

    server.halt();
  }
  @Test
  public void testIncrementalInteraction() throws Exception {
    JournalWriter<Quote> origin = factory.writer(Quote.class, "origin");
    TestUtils.generateQuoteData(origin, 200);

    server.start();
    JournalWriter<Quote> quoteClientWriter = factory.writer(Quote.class, "client");

    JournalDeltaConsumer quoteDeltaConsumer = new JournalDeltaConsumer(quoteClientWriter);

    // send quote journal key
    commandProducer.write(channel, Command.SET_KEY_CMD);
    setKeyRequestProducer.write(channel, new IndexedJournalKey(0, quoteWriter.getKey()));
    agent.process(channel);
    charSequenceResponseConsumer.read(channel);
    TestUtils.assertEquals("OK", charSequenceResponseConsumer.getValue());
    hugeBufferConsumer.read(channel);

    // send quote state
    commandProducer.write(channel, Command.DELTA_REQUEST_CMD);
    journalClientStateProducer.write(channel, new IndexedJournal(0, quoteClientWriter));
    agent.process(channel);
    charSequenceResponseConsumer.read(channel);
    TestUtils.assertEquals("OK", charSequenceResponseConsumer.getValue());

    quoteWriter.append(origin.query().all().asResultSet().subset(0, 100));
    quoteWriter.commit();

    commandProducer.write(channel, Command.CLIENT_READY_CMD);
    agent.process(channel);

    commandConsumer.read(channel);
    Assert.assertEquals(Command.JOURNAL_DELTA_CMD, commandConsumer.getValue());

    Assert.assertEquals(0, intResponseConsumer.getValue(channel));
    quoteDeltaConsumer.read(channel);
    Assert.assertEquals(100, quoteClientWriter.size());

    commandConsumer.read(channel);
    Assert.assertEquals(Command.SERVER_READY_CMD, commandConsumer.getValue());

    quoteWriter.append(origin.query().all().asResultSet().subset(100, 200));
    quoteWriter.commit();

    // send quote state
    commandProducer.write(channel, Command.DELTA_REQUEST_CMD);
    journalClientStateProducer.write(channel, new IndexedJournal(0, quoteClientWriter));
    agent.process(channel);
    charSequenceResponseConsumer.read(channel);
    TestUtils.assertEquals("OK", charSequenceResponseConsumer.getValue());

    commandProducer.write(channel, Command.CLIENT_READY_CMD);
    agent.process(channel);

    commandConsumer.read(channel);
    Assert.assertEquals(Command.JOURNAL_DELTA_CMD, commandConsumer.getValue());

    Assert.assertEquals(0, intResponseConsumer.getValue(channel));
    quoteDeltaConsumer.read(channel);
    Assert.assertEquals(200, quoteClientWriter.size());

    commandConsumer.read(channel);
    Assert.assertEquals(Command.SERVER_READY_CMD, commandConsumer.getValue());

    server.halt();
  }