Tabnine Logo
TestContext
Code IndexAdd Tabnine to your IDE (free)

How to use
TestContext
in
com.vmware.xenon.common.test

Best Java code snippets using com.vmware.xenon.common.test.TestContext (Showing top 20 results out of 315)

origin: vmware/admiral

private void waitFor(Map map, int count) {
  TestContext context = new TestContext(1, Duration.ofMinutes(1));
  schedule(map, count, context);
  context.await();
}
origin: vmware/xenon

public CompletionHandler getCompletion() {
  return (o, e) -> {
    if (e != null) {
      this.fail(e);
    } else {
      this.complete();
    }
  };
}
origin: vmware/xenon

public <T> BiConsumer<T, ? super Throwable> getCompletionDeferred() {
  return (ignore, e) -> {
    if (e != null) {
      if (e instanceof CompletionException) {
        e = e.getCause();
      }
      failIteration(e);
      return;
    }
    completeIteration();
  };
}
origin: vmware/xenon

public void await(DeferredResult<?> deferredResult) {
  await(() -> {
    if (deferredResult.isDone()) {
      this.completeIteration();
    }
  });
}
origin: vmware/admiral

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];
}
origin: vmware/xenon

@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());
}
origin: vmware/admiral

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];
}
origin: vmware/xenon

@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"));
}
origin: vmware/admiral

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;
}
origin: vmware/xenon

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());
}
origin: vmware/admiral

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.");
}
origin: vmware/admiral

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();
}
origin: vmware/admiral

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();
}
origin: vmware/xenon

@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());
}
origin: com.vmware.xenon/xenon-common

public void waitForServiceAvailable(String... links) {
  for (String link : links) {
    TestContext ctx = testCreate(1);
    this.registerForServiceAvailability(ctx.getCompletion(), link);
    ctx.await();
  }
}
origin: vmware/admiral

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();
}
origin: vmware/admiral

private Consumer<SubscriptionNotification<ContainerState>> handler() {
  return (r) -> {
    results.clear();
    results.add(r);
    this.activeContext.completeIteration();
  };
}
origin: com.vmware.xenon/xenon-common

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();
}
origin: vmware/xenon

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);
}
origin: vmware/xenon

/**
 * 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);
}
com.vmware.xenon.common.testTestContext

Javadoc

Test context used for synchronous tests. Provides an isolated version of the VerificationHost testStart and testWait methods and allows nesting

Most used methods

  • await
  • fail
  • completeIteration
    Consider using #complete(). This method exists for backward compatibility, and may be deprecated/del
  • failIteration
    Consider using #fail(Throwable). This method exists for backward compatibility, and may be deprecate
  • <init>
  • complete
  • getCompletion
  • create
    Consider using #TestContext(int,Duration)This method exists for backward compatibility, and may be d
  • getExpectedFailureCompletion
  • getCompletionDeferred
  • logAfter
  • logBefore
  • logAfter,
  • logBefore,
  • setCheckInterval,
  • setTestName,
  • waitFor

Popular in Java

  • Reading from database using SQL prepared statement
  • findViewById (Activity)
  • getApplicationContext (Context)
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • BorderLayout (java.awt)
    A border layout lays out a container, arranging and resizing its components to fit in five regions:
  • Proxy (java.net)
    This class represents proxy server settings. A created instance of Proxy stores a type and an addres
  • SocketTimeoutException (java.net)
    This exception is thrown when a timeout expired on a socket read or accept operation.
  • Selector (java.nio.channels)
    A controller for the selection of SelectableChannel objects. Selectable channels can be registered w
  • ResultSet (java.sql)
    An interface for an object which represents a database table entry, returned as the result of the qu
  • AtomicInteger (java.util.concurrent.atomic)
    An int value that may be updated atomically. See the java.util.concurrent.atomic package specificati
  • Best plugins for Eclipse
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now