@Test
public void testLoadBalancingWithAllBadNodes() throws Exception {

  Configuration conf = new Configuration();
  KMSClientProvider p1 = mock(KMSClientProvider.class);
  when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class)))
      .thenThrow(new IOException("p1"));
  KMSClientProvider p2 = mock(KMSClientProvider.class);
  when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class)))
      .thenThrow(new IOException("p2"));
  KMSClientProvider p3 = mock(KMSClientProvider.class);
  when(p3.createKey(Mockito.anyString(), Mockito.any(Options.class)))
      .thenThrow(new IOException("p3"));
  KMSClientProvider p4 = mock(KMSClientProvider.class);
  when(p4.createKey(Mockito.anyString(), Mockito.any(Options.class)))
      .thenThrow(new IOException("p4"));
  when(p1.getKMSUrl()).thenReturn("p1");
  when(p2.getKMSUrl()).thenReturn("p2");
  when(p3.getKMSUrl()).thenReturn("p3");
  when(p4.getKMSUrl()).thenReturn("p4");
  KeyProvider kp = new LoadBalancingKMSClientProvider(
      new KMSClientProvider[] { p1, p2, p3, p4 }, 0, conf);
  try {
    kp.createKey("test3", new Options(conf)).getName();
    fail("Should fail since all providers threw an IOException");
  } catch (Exception e) {
    assertTrue(e instanceof IOException);
  }
}