ConfiguredFailoverProxyProvider extends
    AbstractNNFailoverProxyProvider<T> { @Override public synchronized ProxyInfo<T> getProxy() { NNProxyInfo<T> current = proxies.get(currentProxyIndex); return createProxyIfNeeded(current); } ConfiguredFailoverProxyProvider(Configuration conf, URI uri,
      Class<T> xface, HAProxyFactory<T> factory);  ConfiguredFailoverProxyProvider(Configuration conf, URI uri,
      Class<T> xface, HAProxyFactory<T> factory, String addressKey); @Override synchronized ProxyInfo<T> getProxy(); @Override void performFailover(T currentProxy); @Override synchronized void close(); @Override boolean useLogicalURI();  }
@Test public void testNonRandomGetProxy() throws Exception { final AtomicInteger nn1Count = new AtomicInteger(0); final AtomicInteger nn2Count = new AtomicInteger(0); Map<InetSocketAddress, ClientProtocol> proxyMap = new HashMap<>(); final ClientProtocol nn1Mock = mock(ClientProtocol.class); when(nn1Mock.getStats()).thenAnswer(createAnswer(nn1Count, 1)); proxyMap.put(ns1nn1, nn1Mock); final ClientProtocol nn2Mock = mock(ClientProtocol.class); when(nn2Mock.getStats()).thenAnswer(createAnswer(nn2Count, 2)); proxyMap.put(ns1nn2, nn2Mock); ConfiguredFailoverProxyProvider<ClientProtocol> provider1 = new ConfiguredFailoverProxyProvider<>(conf, ns1Uri, ClientProtocol.class, createFactory(proxyMap)); ClientProtocol proxy1 = provider1.getProxy().proxy; proxy1.getStats(); assertEquals(1, nn1Count.get()); assertEquals(0, nn2Count.get()); proxy1.getStats(); assertEquals(2, nn1Count.get()); assertEquals(0, nn2Count.get()); nn1Count.set(0); nn2Count.set(0); for (int i = 0; i < NUM_ITERATIONS; i++) { ConfiguredFailoverProxyProvider<ClientProtocol> provider2 = new ConfiguredFailoverProxyProvider<>(conf, ns1Uri, ClientProtocol.class, createFactory(proxyMap)); ClientProtocol proxy2 = provider2.getProxy().proxy; proxy2.getStats(); } assertEquals(NUM_ITERATIONS, nn1Count.get()); assertEquals(0, nn2Count.get()); }
@Test public void testRandomGetProxy() throws Exception { final AtomicInteger nn1Count = new AtomicInteger(0); final AtomicInteger nn2Count = new AtomicInteger(0); final AtomicInteger nn3Count = new AtomicInteger(0); Map<InetSocketAddress, ClientProtocol> proxyMap = new HashMap<>(); final ClientProtocol nn1Mock = mock(ClientProtocol.class); when(nn1Mock.getStats()).thenAnswer(createAnswer(nn1Count, 1)); proxyMap.put(ns2nn1, nn1Mock); final ClientProtocol nn2Mock = mock(ClientProtocol.class); when(nn2Mock.getStats()).thenAnswer(createAnswer(nn2Count, 2)); proxyMap.put(ns2nn2, nn2Mock); final ClientProtocol nn3Mock = mock(ClientProtocol.class); when(nn3Mock.getStats()).thenAnswer(createAnswer(nn3Count, 3)); proxyMap.put(ns2nn3, nn3Mock); for (int i = 0; i < NUM_ITERATIONS; i++) { ConfiguredFailoverProxyProvider<ClientProtocol> provider = new ConfiguredFailoverProxyProvider<>(conf, ns2Uri, ClientProtocol.class, createFactory(proxyMap)); ClientProtocol proxy = provider.getProxy().proxy; proxy.getStats(); } assertTrue(nn1Count.get() < NUM_ITERATIONS && nn1Count.get() > 0); assertTrue(nn2Count.get() < NUM_ITERATIONS && nn2Count.get() > 0); assertTrue(nn3Count.get() < NUM_ITERATIONS && nn3Count.get() > 0); assertEquals(NUM_ITERATIONS, nn1Count.get() + nn2Count.get() + nn3Count.get()); }