// test messages are correctly bridged when failover happens during a batch send.
  // first send some messages, make sure bridge doesn't send it (below batch size)
  // then crash the live
  // then send more messages
  // then receive those messages, no more, no less.
  // this test are valid for ONCE_AND_ONLY_ONCE and AT_MOST_ONCE.
  // with DUPS_OK the test failed because some messages are delivered again
  // after failover, which is fine as in this mode duplication is allowed.
  public void performSourceAndTargetCrashAndFailoverWithMessages(QualityOfServiceMode mode)
      throws Exception {
    JMSBridgeImpl bridge = null;
    TransactionManager txMgr = null;

    try {
      ConnectionFactoryFactory sourceCFF = sourceServer.getConnectionFactoryFactory();
      ConnectionFactoryFactory targetCFF = targetServer.getConnectionFactoryFactory();
      DestinationFactory sourceQueueFactory = sourceServer.getDestinationFactory(sourceQueueName);
      DestinationFactory targetQueueFactory = targetServer.getDestinationFactory(targetQueueName);

      // even number
      final int batchSize = 4;
      bridge =
          new JMSBridgeImpl(
              "test-bridge",
              sourceCFF,
              targetCFF,
              sourceQueueFactory,
              targetQueueFactory,
              null,
              null,
              null,
              null,
              null,
              1000,
              -1,
              mode,
              batchSize,
              -1,
              null,
              null,
              false);

      txMgr = newTransactionManager();
      bridge.setTransactionManager(txMgr);

      // start the bridge
      bridge.start();

      System.out.println("started bridge");

      final int NUM_MESSAGES = batchSize / 2;

      // send some messages to source
      sendMessages(sourceServer, sourceQueueName, NUM_MESSAGES);
      // receive from target, no message should be received.
      receiveMessages(targetServer, targetQueueName, 0);

      // now crash target server
      targetServer.crashLive();

      // send more
      sendMessages(sourceServer, sourceQueueName, NUM_MESSAGES);

      receiveMessages(targetServer, targetQueueName, batchSize);

      // send some again
      sendMessages(sourceServer, sourceQueueName, NUM_MESSAGES);
      // check no messages arrived.
      receiveMessages(targetServer, targetQueueName, 0);
      // now crash source server
      sourceServer.crashLive();

      // verify bridge still work
      sendMessages(sourceServer, sourceQueueName, NUM_MESSAGES);
      receiveMessages(targetServer, targetQueueName, batchSize);
    } finally {
      if (bridge != null) {
        bridge.stop();
      }
    }
  }
  /*
   * Deploy a bridge, source and target queues are in
   * separate live/backup pairs. Source and Target CF are ha.
   * Test the bridge work when the live servers crash.
   */
  private void performSourceAndTargetCrashAndFailover(QualityOfServiceMode mode) throws Exception {

    JMSBridgeImpl bridge = null;
    TransactionManager txMgr = null;

    try {
      ConnectionFactoryFactory sourceCFF = sourceServer.getConnectionFactoryFactory();
      ConnectionFactoryFactory targetCFF = targetServer.getConnectionFactoryFactory();
      DestinationFactory sourceQueueFactory = sourceServer.getDestinationFactory(sourceQueueName);
      DestinationFactory targetQueueFactory = targetServer.getDestinationFactory(targetQueueName);

      bridge =
          new JMSBridgeImpl(
              "test-bridge",
              sourceCFF,
              targetCFF,
              sourceQueueFactory,
              targetQueueFactory,
              null,
              null,
              null,
              null,
              null,
              1000,
              -1,
              mode,
              10,
              1000,
              null,
              null,
              false);

      txMgr = newTransactionManager();
      bridge.setTransactionManager(txMgr);

      // start the bridge
      bridge.start();

      final int NUM_MESSAGES = 10;

      // send some messages to source
      sendMessages(sourceServer, sourceQueueName, NUM_MESSAGES);
      // receive from target
      receiveMessages(targetServer, targetQueueName, NUM_MESSAGES);

      // now crash target server
      targetServer.crashLive();

      // verify bridge still works
      sendMessages(sourceServer, sourceQueueName, NUM_MESSAGES);
      receiveMessages(targetServer, targetQueueName, NUM_MESSAGES);

      // now crash source server
      sourceServer.crashLive();

      // verify bridge still work
      sendMessages(sourceServer, sourceQueueName, NUM_MESSAGES);
      receiveMessages(
          targetServer,
          targetQueueName,
          NUM_MESSAGES,
          mode == QualityOfServiceMode.ONCE_AND_ONLY_ONCE);
    } finally {
      if (bridge != null) {
        bridge.stop();
      }
    }
  }