@Test public void simple_getBulkIdentified() 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<Map<String, Optional<IdentifiedValue<String>>>> get2 = cache.getBulkIdentified("claira", "josie", "jasmin"); assertThat(get2, successful()); final Map<String, Optional<IdentifiedValue<String>>> ivMap1 = unsafeJoin(get2); assertThat(ivMap1.keySet(), containsInAnyOrder("jasmin", "claira", "josie")); assertThat(ivMap1.get("claira"), is(Optional.empty())); assertThat(ivMap1.get("jasmin"), is(Optional.empty())); final IdentifiedValue<String> iv2 = ivMap1.get("josie").get(); assertThat(iv2.identifier(), notNullValue()); assertThat(iv2.identifier(), equalTo(iv1.identifier())); assertThat(iv2.value(), is("football")); 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_PUT_CALL, is(1L), 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, hasMetric(MetricLabel.NUMBER_OF_MISSES, is(1L), is(2L))); assertThat(cacheMetrics, hasSize(is(5))); }
@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_removeIf() 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.removeIf("josie", ivFirst.identifier()); 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_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(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))); }
@Test public void simple_getIdentifiedSupplier_removeIf() throws ExecutionException, InterruptedException { final CompletionStage<IdentifiedValue<String>> get1 = cache.getIdentified("josie", () -> "football"); assertThat(get1, successful()); final IdentifiedValue<String> iv1 = unsafeJoin(get1); assertThat(iv1.identifier(), notNullValue()); assertThat(iv1.value(), is("football")); final CompletionStage<Boolean> rm1 = cache.removeIf("josie", iv1.identifier()); assertThat(rm1, successfulWith(is(true))); final CompletionStage<Boolean> put1 = cache.put("josie", "football", PutPolicy.PUT_ALWAYS); assertThat(put1, successfulWith(is(true))); final CompletionStage<IdentifiedValue<String>> get2 = cache.getIdentified("josie", () -> "swimming"); assertThat(get2, successful()); final IdentifiedValue<String> iv2 = unsafeJoin(get2); assertThat(iv2.identifier(), notNullValue()); assertThat(iv2.value(), is("football")); final CompletionStage<Boolean> rm2 = cache.removeIf("josie", iv2.identifier()); assertThat(rm2, 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_REMOVE_CALL, is(2L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_PUT_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_REMOVE_ALL_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_SUPPLIER_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_HITS, is(1L), is(1L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_MISSES, is(1L), is(1L))); assertThat(cacheMetrics, hasSize(is(7))); }
assertThat(iv2.identifier(), notNullValue()); assertThat(iv2.value(), is("football")); final CompletionStage<Boolean> rm1 = cache.removeIf("josie", iv2.identifier()); assertThat(iv3.identifier(), notNullValue()); assertThat(iv3.value(), is("football")); final CompletionStage<Boolean> rm3 = cache.removeIf("josie", iv3.identifier());
/** * 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))); }
final V curCacheValue = identifiedValue.get().value(); if (!shouldRemove.test(curCacheValue)) { return fold(vcache.removeIf(key, identifiedValue.get().identifier()),
if (identifiedValue.isPresent()) { final CasIdentifier identifier = identifiedValue.get().identifier(); final V value = identifiedValue.get().value(); final V replacement = updater.apply(value);