public void testRunWithTimeout_goodRunnableWithEnoughTime() throws Exception { Stopwatch stopwatch = Stopwatch.createStarted(); service.runWithTimeout(GOOD_RUNNABLE, ENOUGH_MS, MILLISECONDS); assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); }
public void testRunUninterruptiblyWithTimeout_goodRunnableWithEnoughTime() throws Exception { Stopwatch stopwatch = Stopwatch.createStarted(); service.runUninterruptiblyWithTimeout(GOOD_RUNNABLE, ENOUGH_MS, MILLISECONDS); assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); }
public void testCallUninterruptiblyWithTimeout_goodCallableWithEnoughTime() throws Exception { Stopwatch stopwatch = Stopwatch.createStarted(); String result = service.callUninterruptiblyWithTimeout(GOOD_CALLABLE, ENOUGH_MS, MILLISECONDS); assertThat(result).isEqualTo(GOOD_CALLABLE_RESULT); assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); }
public void testCallWithTimeout_goodCallableWithEnoughTime() throws Exception { Stopwatch stopwatch = Stopwatch.createStarted(); String result = service.callWithTimeout(GOOD_CALLABLE, ENOUGH_MS, MILLISECONDS); assertThat(result).isEqualTo(GOOD_CALLABLE_RESULT); assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); }
public void testNewProxy_goodMethodWithEnoughTime() throws Exception { SampleImpl target = new SampleImpl(DELAY_MS); Sample proxy = service.newProxy(target, Sample.class, ENOUGH_MS, MILLISECONDS); Stopwatch stopwatch = Stopwatch.createStarted(); String result = proxy.sleepThenReturnInput("x"); assertThat(result).isEqualTo("x"); assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); assertThat(target.finished).isTrue(); }
public void testNewProxy_badMethodWithEnoughTime() throws Exception { SampleImpl target = new SampleImpl(DELAY_MS); Sample proxy = service.newProxy(target, Sample.class, ENOUGH_MS, MILLISECONDS); Stopwatch stopwatch = Stopwatch.createStarted(); try { proxy.sleepThenThrowException(); fail("no exception thrown"); } catch (SampleException expected) { } assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); }
public void testNewProxy_badMethodWithNotEnoughTime() throws Exception { SampleImpl target = new SampleImpl(9999); Sample proxy = service.newProxy(target, Sample.class, NOT_ENOUGH_MS, MILLISECONDS); Stopwatch stopwatch = Stopwatch.createStarted(); try { proxy.sleepThenThrowException(); fail("no exception thrown"); } catch (UncheckedTimeoutException expected) { } assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(NOT_ENOUGH_MS, DELAY_MS * 2)); }
public void testNewProxy_goodMethodWithNotEnoughTime() throws Exception { SampleImpl target = new SampleImpl(9999); Sample proxy = service.newProxy(target, Sample.class, NOT_ENOUGH_MS, MILLISECONDS); Stopwatch stopwatch = Stopwatch.createStarted(); try { proxy.sleepThenReturnInput("x"); fail("no exception thrown"); } catch (UncheckedTimeoutException expected) { } assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(NOT_ENOUGH_MS, DELAY_MS * 2)); // Is it still computing away anyway? assertThat(target.finished).isFalse(); MILLISECONDS.sleep(ENOUGH_MS); assertThat(target.finished).isFalse(); }
@Test public void testAllocationOrderDoesNotOverFillWithMultipleSizes() { PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder( new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT) .setConfig(defaultBitmapConfig).build(), new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT) .setConfig(defaultBitmapConfig).build(), new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2) .setConfig(defaultBitmapConfig).build()); long byteSize = 0; while (!allocationOrder.isEmpty()) { PreFillType current = allocationOrder.remove(); byteSize += Util.getBitmapByteSize(current.getWidth(), current.getHeight(), current.getConfig()); } assertThat(byteSize).isIn(Range.atMost(poolSize + cacheSize)); }
@Test public void testAllocationOrderDoesNotOverFillWithMultipleSizesAndWeights() { PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder( new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT) .setConfig(defaultBitmapConfig).setWeight(4).build(), new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT) .setConfig(defaultBitmapConfig).build(), new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 3) .setConfig(defaultBitmapConfig).setWeight(3).build()); long byteSize = 0; while (!allocationOrder.isEmpty()) { PreFillType current = allocationOrder.remove(); byteSize += Util.getBitmapByteSize(current.getWidth(), current.getHeight(), current.getConfig()); } assertThat(byteSize).isIn(Range.atMost(poolSize + cacheSize)); }
public void testRunUninterruptiblyWithTimeout_goodRunnableWithEnoughTime() throws Exception { Stopwatch stopwatch = Stopwatch.createStarted(); service.runUninterruptiblyWithTimeout(GOOD_RUNNABLE, ENOUGH_MS, MILLISECONDS); assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); }
public void testRunWithTimeout_goodRunnableWithEnoughTime() throws Exception { Stopwatch stopwatch = Stopwatch.createStarted(); service.runWithTimeout(GOOD_RUNNABLE, ENOUGH_MS, MILLISECONDS); assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); }
public void testCallUninterruptiblyWithTimeout_goodCallableWithEnoughTime() throws Exception { Stopwatch stopwatch = Stopwatch.createStarted(); String result = service.callUninterruptiblyWithTimeout(GOOD_CALLABLE, ENOUGH_MS, MILLISECONDS); assertThat(result).isEqualTo(GOOD_CALLABLE_RESULT); assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); }
public void testCallWithTimeout_goodCallableWithEnoughTime() throws Exception { Stopwatch stopwatch = Stopwatch.createStarted(); String result = service.callWithTimeout(GOOD_CALLABLE, ENOUGH_MS, MILLISECONDS); assertThat(result).isEqualTo(GOOD_CALLABLE_RESULT); assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); }
public void testNewProxy_badMethodWithNotEnoughTime() throws Exception { SampleImpl target = new SampleImpl(9999); Sample proxy = service.newProxy(target, Sample.class, NOT_ENOUGH_MS, MILLISECONDS); Stopwatch stopwatch = Stopwatch.createStarted(); try { proxy.sleepThenThrowException(); fail("no exception thrown"); } catch (UncheckedTimeoutException expected) { } assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(NOT_ENOUGH_MS, DELAY_MS * 2)); }
public void testNewProxy_badMethodWithEnoughTime() throws Exception { SampleImpl target = new SampleImpl(DELAY_MS); Sample proxy = service.newProxy(target, Sample.class, ENOUGH_MS, MILLISECONDS); Stopwatch stopwatch = Stopwatch.createStarted(); try { proxy.sleepThenThrowException(); fail("no exception thrown"); } catch (SampleException expected) { } assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); }
public void testNewProxy_goodMethodWithEnoughTime() throws Exception { SampleImpl target = new SampleImpl(DELAY_MS); Sample proxy = service.newProxy(target, Sample.class, ENOUGH_MS, MILLISECONDS); Stopwatch stopwatch = Stopwatch.createStarted(); String result = proxy.sleepThenReturnInput("x"); assertThat(result).isEqualTo("x"); assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); assertThat(target.finished).isTrue(); }
public void testNewProxy_goodMethodWithNotEnoughTime() throws Exception { SampleImpl target = new SampleImpl(9999); Sample proxy = service.newProxy(target, Sample.class, NOT_ENOUGH_MS, MILLISECONDS); Stopwatch stopwatch = Stopwatch.createStarted(); try { proxy.sleepThenReturnInput("x"); fail("no exception thrown"); } catch (UncheckedTimeoutException expected) { } assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(NOT_ENOUGH_MS, DELAY_MS * 2)); // Is it still computing away anyway? assertThat(target.finished).isFalse(); MILLISECONDS.sleep(ENOUGH_MS); assertThat(target.finished).isFalse(); }
/** * Test with two agents using a solver at the same time. */ @Test public void twoSimultaneousComputationsTest() { final RealtimeClockController clock = (RealtimeClockController) sim.getModelProvider() .getModel(TimeModel.class); final List<LogEntry> log = new ArrayList<>(); clock.getEventAPI().addListener(new Listener() { @Override public void handleEvent(Event e) { log.add(logEntry(clock.getCurrentTime(), e.getEventType())); } }, RtClockEventType.values()); sim.start(); assertThat(log.get(0)).isEqualTo(logEntry(0, SWITCH_TO_SIM_TIME)); assertThat(log.get(1)).isEqualTo(logEntry(200, SWITCH_TO_REAL_TIME)); assertThat(log.get(2).eventType()).isEqualTo(SWITCH_TO_SIM_TIME); assertThat(log.get(2).time()).isIn(ImmutableSet.of(500L, 600L)); assertThat(log.get(3)).isEqualTo(logEntry(1000, SWITCH_TO_REAL_TIME)); assertThat(log.get(4).eventType()).isEqualTo(SWITCH_TO_SIM_TIME); assertThat(log.get(4).time()).isIn(ImmutableSet.of(1300L, 1400L)); assertThat(log.get(5)).isEqualTo(logEntry(3000, SWITCH_TO_REAL_TIME)); }
/** * Tests that the realtime model shuts down as soon as possible upon receiving * an interrupt. */ @Test public void testInterrupt() { final Thread main = Thread.currentThread(); final long tickLength = getModel().getTickLength(); Executors.newScheduledThreadPool(1).schedule(new Runnable() { @Override public void run() { main.interrupt(); } }, 10 * tickLength, TimeUnit.MILLISECONDS); getModel().start(); assertThat(getModel().getCurrentTime()) .isIn(Range.open(9L * tickLength, 11L * tickLength)); }