@SuppressWarnings("ConstantConditions") @Test public void check_null_detection_with_cas() { final CompletionStage<IdentifiedValue<String>> geti = cache.getIdentified("temp", () -> "value"); assertThat(geti, successful()); assertThat(cache.replaceIf("temp", unsafeJoin(geti).identifier(), null), not(successful())); final Map<MetricLabel, ? extends LongMetric> cacheMetrics = requestMetrics().allExternalCacheLongMetrics().get(CACHE_NAME); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_IDENTIFIED_GET_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_IDENTIFIED_REPLACE_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_SUPPLIER_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_REMOVE_ALL_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_FAILED_IDENTIFIED_REPLACE, is(1L), is(1L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_MISSES, is(1L), is(1L))); assertThat(cacheMetrics, hasSize(is(6))); }
/** * Tests where the cas id is genuinely unique, and not just a copy of the original value. */ @Test public void exact_getIdentified_replaceIf() throws ExecutionException, InterruptedException { final CompletionStage<Boolean> putFirst = cache.put("josie", "football", PutPolicy.PUT_ALWAYS); assertThat(putFirst, successfulWith(is(true))); final CompletionStage<Optional<IdentifiedValue<String>>> getFirst = cache.getIdentified("josie"); assertThat(getFirst, successful()); final IdentifiedValue<String> ivFirst = unsafeJoin(getFirst).get(); assertThat(ivFirst.identifier(), notNullValue()); assertThat(ivFirst.value(), is("football")); final CompletionStage<Boolean> putSecond = cache.put("josie", "football", PutPolicy.PUT_ALWAYS); assertThat(putSecond, successfulWith(is(true))); final CompletionStage<Boolean> rm1 = cache.replaceIf("josie", ivFirst.identifier(), "afl"); assertThat(rm1, successfulWith(is(false))); final Map<MetricLabel, ? extends LongMetric> cacheMetrics = requestMetrics().allExternalCacheLongMetrics().get(CACHE_NAME); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_IDENTIFIED_GET_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_IDENTIFIED_REPLACE_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_PUT_CALL, is(2L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_REMOVE_ALL_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_HITS, is(1L), is(1L))); assertThat(cacheMetrics, hasSize(is(5))); }
@Test public void simple_getIdentified_replaceIf() throws ExecutionException, InterruptedException { final CompletionStage<Boolean> put1 = cache.put("josie", "football", PutPolicy.PUT_ALWAYS); assertThat(put1, successfulWith(is(true))); final CompletionStage<Optional<IdentifiedValue<String>>> get1 = cache.getIdentified("josie"); assertThat(get1, successful()); final IdentifiedValue<String> iv1 = unsafeJoin(get1).get(); assertThat(iv1.identifier(), notNullValue()); assertThat(iv1.value(), is("football")); final CompletionStage<Boolean> rm1 = cache.replaceIf("josie", iv1.identifier(), "soccer"); assertThat(rm1, successfulWith(is(true))); final CompletionStage<Boolean> put2 = cache.put("josie", "football", PutPolicy.PUT_ALWAYS); assertThat(put2, successfulWith(is(true))); final CompletionStage<Optional<IdentifiedValue<String>>> get3 = cache.getIdentified("josie"); assertThat(get3, successful()); final IdentifiedValue<String> iv3 = unsafeJoin(get3).get(); assertThat(iv3.identifier(), notNullValue()); assertThat(iv3.value(), is("football")); final CompletionStage<Boolean> rm3 = cache.removeIf("josie", iv3.identifier()); assertThat(rm3, successfulWith(is(true))); final Map<MetricLabel, ? extends LongMetric> cacheMetrics = requestMetrics().allExternalCacheLongMetrics().get(CACHE_NAME); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_IDENTIFIED_GET_CALL, is(2L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_IDENTIFIED_REPLACE_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_IDENTIFIED_REMOVE_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_PUT_CALL, is(2L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_REMOVE_ALL_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_HITS, is(2L), is(2L))); assertThat(cacheMetrics, hasSize(is(6))); }
final V replacement = updater.apply(value); return fold(vcache.replaceIf(key, identifier, replacement),