public static TestContext testCreate(int count) { long expIntervalMicros = TimeUnit.MILLISECONDS.toMicros( WAIT_FOR_STAGE_CHANGE_COUNT * WAIT_THREAD_SLEEP_IN_MILLIS); return TestContext.create(count, expIntervalMicros); }
/** * Creates a test wait context that can be nested and isolated from other wait contexts */ public TestContext testCreate(int c) { return TestContext.create(c, TimeUnit.SECONDS.toMicros(this.timeoutSeconds)); }
/** * Creates a test wait context that can be nested and isolated from other wait contexts */ public TestContext testCreate(int c) { return TestContext.create(c, TimeUnit.SECONDS.toMicros(this.timeoutSeconds)); }
public void testStart(String testName, EnumSet<TestProperty> properties, long c) { if (this.isSingleton) { throw new IllegalStateException("Use startTest on singleton, shared host instances"); } if (this.context != null) { throw new IllegalStateException("A test is already started"); } String negative = properties != null && properties.contains(TestProperty.FORCE_FAILURE) ? "(NEGATIVE)" : ""; if (c > 1) { log("%sTest %s, iterations %d, started", negative, testName, c); } this.failure = null; this.expectedCompletionCount = c; this.testStartMicros = Utils.getSystemNowMicrosUtc(); this.context = TestContext.create((int) c, TimeUnit.SECONDS.toMicros(this.timeoutSeconds)); }
public void testStart(String testName, EnumSet<TestProperty> properties, long c) { if (this.isSingleton) { throw new IllegalStateException("Use startTest on singleton, shared host instances"); } if (this.context != null) { throw new IllegalStateException("A test is already started"); } String negative = properties != null && properties.contains(TestProperty.FORCE_FAILURE) ? "(NEGATIVE)" : ""; if (c > 1) { log("%sTest %s, iterations %d, started", negative, testName, c); } this.failure = null; this.expectedCompletionCount = c; this.testStartMicros = Utils.getSystemNowMicrosUtc(); this.context = TestContext.create((int) c, TimeUnit.SECONDS.toMicros(this.timeoutSeconds)); }
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(); }
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 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 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 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 void doSimpleTest(boolean isRemote) { TestContext ctx = TestContext.create(1, TimeUnit.MINUTES.toMicros(1)); List<ServerSentEvent> events = new ArrayList<>(); List<Long> timesReceived = new ArrayList<>(); Operation get = Operation.createGet(this.host, EventStreamService.SELF_LINK) .setHeadersReceivedHandler(op -> { assertEquals(Operation.MEDIA_TYPE_TEXT_EVENT_STREAM, op.getContentType()); assertEquals(0, events.size()); }) .setServerSentEventHandler(event -> { timesReceived.add(System.currentTimeMillis()); events.add(event); }) .setCompletion(ctx.getCompletion()); if (isRemote) { get.forceRemote(); } this.host.send(get); ctx.await(); assertEquals(EVENTS, events); double averageDelay = IntStream.range(1, timesReceived.size()) .mapToLong(i -> timesReceived.get(i) - timesReceived.get(i - 1)) .average().getAsDouble(); Assert.assertTrue(averageDelay >= EVENT_EMIT_PERIOD_MS / 2.0); }
private void doSimpleTest(boolean isRemote) { TestContext ctx = TestContext.create(1, TimeUnit.MINUTES.toMicros(1)); List<ServerSentEvent> events = new ArrayList<>(); List<Long> timesReceived = new ArrayList<>(); Operation get = Operation.createGet(this.host, EventStreamService.SELF_LINK) .setHeadersReceivedHandler(op -> { assertEquals(Operation.MEDIA_TYPE_TEXT_EVENT_STREAM, op.getContentType()); assertEquals(0, events.size()); }) .setServerSentEventHandler(event -> { timesReceived.add(System.currentTimeMillis()); events.add(event); }) .setCompletion(ctx.getCompletion()); if (isRemote) { get.forceRemote(); } this.host.send(get); ctx.await(); assertEquals(EVENTS, events); double averageDelay = IntStream.range(1, timesReceived.size()) .mapToLong(i -> timesReceived.get(i) - timesReceived.get(i - 1)) .average().getAsDouble(); Assert.assertTrue(averageDelay >= EVENT_EMIT_PERIOD_MS / 2.0); }
public static String create(ServiceHost h, ServiceOption... extraOptions) throws Throwable { // create an on demand load factory and services TestContext ctx = TestContext.create(1, h.getOperationTimeoutMicros()); OnDemandLoadFactoryService s = new OnDemandLoadFactoryService(); EnumSet<ServiceOption> childOptions = EnumSet.of(ServiceOption.PERSISTENCE, ServiceOption.REPLICATION, ServiceOption.OWNER_SELECTION); if (extraOptions != null) { for (ServiceOption eo : extraOptions) { childOptions.add(eo); } } s.setChildServiceCaps(childOptions); Operation factoryPost = Operation.createPost( UriUtils.buildUri(h, s.getClass())) .setCompletion(ctx.getCompletion()); h.startService(factoryPost, s); ctx.await(); String factoryLink = s.getSelfLink(); h.log(Level.INFO, "Started on demand load factory at %s", factoryLink); return factoryLink; }
public static String create(ServiceHost h, ServiceOption... extraOptions) throws Throwable { // create an on demand load factory and services TestContext ctx = TestContext.create(1, h.getOperationTimeoutMicros()); OnDemandLoadFactoryService s = new OnDemandLoadFactoryService(); EnumSet<ServiceOption> childOptions = EnumSet.of(ServiceOption.PERSISTENCE, ServiceOption.REPLICATION, ServiceOption.OWNER_SELECTION); if (extraOptions != null) { for (ServiceOption eo : extraOptions) { childOptions.add(eo); } } s.setChildServiceCaps(childOptions); Operation factoryPost = Operation.createPost( UriUtils.buildUri(h, s.getClass())) .setCompletion(ctx.getCompletion()); h.startService(factoryPost, s); ctx.await(); String factoryLink = s.getSelfLink(); h.log(Level.INFO, "Started on demand load factory at %s", factoryLink); return factoryLink; }