private void waitFor(Map map, int count) { TestContext context = new TestContext(1, Duration.ofMinutes(1)); schedule(map, count, context); context.await(); }
public <T> BiConsumer<T, ? super Throwable> getCompletionDeferred() { return (ignore, e) -> { if (e != null) { if (e instanceof CompletionException) { e = e.getCause(); } failIteration(e); return; } completeIteration(); }; }
public void await(DeferredResult<?> deferredResult) { await(() -> { if (deferredResult.isDone()) { this.completeIteration(); } }); }
protected SecurityContext getSecurityContext() { final SecurityContext[] context = new SecurityContext[1]; TestContext ctx = testCreate(1); host.send(Operation.createGet(host, SessionService.SELF_LINK) .setCompletion((o, ex) -> { if (ex != null) { ctx.failIteration(ex); return; } context[0] = o.getBody(SecurityContext.class); ctx.completeIteration(); })); ctx.await(); return context[0]; }
@Test public void testGetNowWithValue() throws Throwable { TestContext ctx = new TestContext(1, TestContext.DEFAULT_WAIT_DURATION); TestContext synchCtx = new TestContext(1, TestContext.DEFAULT_WAIT_DURATION); int defaultValue = 0; int value = 10; DeferredResult<Integer> deferredResult = new DeferredResult<>(); deferredResult.whenComplete(ctx.getCompletionDeferred()); runAfter(synchCtx, () -> deferredResult.complete(value)); Assert.assertEquals(defaultValue, deferredResult.getNow(defaultValue).intValue()); synchCtx.complete(); ctx.await(); Assert.assertEquals(value, deferredResult.getNow(defaultValue).intValue()); }
private ExampleServiceState getDocument(String serviceLink) { // This single element array is used to extract the result from lambda. final ExampleServiceState[] getResult = new ExampleServiceState[1]; TestContext ctx = new TestContext(1, Duration.ofSeconds(WAIT_FOR_OPERATION_RESPONSE)); Operation.createGet(this.peerHost, serviceLink) .setReferer(cluster.getUri()) .setCompletion((o, ex) -> { if (ex != null) { ctx.failIteration(ex); } else { getResult[0] = o.getBody(ExampleServiceState.class); ctx.completeIteration(); } }).sendWith(cluster); ctx.await(); return getResult[0]; }
@Test public void testGetNowWithSlowWhenComplete() throws Throwable { TestContext ctx = new TestContext(1, TestContext.DEFAULT_WAIT_DURATION); DeferredResult<String> original = new DeferredResult<>(); DeferredResult<String> result = original .whenComplete((ignore, error) -> { if (error != null) { ctx.fail(error.getCause()); } else { ctx.complete(); } sleep(500); }); runAfter(1, () -> original.complete("foo")); ctx.await(); Assert.assertEquals("bar", result.getNow("bar")); }
private List<PKSPlan> sendListRequest(String endpointLink) { URI serviceUri = UriUtils.buildUri(host, PKSPlanListService.SELF_LINK, UriUtils.buildUriQuery(PKS_ENDPOINT_QUERY_PARAM_NAME, endpointLink)); TestContext ctx = testCreate(1); List<PKSPlan> plans = new ArrayList<>(); Operation get = Operation.createGet(serviceUri) .setCompletion((op, ex) -> { if (ex != null) { ctx.fail(ex); return; } plans.addAll(Arrays.asList(op.getBody(PKSPlan[].class))); ctx.complete(); }); sender.sendRequest(get); ctx.await(); return plans; }
private void doParallelTest(int parallelism, boolean isRemote) { TestContext ctx = TestContext.create(parallelism, TimeUnit.MINUTES.toMicros(1)); List<ServerSentEvent> events = new ArrayList<>(); for (int i = 0; i < parallelism; ++i) { Operation get = Operation.createGet(this.host, EventStreamService.SELF_LINK) .setServerSentEventHandler(event -> { synchronized (events) { events.add(event); } }) .setCompletion(ctx.getCompletion()); if (isRemote) { get.forceRemote(); } this.host.send(get); } ctx.await(); assertEquals(EVENTS.size() * parallelism, events.size()); }
private static void waitForDefaultRegistryCreated(ManagementHost host) { TestContext ctx = new TestContext(1, Duration.ofSeconds(120)); host.log(Level.INFO, "Waiting for default registry to start."); host.registerForServiceAvailability(ctx.getCompletion(), RegistryService.DEFAULT_INSTANCE_LINK); ctx.await(); host.log(Level.INFO, "Default registry started."); }
private void changeTaskName(ExtensibilitySubscriptionCallback callback, Operation post) { Map<String, String> response = new HashMap<>(); response.put("name", SELF_LINK); TestContext context = new TestContext(1, Duration.ofSeconds(20)); Operation.createPost(callback.serviceCallback) .setReferer(getHost().getUri()) .setBody(response) .setCompletion((o, e) -> { if (e != null) { post.fail(e); context.fail(e); return; } context.completeIteration(); }).sendWith(getHost()); context.await(); }
public static void runParallel(ExecutorService es, long timeoutSeconds, int count, RunnableWithThrowable runnable) { TestContext tc = TestContext.create(count, TimeUnit.SECONDS.toMicros(timeoutSeconds)); for (int i = 0; i < count; i++) { int i2 = i; es.submit(() -> { try { runnable.run(i2); tc.complete(); } catch (Throwable e) { tc.fail(e); } }); } tc.await(); }
@Test public void testThenApply() throws Throwable { TestContext ctx = new TestContext(1, TestContext.DEFAULT_WAIT_DURATION); DeferredResult<Integer> original = new DeferredResult<>(); DeferredResult<Integer> result = original .thenApply(i -> i + 1); result.whenComplete(ctx.getCompletionDeferred()); runAfter(10, () -> original.complete(1)); ctx.await(); Assert.assertEquals(2, result.getNow(0).intValue()); }
public void waitForServiceAvailable(String... links) { for (String link : links) { TestContext ctx = testCreate(1); this.registerForServiceAvailability(ctx.getCompletion(), link); ctx.await(); } }
private void changeContainerName( Operation post) { DummyServiceTaskState body = post.getBody(DummyServiceTaskState.class); ContainerState containerState = new ContainerState(); containerState.name = DummySubscriber.class.getSimpleName(); containerState.documentSelfLink = body.containerState.documentSelfLink; // Patch Container's name. TestContext context = new TestContext(1, Duration.ofSeconds(20)); Operation.createPatch(UriUtils.buildUri(getHost(), containerState.documentSelfLink)) .setBody(containerState) .setReferer(getHost().getUri()) .setCompletion((o, e) -> { if (e != null) { post.fail(e); context.fail(e); return; } }).sendWith(getHost()); context.await(); }
private Consumer<SubscriptionNotification<ContainerState>> handler() { return (r) -> { results.clear(); results.add(r); this.activeContext.completeIteration(); }; }
private void doTestWithLoad() throws Throwable { TestContext ctx = TestContext.create(1, TimeUnit.MINUTES.toMicros(1)); CompletableFuture<Void> future = CompletableFuture.runAsync(() -> { this.doParallelTest(PARALLELISM, true); }); future.whenComplete(ctx.getCompletionDeferred()); List<DeferredResult<String>> deferredResults = new ArrayList<>(); while (!future.isDone() && deferredResults.size() < 500) { ExampleServiceState state = new ExampleServiceState(); state.name = "test"; Operation postOp = Operation.createPost(this.host, ExampleService.FACTORY_LINK) .setBody(state).forceRemote(); deferredResults.add(this.host.sendWithDeferredResult(postOp, ExampleServiceState.class) .thenApply(s -> s.documentSelfLink)); Thread.sleep(3); // Slowdown if (Math.random() < 1 / 100.0) { System.gc(); } } this.host.log("Requests sent: %d", deferredResults.size()); ctx.await(); ctx = TestContext.create(1, TimeUnit.MINUTES.toMicros(1)); DeferredResult.allOf(deferredResults) .whenComplete(ctx.getCompletionDeferred()); ctx.await(); System.gc(); }
private void createUserServices(URI userFactorURI, List<URI> userServices) throws Throwable { TestContext ctx = this.host.testCreate(this.serviceCount); for (int i = 0; i < this.serviceCount; i++) { UserService.UserState s = new UserService.UserState(); s.email = UUID.randomUUID().toString() + "@example.org"; userServices.add(UriUtils.buildUri(this.host.getUri(), ExampleService.FACTORY_LINK, s.documentSelfLink)); this.host.send(Operation.createPost(userFactorURI) .setBody(s) .setCompletion(ctx.getCompletion())); } this.host.testWait(ctx); }
/** * Consider using {@link #fail(Throwable)}. * This method exists for backward compatibility, and may be deprecated/deleted in future. */ public void failIteration(Throwable e) { fail(e); }