@Test
public void testRMConnectionRetry() throws Exception {

  // verify the connection exception is thrown
  // if we haven't exhausted the retry interval
  ApplicationMasterProtocol mockScheduler =
      mock(ApplicationMasterProtocol.class);
  when(mockScheduler.allocate(isA(AllocateRequest.class)))
    .thenThrow(RPCUtil.getRemoteException(new IOException("forcefail")));
  Configuration conf = new Configuration();
  LocalContainerAllocator lca =
      new StubbedLocalContainerAllocator(mockScheduler);
  lca.init(conf);
  lca.start();
  try {
    lca.heartbeat();
    Assert.fail("heartbeat was supposed to throw");
  } catch (YarnException e) {
    // YarnException is expected
  } finally {
    lca.stop();
  }
  // verify YarnRuntimeException is thrown when the retry interval has expired
  conf.setLong(MRJobConfig.MR_AM_TO_RM_WAIT_INTERVAL_MS, 0);
  lca = new StubbedLocalContainerAllocator(mockScheduler);
  lca.init(conf);
  lca.start();
  try {
    lca.heartbeat();
    Assert.fail("heartbeat was supposed to throw");
  } catch (YarnRuntimeException e) {
    // YarnRuntimeException is expected
  } finally {
    lca.stop();
  }
}