@Test
  public void testBackupFailure() throws Exception {
    IPartitionManager pm = ManagementContainer.getInstance().getPartitionManager();

    m_part.setAccessibleRemotely(true);
    pm.updatePartition(m_part);
    m_part = pm.getPartition(TEST_PID);

    Customer cust =
        TestUtils.createActiveMailboxTestCustomer(Capabilities.ActiveMailboxSupport.ARCHIVE);
    try {
      MockMessageImporter mock = new MockMessageImporter(getClass().getSimpleName());
      PartitionStoreStage stg =
          new PartitionStoreStage(mock) {
            public void finish() {
              super.finish();
              ((StatComponent) m_importer.getStatComponent()).stop();
            }
          };
      MockStageController ctrl = new MockStageController("test");
      stg.setStageController(ctrl);

      assertEquals(0, mock.getNumFailedMessages());
      assertEquals(0, mock.getNumSucceededMessages());
      // Partition dummyPart = new Partition(10, "sdcvsdv");

      File backupSubDir = new File(m_part.getStorageDirectory(), Partition.BACKUP_SUBDIR);

      Pair<Partition, List<IMessageContext>> buffer =
          new Pair<Partition, List<IMessageContext>>(
              m_part, generateMockMessageContext(10000, 10, cust));

      stg.process(buffer);

      // Nothing should have failed
      assertEquals(0, mock.getNumFailedMessages());
      assertEquals(10, ctrl.getOutputCount());

      assertTrue(backupSubDir.exists());

      // Stop backup writers, delete backup directory and then make sure everything works when we
      // start back up
      Utils.finishBackupWriters();

      FileUtils.deleteDirectory(backupSubDir);

      assertFalse(backupSubDir.exists());
      Utils.startBackupWriters();

      ctrl.resetOutputCount();
      buffer =
          new Pair<Partition, List<IMessageContext>>(
              m_part, generateMockMessageContext(100000, 10, cust));

      stg.process(buffer);

      // Nothing should have failed
      assertEquals(0, mock.getNumFailedMessages());
      assertEquals(10, ctrl.getOutputCount());

      assertTrue(backupSubDir.exists());
      stg.finish();
    } finally {
      TestUtils.quietDeleteCustomer(cust);
    }
  }
  /**
   * Validates that the stage properly identifies when a SNAException has occured on a message
   * multiple times consecutively and properly fails the message
   *
   * @throws Exception on error
   */
  @Test
  public void testServiceNotAvailableExceptionHandling() throws Exception {
    MockMessageImporter mock = new MockMessageImporter(getClass().getSimpleName());
    PartitionStoreStage stg =
        new PartitionStoreStage(mock) {
          public void finish() {
            super.finish();
            ((StatComponent) m_importer.getStatComponent()).stop();
          }
        };
    stg.setStageController(new MockStageController("test"));

    final IPartitionProxy partitionProxy = createNiceMock(IPartitionProxy.class);

    assertEquals(0, mock.getNumFailedMessages());
    assertEquals(0, mock.getNumSucceededMessages());

    String path = m_tmpDir + "/storage";
    Partition dummyPart = null;
    try {

      int messageBufferSize = 10;
      Customer cust =
          TestUtils.createActiveMailboxTestCustomer(Capabilities.ActiveMailboxSupport.ARCHIVE);
      List<IMessageContext> msgs = generateMockMessageContext(100, messageBufferSize, cust);

      // force an exception on any call to storeMessages
      expect(partitionProxy.storeMessages(msgs))
          .andThrow(new IPartitionProxy.ServiceNotAvailableException(new Exception()))
          .anyTimes();
      replay(partitionProxy);

      PartitionManager pm =
          new PartitionManager(
              ManagementContainer.getInstance().getPool(ManagementContainer.AM_POOL_NAME)) {
            @Override
            public IPartitionProxy getContentProxy(int id) {
              return partitionProxy;
            }
          };
      PartitionManagerTest.preparePartitionManager(pm);
      stg.setPartitionManager(pm);
      dummyPart = pm.initializePartition(TEST_PID2, "localhost", path, null);
      s_logger.debug("Dummy Partition: " + dummyPart + " (type: " + dummyPart.getClass() + ")");
      Pair<Partition, List<IMessageContext>> buffer =
          new Pair<Partition, List<IMessageContext>>(dummyPart, msgs);

      for (int i = 0; i < stg.getErrorThreshold(); i++) {
        stg.process(buffer);
        assertEquals("Wrong number of failed messages.", 0, mock.getNumFailedMessages());
        assertEquals("Wrong number of succeeded messages.", 0, mock.getNumSucceededMessages());
      }

      stg.process(buffer);
      assertEquals(
          "Wrong number of failed messages", messageBufferSize, mock.getNumFailedMessages());
      assertEquals("Wrong number of succeeded messages", 0, mock.getNumSucceededMessages());

      // Test that errors must come from the same importer before they can fail a message
      mock = new MockMessageImporter(getClass().getSimpleName());
      MockMessageImporter mock2 = new MockMessageImporter(getClass().getSimpleName());
      PartitionStoreStage stg2 = new PartitionStoreStage(mock2);
      stg2.setPartitionManager(pm);
      stg2.setStageController(new MockStageController("test"));
      stg.finish();
      stg = new PartitionStoreStage(mock);
      stg.setStageController(new MockStageController("test"));
      stg.setPartitionManager(pm);

      buffer = new Pair<Partition, List<IMessageContext>>(dummyPart, msgs);

      // Send the message buffer through Threshold - 1 times using importer 1 and then 1 time with
      // importer 2 which should not result in failure
      // Then send the messages through importer 1 again Threshold times and verify it only fails
      // the messages once we have reached threshold
      for (int i = 0; i < stg.getErrorThreshold(); i++) {
        stg.process(buffer);
        assertEquals(0, mock.getNumFailedMessages());
        assertEquals(0, mock.getNumSucceededMessages());
      }

      stg2.process(buffer);
      assertEquals(0, mock2.getNumFailedMessages());
      assertEquals(0, mock2.getNumSucceededMessages());
      assertEquals(0, mock.getNumFailedMessages());
      assertEquals(0, mock.getNumSucceededMessages());
      for (int i = 0; i < stg.getErrorThreshold(); i++) {
        stg.process(buffer);
        assertEquals(0, mock.getNumFailedMessages());
        assertEquals(0, mock.getNumSucceededMessages());
      }

      stg.process(buffer);
      assertEquals(messageBufferSize, mock.getNumFailedMessages());
      assertEquals(0, mock.getNumSucceededMessages());

      // Now validate that age comes into play as well
      mock = new MockMessageImporter(getClass().getSimpleName());
      stg.finish();
      stg = new PartitionStoreStage(mock);
      stg.setStageController(new MockStageController("test"));
      stg.setErrorAgeThreshold(5000);
      stg.setPartitionManager(pm);
      buffer =
          new Pair<Partition, List<IMessageContext>>(
              m_part, generateMockMessageContext(1000, messageBufferSize, cust));

      for (int i = 0; i < stg.getErrorThreshold(); i++) {
        stg.process(buffer);
        assertEquals("Wrong number of failed messages", 0, mock.getNumFailedMessages());
        assertEquals("Wrong number of succeeded messages", 0, mock.getNumSucceededMessages());
      }

      // Thread.sleep( 6000 );
      stg.process(buffer);
      stg.finish();
      stg2.finish();
      assertEquals(0, mock.getNumFailedMessages());
      assertEquals(0, mock.getNumSucceededMessages());
    } finally {

    }
  }