OrElseEndpointGroup extends AbstractListenable<List<Endpoint>> implements EndpointGroup, ListenableAsyncCloseable { @Override public List<Endpoint> endpoints() { final List<Endpoint> endpoints = first.endpoints(); if (!endpoints.isEmpty()) { return endpoints; } return second.endpoints(); } OrElseEndpointGroup(EndpointGroup first, EndpointGroup second); @Override List<Endpoint> endpoints(); @Override EndpointSelectionStrategy selectionStrategy(); @Override Endpoint selectNow(ClientRequestContext ctx); @Override CompletableFuture<Endpoint> select(ClientRequestContext ctx,
                                              ScheduledExecutorService executor,
                                              long timeoutMillis); @Override CompletableFuture<List<Endpoint>> whenReady(); @Override boolean isClosing(); @Override boolean isClosed(); @Override CompletableFuture<?> whenClosed(); @Override CompletableFuture<?> closeAsync(); @Override void close();  }
@Test void updateFirstEndpoints() { final DynamicEndpointGroup firstEndpointGroup = new DynamicEndpointGroup(); final DynamicEndpointGroup secondEndpointGroup = new DynamicEndpointGroup(); final EndpointGroup endpointGroup = new OrElseEndpointGroup(firstEndpointGroup, secondEndpointGroup); firstEndpointGroup.setEndpoints(ImmutableList.of(Endpoint.of("127.0.0.1", 1111), Endpoint.of("127.0.0.1", 2222))); assertThat(endpointGroup.endpoints()).isEqualTo(ImmutableList.of(Endpoint.of("127.0.0.1", 1111), Endpoint.of("127.0.0.1", 2222))); firstEndpointGroup.addEndpoint(Endpoint.of("127.0.0.1", 3333)); assertThat(endpointGroup.endpoints()).isEqualTo(ImmutableList.of(Endpoint.of("127.0.0.1", 1111), Endpoint.of("127.0.0.1", 2222), Endpoint.of("127.0.0.1", 3333))); firstEndpointGroup.removeEndpoint(Endpoint.of("127.0.0.1", 2222)); assertThat(endpointGroup.endpoints()).isEqualTo(ImmutableList.of(Endpoint.of("127.0.0.1", 1111), Endpoint.of("127.0.0.1", 3333))); }
@Test void updateSecondEndpoints() { final DynamicEndpointGroup firstEndpointGroup = new DynamicEndpointGroup(); final DynamicEndpointGroup secondEndpointGroup = new DynamicEndpointGroup(); final EndpointGroup endpointGroup = new OrElseEndpointGroup(firstEndpointGroup, secondEndpointGroup); secondEndpointGroup.setEndpoints(ImmutableList.of(Endpoint.of("127.0.0.1", 1111), Endpoint.of("127.0.0.1", 2222))); secondEndpointGroup.addEndpoint(Endpoint.of("127.0.0.1", 3333)); assertThat(endpointGroup.endpoints()).isEqualTo(ImmutableList.of(Endpoint.of("127.0.0.1", 1111), Endpoint.of("127.0.0.1", 2222), Endpoint.of("127.0.0.1", 3333))); secondEndpointGroup.removeEndpoint(Endpoint.of("127.0.0.1", 2222)); assertThat(endpointGroup.endpoints()).isEqualTo(ImmutableList.of(Endpoint.of("127.0.0.1", 1111), Endpoint.of("127.0.0.1", 3333))); firstEndpointGroup.addEndpoint(Endpoint.of("127.0.0.1", 4444)); assertThat(endpointGroup.endpoints()).isEqualTo(ImmutableList.of(Endpoint.of("127.0.0.1", 4444))); secondEndpointGroup.addEndpoint(Endpoint.of("127.0.0.1", 5555)); assertThat(endpointGroup.endpoints()).isEqualTo(ImmutableList.of(Endpoint.of("127.0.0.1", 4444))); firstEndpointGroup.removeEndpoint(Endpoint.of("127.0.0.1", 4444)); assertThat(endpointGroup.endpoints()).isEqualTo(ImmutableList.of(Endpoint.of("127.0.0.1", 1111), Endpoint.of("127.0.0.1", 3333), Endpoint.of("127.0.0.1", 5555))); }