/**
   * Basic fail-back test.
   *
   * @throws Exception
   */
  @Test
  public void testFailBack() throws Exception {
    createSessionFactory();
    ClientSession session = sendAndConsume(sf, true);

    ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);

    sendMessages(session, producer, NUM_MESSAGES);
    session.commit();

    crash(session);

    session.start();
    ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
    receiveMessages(consumer, 0, NUM_MESSAGES, true);
    producer = session.createProducer(FailoverTestBase.ADDRESS);
    sendMessages(session, producer, 2 * NUM_MESSAGES);
    session.commit();
    assertFalse("must NOT be a backup", liveServer.getServer().getConfiguration().isBackup());
    adaptLiveConfigForReplicatedFailBack(liveServer.getServer().getConfiguration());

    CountDownSessionFailureListener listener = new CountDownSessionFailureListener();
    session.addFailureListener(listener);

    liveServer.start();

    assertTrue(listener.getLatch().await(5, TimeUnit.SECONDS));
    assertTrue(
        "live initialized after restart",
        liveServer.getServer().waitForActivation(15, TimeUnit.SECONDS));

    session.start();
    receiveMessages(consumer, 0, NUM_MESSAGES, true);
  }
  @Test
  public void testAutoFailbackThenFailover() throws Exception {
    createSessionFactory();
    ClientSession session = sendAndConsume(sf, true);

    CountDownSessionFailureListener listener = new CountDownSessionFailureListener();

    session.addFailureListener(listener);

    liveServer.crash(session);

    ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);

    ClientMessage message = session.createMessage(true);

    setBody(0, message);

    producer.send(message);

    session.removeFailureListener(listener);

    listener = new CountDownSessionFailureListener();

    session.addFailureListener(listener);

    log.info("restarting live node now");
    liveServer.start();

    assertTrue("expected a session failure", listener.getLatch().await(5, TimeUnit.SECONDS));

    message = session.createMessage(true);

    setBody(1, message);

    producer.send(message);

    session.removeFailureListener(listener);

    listener = new CountDownSessionFailureListener();

    session.addFailureListener(listener);

    waitForBackup(sf, 10);

    liveServer.crash();

    assertTrue("expected a session failure", listener.getLatch().await(5, TimeUnit.SECONDS));

    session.close();

    wrapUpSessionFactory();
  }