@Test public void withInitiallyDelayedFixedRateTask() throws InterruptedException { Assume.group(TestGroup.PERFORMANCE); ctx = new AnnotationConfigApplicationContext(FixedRateTaskConfig_withInitialDelay.class); Thread.sleep(1950); AtomicInteger counter = ctx.getBean(AtomicInteger.class); // The @Scheduled method should have been called at least once but // not more times than the delay allows. assertThat(counter.get(), both(greaterThan(0)).and(lessThanOrEqualTo(10))); }
@Test(dataProvider = "caches") @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void computeIfAbsent_error(Map<Integer, Integer> map, CacheContext context) { try { map.computeIfAbsent(context.absentKey(), key -> { throw new Error(); }); } catch (Error e) {} assertThat(map, is(equalTo(context.original()))); assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); assertThat(map.computeIfAbsent(context.absentKey(), key -> key), is(context.absentKey())); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(loader = { Loader.NEGATIVE, Loader.BULK_NEGATIVE }, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void getAll_iterable_empty(LoadingCache<Integer, Integer> cache, CacheContext context) { Map<Integer, Integer> result = cache.getAll(ImmutableList.of()); assertThat(result.size(), is(0)); assertThat(context, both(hasMissCount(0)).and(hasHitCount(0))); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void computeIfAbsent_absent(Map<Integer, Integer> map, CacheContext context) { assertThat(map.computeIfAbsent(context.absentKey(), key -> context.absentValue()), is(context.absentValue())); assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0))); assertThat(map.get(context.absentKey()), is(context.absentValue())); assertThat(map.size(), is(1 + context.original().size())); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(loader = Loader.NULL) public void getFunc_absent_null(AsyncCache<Integer, Integer> cache, CacheContext context) { Integer key = context.absentKey(); CompletableFuture<Integer> valueFuture = cache.get(key, k -> null); assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); assertThat(valueFuture.isDone(), is(true)); assertThat(cache.getIfPresent(key), is(nullValue())); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(loader = { Loader.NEGATIVE, Loader.BULK_NEGATIVE }, population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void getAll_present_full(LoadingCache<Integer, Integer> cache, CacheContext context) { Map<Integer, Integer> result = cache.getAll(context.original().keySet()); assertThat(result, is(equalTo(context.original()))); assertThat(context, both(hasMissCount(0)).and(hasHitCount(result.size()))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0))); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void compute_absent(Map<Integer, Integer> map, CacheContext context) { assertThat(map.compute(context.absentKey(), (key, value) -> context.absentValue()), is(context.absentValue())); assertThat(context, both(hasMissCount(0)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0))); assertThat(map.get(context.absentKey()), is(context.absentValue())); assertThat(map.size(), is(1 + context.original().size())); }
@CacheSpec @CheckNoWriter @Test(dataProvider = "caches") public void get_absent_null(LoadingCache<Integer, Integer> cache, CacheContext context) { assertThat(cache.get(context.absentKey(), k -> null), is(nullValue())); assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(loader = { Loader.BULK_NEGATIVE_EXCEEDS }, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void getAll_exceeds(AsyncLoadingCache<Integer, Integer> cache, CacheContext context) { Map<Integer, Integer> result = cache.getAll(context.absentKeys()).join(); assertThat(result.keySet(), equalTo(context.absentKeys())); assertThat(cache.synchronous().estimatedSize(), is(greaterThan(context.initialSize() + context.absentKeys().size()))); assertThat(context, both(hasMissCount(result.size())).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0))); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void getIfPresent_absent(Cache<Integer, Integer> cache, CacheContext context) { assertThat(cache.getIfPresent(context.absentKey()), is(nullValue())); assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0))); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(loader = Loader.NULL) public void get_absent_null(LoadingCache<Integer, Integer> cache, CacheContext context) { assertThat(cache.get(context.absentKey()), is(nullValue())); assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); }
@CacheSpec @CheckNoWriter @Test(dataProvider = "caches") public void getBiFunc_absent_cancelled(AsyncCache<Integer, Integer> cache, CacheContext context) { CompletableFuture<Integer> cancelledFuture = new CompletableFuture<>(); cache.get(context.absentKey(), (k, executor) -> cancelledFuture); cancelledFuture.cancel(true); assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); assertThat(cache.getIfPresent(context.absentKey()), is(nullValue())); }
@Test(dataProvider = "caches") @CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }) public void computeIfPresent_error(Map<Integer, Integer> map, CacheContext context) { try { map.computeIfPresent(context.firstKey(), (key, value) -> { throw new Error(); }); } catch (Error e) {} assertThat(map, is(equalTo(context.original()))); assertThat(context, both(hasMissCount(0)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); assertThat(map.computeIfPresent(context.firstKey(), (k, v) -> -k), is(-context.firstKey())); }
@CacheSpec @CheckNoWriter @Test(dataProvider = "caches") public void getFunc_absent_failure(AsyncCache<Integer, Integer> cache, CacheContext context) { CompletableFuture<Integer> valueFuture = cache.get(context.absentKey(), k -> { throw new IllegalStateException(); }); assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); assertThat(valueFuture.isCompletedExceptionally(), is(true)); assertThat(cache.getIfPresent(context.absentKey()), is(nullValue())); }
@CacheSpec @CheckNoWriter @Test(dataProvider = "caches", expectedExceptions = IllegalStateException.class) public void get_throwsException(Cache<Integer, Integer> cache, CacheContext context) { try { cache.get(context.absentKey(), key -> { throw new IllegalStateException(); }); } finally { assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); } }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(loader = { Loader.NEGATIVE, Loader.BULK_NEGATIVE }, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void getAll_absent(LoadingCache<Integer, Integer> cache, CacheContext context) { Map<Integer, Integer> result = cache.getAll(context.absentKeys()); int count = context.absentKeys().size(); int loads = context.loader().isBulk() ? 1 : count; assertThat(result.size(), is(count)); assertThat(context, both(hasMissCount(count)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(loads)).and(hasLoadFailureCount(0))); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void refresh_absent(LoadingCache<Integer, Integer> cache, CacheContext context) { cache.refresh(context.absentKey()); assertThat(cache.estimatedSize(), is(1 + context.initialSize())); assertThat(context, both(hasMissCount(0)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0))); // records a hit assertThat(cache.get(context.absentKey()), is(-context.absentKey())); }
@CacheSpec @CheckNoWriter @Test(dataProvider = "caches") public void getFunc_absent(AsyncCache<Integer, Integer> cache, CacheContext context) { Integer key = context.absentKey(); CompletableFuture<Integer> value = cache.get(key, k -> context.absentValue()); assertThat(value, is(futureOf(context.absentValue()))); assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0))); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(executor = CacheExecutor.DIRECT, loader = Loader.EXCEPTIONAL, removalListener = { Listener.DEFAULT, Listener.REJECTING }, population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }) public void refresh_failure(LoadingCache<Integer, Integer> cache, CacheContext context) { // Shouldn't leak exception to caller and should retain stale entry cache.refresh(context.absentKey()); cache.refresh(context.firstKey()); assertThat(cache.estimatedSize(), is(context.initialSize())); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(2))); }
private static void testLoggingOfWriteErrorAndOriginalErrorWhenUnknownFailure( Neo4jError original ) throws Exception { RuntimeException outputError = new RuntimeException( "Output failed" ); AssertableLogProvider logProvider = emulateFailureWritingError( original, outputError ); logProvider.assertExactly( inLog( "Test" ).error( startsWith( "Unable to send error back to the client" ), both( equalTo( outputError ) ).and( hasSuppressed( original.cause() ) ) ) ); }