waitContext.complete(); }); sendRequest(op);
waitContext.complete(); }); sendRequest(op);
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; }
/** * Synchronously perform operation. * * Expecting provided operation to be failed. * * @param op operation to perform * @return callback operation and failure */ public FailureResponse sendAndWaitFailure(Operation op) { FailureResponse response = new FailureResponse(); // Prepare caller stack information for operation success. String msg = String.format("Expected operation failure but was successful. uri=%s ", op.getUri()); Exception callerStack = new RuntimeException(msg); TestContext waitContext = new TestContext(1, this.timeout); op.appendCompletion((o, e) -> { if (e != null) { response.op = o; response.failure = e; waitContext.complete(); return; } waitContext.fail(callerStack); }); sendRequest(op); waitContext.await(); return response; }
/** * Synchronously perform operation. * * Expecting provided operation to be failed. * * @param op operation to perform * @return callback operation and failure */ public FailureResponse sendAndWaitFailure(Operation op) { FailureResponse response = new FailureResponse(); // Prepare caller stack information for operation success. String msg = String.format("Expected operation failure but was successful. uri=%s ", op.getUri()); Exception callerStack = new RuntimeException(msg); TestContext waitContext = new TestContext(1, this.timeout); op.appendCompletion((o, e) -> { if (e != null) { response.op = o; response.failure = e; waitContext.complete(); return; } waitContext.fail(callerStack); }); sendRequest(op); waitContext.await(); return response; }
@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 synchTaskStopsSelfPatchingOnFactoryDelete() throws Throwable { String factoryLink = ExampleService.FACTORY_LINK; this.host.createExampleServices(this.host, this.serviceCount, null, false, factoryLink); SynchronizationTaskService.State task = createSynchronizationTaskState(Long.MAX_VALUE, factoryLink); Operation op = Operation .createPost(UriUtils.buildUri(this.host, SynchronizationTaskService.FACTORY_LINK)) .setBody(task); TestRequestSender sender = new TestRequestSender(this.host); sender.sendRequest(Operation.createDelete(UriUtils.buildUri(this.host, factoryLink))); SynchronizationTaskService.State result = sender.sendAndWait(op, SynchronizationTaskService.State.class); // Verify that patch count stops incrementing after a while AtomicInteger previousValue = new AtomicInteger(); this.host.waitFor("Expected synch task to stop patching itself", () -> { // Get the latest patch count URI statsURI = UriUtils.buildStatsUri(this.host, result.documentSelfLink); ServiceStats stats = this.host.getServiceState(null, ServiceStats.class, statsURI); ServiceStats.ServiceStat synchRetryCount = stats.entries .get(STAT_NAME_PATCH_REQUEST_COUNT); return previousValue.getAndSet((int)synchRetryCount.latestValue) == synchRetryCount.latestValue; }); }
@Test public void synchTaskStopsSelfPatchingOnFactoryDelete() throws Throwable { String factoryLink = ExampleService.FACTORY_LINK; this.host.createExampleServices(this.host, this.serviceCount, null, false, factoryLink); SynchronizationTaskService.State task = createSynchronizationTaskState(Long.MAX_VALUE, factoryLink); Operation op = Operation .createPost(UriUtils.buildUri(this.host, SynchronizationTaskService.FACTORY_LINK)) .setBody(task); TestRequestSender sender = new TestRequestSender(this.host); sender.sendRequest(Operation.createDelete(UriUtils.buildUri(this.host, factoryLink))); SynchronizationTaskService.State result = sender.sendAndWait(op, SynchronizationTaskService.State.class); // Verify that patch count stops incrementing after a while AtomicInteger previousValue = new AtomicInteger(); this.host.waitFor("Expected synch task to stop patching itself", () -> { // Get the latest patch count URI statsURI = UriUtils.buildStatsUri(this.host, result.documentSelfLink); ServiceStats stats = this.host.getServiceState(null, ServiceStats.class, statsURI); ServiceStats.ServiceStat synchRetryCount = stats.entries .get(STAT_NAME_PATCH_REQUEST_COUNT); return previousValue.getAndSet((int)synchRetryCount.latestValue) == synchRetryCount.latestValue; }); }
ExampleServiceState state = new ExampleServiceState(); state.name = this.host.nextUUID(); sender.sendRequest(Operation.createGet(u).setCompletion(ctx.getCompletion())); sender.sendRequest( Operation.createPatch(u) .setContextId(this.host.nextUUID()) ExampleServiceState state = new ExampleServiceState(); state.name = this.host.nextUUID(); sender.sendRequest(Operation.createGet(u).setCompletion(ctx.getCompletion())); sender.sendRequest( Operation.createPatch(u).setContextId(this.host.nextUUID()).setBody(state) .setCompletion(ctx.getCompletion()));
ExampleServiceState state = new ExampleServiceState(); state.name = this.host.nextUUID(); sender.sendRequest(Operation.createGet(u).setCompletion(ctx.getCompletion())); sender.sendRequest( Operation.createPatch(u) .setContextId(this.host.nextUUID()) ExampleServiceState state = new ExampleServiceState(); state.name = this.host.nextUUID(); sender.sendRequest(Operation.createGet(u).setCompletion(ctx.getCompletion())); sender.sendRequest( Operation.createPatch(u).setContextId(this.host.nextUUID()).setBody(state) .setCompletion(ctx.getCompletion()));
@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(); }