/** * 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); }
/** * 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); }
public CompletionHandler getCompletion() { return (o, e) -> { if (e != null) { this.fail(e); } else { this.complete(); } }; }
public CompletionHandler getCompletion() { return (o, e) -> { if (e != null) { this.fail(e); } else { this.complete(); } }; }
public CompletionHandler getExpectedFailureCompletion() { return (o, e) -> { if (e != null) { this.complete(); } else { this.fail(new IllegalStateException("got success, expected failure")); } }; }
public CompletionHandler getExpectedFailureCompletion() { return (o, e) -> { if (e != null) { this.complete(); } else { this.fail(new IllegalStateException("got success, expected failure")); } }; }
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(); }
@Override public void handleRequest(Operation update) { update.complete(); if (update.getAction().equals(Action.PATCH)) { if (update.getUri().getHost() == null) { oneUseNotificationCtx.fail(new IllegalStateException( "Notification URI does not have host specified")); return; } oneUseNotificationCtx.complete(); } } };
@Override public void handleRequest(Operation update) { update.complete(); if (update.getAction().equals(Action.PATCH)) { if (update.getUri().getHost() == null) { oneUseNotificationCtx.fail(new IllegalStateException( "Notification URI does not have host specified")); return; } oneUseNotificationCtx.complete(); } } };
private void traversePageLinks(TestContext ctx, String nextPageLink, List<String> nextPageLinks) { nextPageLinks.add(nextPageLink); Operation get = Operation.createGet(this.host, nextPageLink) .setCompletion((o, e) -> { if (e != null) { ctx.fail(e); return; } QueryTask page = o.getBody(QueryTask.class); if (page.results.nextPageLink == null) { ctx.complete(); return; } traversePageLinks(ctx, page.results.nextPageLink, nextPageLinks); }); this.host.send(get); }
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 traversePageLinks(TestContext ctx, String nextPageLink, List<String> nextPageLinks) { nextPageLinks.add(nextPageLink); Operation get = Operation.createGet(this.host, nextPageLink) .setCompletion((o, e) -> { if (e != null) { ctx.fail(e); return; } QueryTask page = o.getBody(QueryTask.class); if (page.results.nextPageLink == null) { ctx.complete(); return; } traversePageLinks(ctx, page.results.nextPageLink, nextPageLinks); }); this.host.send(get); }
private void patchExampleServices(Map<URI, ExampleServiceState> states, int count) throws Throwable { TestContext ctx = this.host.testCreate(states.size() * count); for (ExampleServiceState st : states.values()) { for (int i = 0; i < count; i++) { st.name = "updated" + Utils.getNowMicrosUtc() + ""; Operation patch = Operation .createPatch(UriUtils.buildUri(this.host, st.documentSelfLink)) .setCompletion((o, e) -> { if (e != null) { ctx.fail(e); return; } ctx.complete(); }).setBody(st); this.host.send(patch); } } this.host.testWait(ctx); }
private void patchExampleServices(Map<URI, ExampleServiceState> states, int count) throws Throwable { TestContext ctx = this.host.testCreate(states.size() * count); for (ExampleServiceState st : states.values()) { for (int i = 0; i < count; i++) { st.name = "updated" + Utils.getNowMicrosUtc() + ""; Operation patch = Operation .createPatch(UriUtils.buildUri(this.host, st.documentSelfLink)) .setCompletion((o, e) -> { if (e != null) { ctx.fail(e); return; } ctx.complete(); }).setBody(st); this.host.send(patch); } } this.host.testWait(ctx); }
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(); }
@Test public void testEmptyEndpoint() { TestContext ctx = testCreate(1); List<PKSPlan> plans = new ArrayList<>(); Operation get = Operation.createGet(host, PKSPlanListService.SELF_LINK) .setCompletion((op, ex) -> { if (ex != null) { assertEquals(Operation.STATUS_CODE_BAD_REQUEST, op.getStatusCode()); ctx.complete(); return; } ctx.fail(null); }); sender.sendRequest(get); ctx.await(); }
@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")); }
@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 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(); }
@Test public void testPost() { String endpointLink = createEndpoint().documentSelfLink; 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.createPost(serviceUri) .setCompletion((op, ex) -> { if (ex != null) { assertEquals(Operation.STATUS_CODE_BAD_METHOD, op.getStatusCode()); ctx.complete(); return; } ctx.fail(null); }); sender.sendRequest(get); ctx.await(); }