Skip to content

Commit

Permalink
refactor ClientFactory.open to return CompletableFuture<Client> inste… (
Browse files Browse the repository at this point in the history
#155)

* refactor ClientFactory.open to return CompletableFuture<Client> instead of Client

* fix javadoc
  • Loading branch information
kortemik authored May 7, 2024
1 parent 41cb0d3 commit a0efd9d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
19 changes: 5 additions & 14 deletions src/main/java/com/teragrep/rlp_03/client/ClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,13 @@ public ClientFactory(ConnectContextFactory connectContextFactory, EventLoop even
* {@link EventLoop} needs to run in order to proceed with the connection.
*
* @param inetSocketAddress destination {@link InetSocketAddress} to connect to.
* @param timeout timeout value for connection attempt
* @param unit {@link TimeUnit} of the timeout value
* @return {@link Client} once connection succeeds.
* @throws IOException if connection attempt fails
* @return a {@link Client} {@link CompletableFuture}.
* @throws IOException if connection attempt fails.
* @throws InterruptedException if waiting for connection establishment is interrupted.
* @throws ExecutionException if connection establishment fails to complete successfully.
* @throws TimeoutException if connection establishment times out.
*/
public Client open(InetSocketAddress inetSocketAddress, long timeout, TimeUnit unit)
public CompletableFuture<Client> open(InetSocketAddress inetSocketAddress)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// this is for returning ready connection
CompletableFuture<EstablishedContext> readyContextFuture = new CompletableFuture<>();
Expand All @@ -110,14 +108,7 @@ public Client open(InetSocketAddress inetSocketAddress, long timeout, TimeUnit u
LOGGER.debug("registering to eventLoop <{}>", eventLoop);
eventLoop.register(connectContext);
LOGGER.debug("registered to eventLoop <{}>", eventLoop);
try {
EstablishedContext establishedContext = readyContextFuture.get(timeout, unit);
LOGGER.debug("returning establishedContext <{}>", establishedContext);
return clientDelegate.create(establishedContext);
}
catch (TimeoutException timeoutException) {
connectContext.close();
throw timeoutException;
}

return readyContextFuture.thenApply(clientDelegate::create);
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/teragrep/rlp_03/client/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void testClient() throws IOException {
ConnectContextFactory connectContextFactory = new ConnectContextFactory(executorService, socketFactory);
ClientFactory clientFactory = new ClientFactory(connectContextFactory, eventLoop);

try (Client client = clientFactory.open(new InetSocketAddress("localhost", port), 1, TimeUnit.SECONDS)) {
try (Client client = clientFactory.open(new InetSocketAddress("localhost", port)).get(1, TimeUnit.SECONDS)) {

// send open
CompletableFuture<RelpFrame> open = client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void testStuckClient() {
ConnectContextFactory connectContextFactory = new ConnectContextFactory(executorService, socketFactory);
ClientFactory clientFactory = new ClientFactory(connectContextFactory, eventLoop);

try (Client client = clientFactory.open(new InetSocketAddress("localhost", port), 1, TimeUnit.SECONDS)) {
try (Client client = clientFactory.open(new InetSocketAddress("localhost", port)).get(1, TimeUnit.SECONDS)) {

// send open
CompletableFuture<RelpFrame> open = client
Expand Down

0 comments on commit a0efd9d

Please sign in to comment.