RpcStrategyFirstValidResult implements RpcStrategy { @Nullable @Override public RpcSender createSender(RpcClientConnectionPool pool) { List<RpcSender> senders = list.listOfSenders(pool); if (senders.size() == 0) return null; return new Sender(senders, resultValidator, noValidResultException); } private  RpcStrategyFirstValidResult(RpcStrategyList list, ResultValidator<?> resultValidator,
			@Nullable StacklessException noValidResultException); static RpcStrategyFirstValidResult create(RpcStrategyList list); RpcStrategyFirstValidResult withResultValidator(@NotNull ResultValidator<?> resultValidator); RpcStrategyFirstValidResult withNoValidResultException(@NotNull StacklessException e); @Override Set<InetSocketAddress> getAddresses(); @Nullable @Override RpcSender createSender(RpcClientConnectionPool pool);  }
@Test public void itShouldSendRequestToAllAvailableSenders() { RpcClientConnectionPoolStub pool = new RpcClientConnectionPoolStub(); RpcSenderStub connection1 = new RpcSenderStub(); RpcSenderStub connection2 = new RpcSenderStub(); RpcSenderStub connection3 = new RpcSenderStub(); RpcStrategy firstValidResult = firstValidResult(servers(ADDRESS_1, ADDRESS_2, ADDRESS_3)); int callsAmountIterationOne = 10; int callsAmountIterationTwo = 25; RpcSender senderToAll; pool.put(ADDRESS_1, connection1); pool.put(ADDRESS_2, connection2); pool.put(ADDRESS_3, connection3); senderToAll = firstValidResult.createSender(pool); for (int i = 0; i < callsAmountIterationOne; i++) { senderToAll.sendRequest(new Object(), 50, ignore()); } pool.remove(ADDRESS_1); senderToAll = firstValidResult.createSender(pool); for (int i = 0; i < callsAmountIterationTwo; i++) { senderToAll.sendRequest(new Object(), 50, ignore()); } assertEquals(callsAmountIterationOne, connection1.getRequests()); assertEquals(callsAmountIterationOne + callsAmountIterationTwo, connection2.getRequests()); assertEquals(callsAmountIterationOne + callsAmountIterationTwo, connection3.getRequests()); }
@Test public void itShouldCallOnResultWithNullIfAllSendersReturnedNullAndValidatorAndExceptionAreNotSpecified() throws ExecutionException, InterruptedException { RpcStrategy strategy1 = new RequestSenderOnResultWithNullStrategy(); RpcStrategy strategy2 = new RequestSenderOnResultWithNullStrategy(); RpcStrategy strategy3 = new RequestSenderOnResultWithNullStrategy(); RpcStrategyFirstValidResult firstValidResult = firstValidResult(strategy1, strategy2, strategy3); RpcSender sender = firstValidResult.createSender(new RpcClientConnectionPoolStub()); CompletableFuture<Object> future = new CompletableFuture<>(); sender.sendRequest(new Object(), 50, forFuture(future)); assertNull(future.get()); }
@Test public void itShouldBeCreatedWhenThereIsAtLeastOneActiveSubSender() { RpcClientConnectionPoolStub pool = new RpcClientConnectionPoolStub(); RpcSenderStub connection = new RpcSenderStub(); pool.put(ADDRESS_2, connection); RpcStrategy firstValidResult = firstValidResult(servers(ADDRESS_1, ADDRESS_2)); assertNotNull(firstValidResult.createSender(pool)); }
@Test public void itShouldNotBeCreatedWhenThereAreNoActiveSubSenders() { RpcClientConnectionPoolStub pool = new RpcClientConnectionPoolStub(); RpcStrategy firstValidResult = firstValidResult(servers(ADDRESS_1, ADDRESS_2, ADDRESS_3)); assertNull(firstValidResult.createSender(pool)); }