@Test public void asBiFunction() { Operation op = Operation.createGet(URI.create("/hello")); Throwable th = new Exception(); CompletionHandler h = (o, e) -> { assertSame(op, o); assertSame(th, e); }; BiFunction<Operation, Throwable, Void> func = OperationFutures.asFunc(h); func.apply(op, th); }
@Test public void seq() throws InterruptedException, ExecutionException { ArrayList<CompletableFuture<Operation>> futs = new ArrayList<CompletableFuture<Operation>>(); // create 10 services in parallel for (int i = 0; i < 10; i++) { AuthCredentialsServiceState state = new AuthCredentialsServiceState(); state.documentSelfLink = "" + i; state.privateKey = "" + i; CompletableFuture<Operation> f = this.host.sendWithFuture(Operation .createPost(this.host, AuthCredentialsService.FACTORY_LINK) .setBody(state) .setReferer(this.host.getReferer())); futs.add(f); } // wait for the 10 requests to complete CompletableFuture<List<Operation>> join = OperationFutures.join(futs); List<Operation> allServices = join.get(); for (Operation service : allServices) { this.host.log(service.getBody(AuthCredentialsServiceState.class).documentSelfLink); } // retrieve all created service in sequence Operation getFirst = Operation.createGet(this.host, UriUtils.buildUriPath(AuthCredentialsService.FACTORY_LINK, "0")); // chain 10 CompletableFuture<Operation> f = this.host.sendWithFuture(getFirst); for (int i = 1; i < 10; i++) { f = f.thenComposeAsync(this::getNextService); } assertEquals(f.get().getBody(AuthCredentialsServiceState.class).privateKey, "9"); }
@Test public void seq() throws InterruptedException, ExecutionException { ArrayList<CompletableFuture<Operation>> futs = new ArrayList<CompletableFuture<Operation>>(); // create 10 services in parallel for (int i = 0; i < 10; i++) { AuthCredentialsServiceState state = new AuthCredentialsServiceState(); state.documentSelfLink = "" + i; state.privateKey = "" + i; CompletableFuture<Operation> f = this.host.sendWithFuture(Operation .createPost(this.host, AuthCredentialsService.FACTORY_LINK) .setBody(state) .setReferer(this.host.getReferer())); futs.add(f); } // wait for the 10 requests to complete CompletableFuture<List<Operation>> join = OperationFutures.join(futs); List<Operation> allServices = join.get(); for (Operation service : allServices) { this.host.log(service.getBody(AuthCredentialsServiceState.class).documentSelfLink); } // retrieve all created service in sequence Operation getFirst = Operation.createGet(this.host, UriUtils.buildUriPath(AuthCredentialsService.FACTORY_LINK, "0")); // chain 10 CompletableFuture<Operation> f = this.host.sendWithFuture(getFirst); for (int i = 1; i < 10; i++) { f = f.thenComposeAsync(this::getNextService); } assertEquals(f.get().getBody(AuthCredentialsServiceState.class).privateKey, "9"); }
@Test public void asBiFunction() { Operation op = Operation.createGet(URI.create("/hello")); Throwable th = new Exception(); CompletionHandler h = (o, e) -> { assertSame(op, o); assertSame(th, e); }; BiFunction<Operation, Throwable, Void> func = OperationFutures.asFunc(h); func.apply(op, th); }