/** * Syntactic sugar for writing {@code stage.handle(fn).toCompletableFuture().unsafeJoin()}. * * @param stage the stage to handle the results * @param fn called to handle the result * @param <T> the type returned by the stage * @param <R> the type to be returned * @return the result of calling <tt>fn</tt> on the stage outcome. */ public static <T, R> R fold(CompletionStage<T> stage, BiFunction<T, Throwable, R> fn) { return unsafeJoin(stage.handle(fn)); } }
/** * Syntactic sugar for handling the result (either success or failure) from a {@link CompletionStage}. * * @param stage the stage to handle the results * @param success called if the stage returned a successful result * @param failure called if the stage failed * @param <T> the type returned by the stage * @param <R> the type to be returned * @return if the stage is successful, then the result of calling <tt>success</tt>, otherwise the result of * calling <tt>failure</tt> */ public static <T, R> R fold(CompletionStage<T> stage, Function<T, R> success, Function<Throwable, R> failure) { return unsafeJoin(stage.handle((val, err) -> (err != null) ? failure.apply(err) : success.apply(val))); }
@SuppressWarnings("unchecked") @Test public void simple_getBulk() throws ExecutionException, InterruptedException { final CompletionStage<Map<String, Optional<String>>> get1 = cache.getBulk("claira", "jasmin", "josie", "josie"); assertThat(get1, successful()); assertThat(unsafeJoin(get1).keySet(), containsInAnyOrder("claira", "jasmin", "josie")); assertThat(unsafeJoin(get1).values(), containsInAnyOrder(Optional.empty(), Optional.empty(), Optional.empty())); final CompletionStage<Boolean> put1 = cache.put("claira", "youtube", PutPolicy.PUT_ALWAYS); assertThat(put1, successfulWith(is(true))); final CompletionStage<Map<String, Optional<String>>> get2 = cache.getBulk("jasmin", "claira", "josie", "claira"); assertThat(get2, successful()); assertThat(unsafeJoin(get2).keySet(), containsInAnyOrder("claira", "jasmin", "josie")); assertThat(unsafeJoin(get2).values(), containsInAnyOrder(Optional.of("youtube"), Optional.empty(), Optional.empty())); final Map<MetricLabel, ? extends LongMetric> cacheMetrics = requestMetrics().allExternalCacheLongMetrics().get(CACHE_NAME); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_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(1L), is(1L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_MISSES, is(2L), is(5L))); assertThat(cacheMetrics, hasSize(is(5))); }
@Test public void simple_getBulkFactory() throws ExecutionException, InterruptedException { final CompletionStage<Map<String, String>> get1 = cache.getBulk( strings -> strings.stream().collect(Collectors.toMap(k -> k, k -> k + "-1")), "claira", "josie", "josie"); assertThat(get1, successful()); assertThat(unsafeJoin(get1).keySet(), containsInAnyOrder("claira", "josie")); assertThat(unsafeJoin(get1).values(), containsInAnyOrder("claira-1", "josie-1")); final CompletionStage<Map<String, String>> get2 = cache.getBulk( strings -> strings.stream().collect(Collectors.toMap(k -> k, k -> k + "-2")), "claira", "josie", "jasmin"); assertThat(get2, successful()); assertThat(unsafeJoin(get2).keySet(), containsInAnyOrder("claira", "josie", "jasmin")); assertThat(unsafeJoin(get2).values(), containsInAnyOrder("claira-1", "josie-1", "jasmin-2")); final Map<MetricLabel, ? extends LongMetric> cacheMetrics = requestMetrics().allExternalCacheLongMetrics().get(CACHE_NAME); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_GET_CALL, is(2L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_FACTORY_CALL, is(2L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_REMOVE_ALL_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_FACTORY_KEYS, is(2L), is(3L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_HITS, is(1L), is(2L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_MISSES, is(2L), is(3L))); assertThat(cacheMetrics, hasSize(is(6))); }
@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))); }
/** * 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_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))); }
@Test public void simple_put_remove() throws ExecutionException, InterruptedException { final CompletionStage<Void> rm1 = cache.remove("claira"); assertThat(rm1, successful()); assertThat(unsafeJoin(rm1), nullValue()); final CompletionStage<Boolean> put1 = cache.put("claira", "dancing", PutPolicy.PUT_ALWAYS); assertThat(put1, successfulWith(is(true))); final CompletionStage<Void> rm2 = cache.remove("claira"); assertThat(rm2, successful()); final CompletionStage<Boolean> put2 = cache.put("claira", "dancing", PutPolicy.PUT_ALWAYS); assertThat(put2, successfulWith(is(true))); final CompletionStage<Optional<String>> get1 = cache.get("claira"); assertThat(get1, successfulWith(is(Optional.of("dancing")))); final CompletionStage<Void> rm3 = cache.remove("josie", "claira", "jasmin"); assertThat(rm3, successful()); final CompletionStage<Optional<String>> get2 = cache.get("claira"); assertThat(get2, successfulWith(is(Optional.empty()))); final Map<MetricLabel, ? extends LongMetric> cacheMetrics = requestMetrics().allExternalCacheLongMetrics().get(CACHE_NAME); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_GET_CALL, is(2L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_PUT_CALL, is(2L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_REMOVE_CALL, is(3L), 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, hasMetric(MetricLabel.NUMBER_OF_MISSES, is(1L), is(1L))); assertThat(cacheMetrics, hasSize(is(6))); }
@Test public void simple_getBulk_function() throws ExecutionException, InterruptedException { final CompletionStage<Map<String, String>> get1 = cache.getBulk( keys -> Maps.asMap(keys, k -> k + "-1"), "claira"); assertThat(get1, successful()); assertThat(unsafeJoin(get1).keySet(), containsInAnyOrder("claira")); assertThat(unsafeJoin(get1).values(), containsInAnyOrder("claira-1")); final CompletionStage<Map<String, String>> get2 = cache.getBulk( keys -> Maps.asMap(keys, k -> k + "-2"), "claira", "josephine", "claira"); assertThat(get2, successful()); assertThat(unsafeJoin(get2).keySet(), containsInAnyOrder("claira", "josephine")); assertThat(unsafeJoin(get2).values(), containsInAnyOrder("claira-1", "josephine-2")); final Map<MetricLabel, ? extends LongMetric> cacheMetrics = requestMetrics().allExternalCacheLongMetrics().get(CACHE_NAME); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_FACTORY_CALL, is(2L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_GET_CALL, is(2L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_FACTORY_KEYS, is(2L), is(2L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_HITS, is(1L), is(1L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_MISSES, is(2L), is(2L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_REMOTE_GET, is(4L), is(4L))); assertThat(cacheMetrics, hasSize(is(6))); }
@Test public void directexternalcache_duplicate_obtain() { final DirectExternalCache<String> firstTime = vCacheFactory().getDirectExternalCache( "duplicate", dodgyPair("first"), new ExternalCacheSettingsBuilder().build()); final CompletionStage<Boolean> put1 = firstTime.put("key", "ignored", PUT_ALWAYS); assertThat(VCacheUtils.unsafeJoin(put1), is(true)); final CompletionStage<Optional<String>> get1 = firstTime.get("key"); assertThat(VCacheUtils.unsafeJoin(get1), is(Optional.of("first"))); final DirectExternalCache<String> secondTime = vCacheFactory().getDirectExternalCache( "duplicate", dodgyPair("second"), new ExternalCacheSettingsBuilder().build()); final CompletionStage<Optional<String>> get2 = secondTime.get("key"); assertThat(VCacheUtils.unsafeJoin(get2), is(Optional.of("second"))); }
@Test public void existing_removeAll_getBulk() { directCache().put("key", "existing", PutPolicy.PUT_ALWAYS); final CompletionStage<Optional<String>> get1 = cache().get("key"); assertThat(get1, successfulWith(is(Optional.of("existing")))); cache().removeAll(); final CompletionStage<Map<String, String>> get2 = cache().getBulk( strings -> strings.stream().collect(Collectors.toMap(k -> k, k -> k + "-new")), "key"); assertThat(get2, successful()); assertThat(unsafeJoin(get2).keySet(), containsInAnyOrder("key")); assertThat(unsafeJoin(get2).values(), containsInAnyOrder("key-new")); final Map<MetricLabel, ? extends LongMetric> cacheMetrics = requestMetrics().allExternalCacheLongMetrics().get(CACHE_NAME); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_GET_CALL, is(2L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_FACTORY_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_REMOVE_ALL_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_FACTORY_KEYS, is(1L), is(1L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_HITS, is(1L), is(1L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_MISSES, is(1L), is(1L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_REMOTE_GET, is(1L), is(1L))); assertThat(cacheMetrics, hasSize(is(7))); }
@Test public void existing_remove_getBulk() { directCache().put("key", "existing", PutPolicy.PUT_ALWAYS); final CompletionStage<Optional<String>> get1 = cache().get("key"); assertThat(get1, successfulWith(is(Optional.of("existing")))); cache().remove("key"); final CompletionStage<Map<String, String>> get2 = cache().getBulk( strings -> strings.stream().collect(Collectors.toMap(k -> k, k -> k + "-new")), "key", "extra"); assertThat(get2, successful()); assertThat(unsafeJoin(get2).keySet(), containsInAnyOrder("key", "extra")); assertThat(unsafeJoin(get2).values(), containsInAnyOrder("key-new", "extra-new")); final Map<MetricLabel, ? extends LongMetric> cacheMetrics = requestMetrics().allExternalCacheLongMetrics().get(CACHE_NAME); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_GET_CALL, is(2L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_FACTORY_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_REMOVE_CALL, is(1L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_FACTORY_KEYS, is(1L), is(2L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_HITS, is(1L), is(1L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_MISSES, is(1L), is(2L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_REMOTE_GET, is(2L), is(2L))); assertThat(cacheMetrics, hasSize(is(7))); }
@Test public void stablereadexternalcache_duplicate_obtain() { final StableReadExternalCache<String> firstTime = vCacheFactory().getStableReadExternalCache( "duplicate", dodgyPair("first"), new ExternalCacheSettingsBuilder().build()); final CompletionStage<Boolean> put1 = firstTime.put("key", "ignored", PUT_ALWAYS); assertThat(VCacheUtils.unsafeJoin(put1), is(true)); forceNewRequestContext(); final CompletionStage<Optional<String>> get1 = firstTime.get("key"); assertThat(VCacheUtils.unsafeJoin(get1), is(Optional.of("first"))); final StableReadExternalCache<String> secondTime = vCacheFactory().getStableReadExternalCache( "duplicate", dodgyPair("second"), new ExternalCacheSettingsBuilder().build()); forceNewRequestContext(); final CompletionStage<Optional<String>> get2 = secondTime.get("key"); assertThat(VCacheUtils.unsafeJoin(get2), is(Optional.of("second"))); }
@Test public void getSupplier_removeAll_getBulk_getBulk() { final CompletionStage<String> get1 = cache().get("vienna", () -> "sandwiches"); assertThat(get1, successfulWith(is("sandwiches"))); cache().removeAll(); final CompletionStage<Optional<String>> get2 = cache().get("vienna"); assertThat(get2, successfulWith(is(Optional.empty()))); final CompletionStage<Map<String, String>> get3 = cache().getBulk(keys -> Maps.asMap(keys, k -> k + "-1"), "vienna"); assertThat(get3, successful()); assertThat(unsafeJoin(get3).keySet(), containsInAnyOrder("vienna")); assertThat(unsafeJoin(get3).values(), containsInAnyOrder("vienna-1")); final CompletionStage<Map<String, String>> get4 = cache().getBulk(keys -> Maps.asMap(keys, k -> k + "-2"), "vienna"); assertThat(get4, successful()); assertThat(unsafeJoin(get4).keySet(), containsInAnyOrder("vienna")); assertThat(unsafeJoin(get4).values(), containsInAnyOrder("vienna-1")); final Map<MetricLabel, ? extends LongMetric> cacheMetrics = requestMetrics().allExternalCacheLongMetrics().get(CACHE_NAME); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_GET_CALL, is(4L), greaterThan(0L))); assertThat(cacheMetrics, hasMetric(MetricLabel.TIMED_FACTORY_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_FACTORY_KEYS, is(1L), is(1L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_HITS, is(1L), is(1L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_MISSES, is(3L), is(3L))); assertThat(cacheMetrics, hasMetric(MetricLabel.NUMBER_OF_REMOTE_GET, is(1L), is(1L))); assertThat(cacheMetrics, hasSize(is(8))); }
@Test public void transactionalExternalCache_normal_marshalling() { final ExternalCacheSettings settings = new ExternalCacheSettingsBuilder() .defaultTtl(Duration.ofSeconds(60)) .entryGrowthRateHint(ChangeRate.LOW_CHANGE) .dataChangeRateHint(ChangeRate.LOW_CHANGE) .entryCountHint(100) .build(); final TransactionalExternalCache<String> cache = vCacheFactory().getTransactionalExternalCache( "my-txn-cache", StringMarshalling.pair(), settings); assertThat(cache, notNullValue()); assertThat(cache.getName(), is("my-txn-cache")); assertThat(invocationsOfBegunTxns, is(0)); cache.removeAll(); assertThat(invocationsOfBegunTxns, is(1)); cache.put("three", "drei", PUT_ALWAYS); final CompletionStage<Optional<String>> get1 = cache.get("three"); assertThat(get1, successfulWith(is(Optional.of("drei")))); vCacheLifecycleManager().transactionSync(currentRequestContext()); assertThat(invocationsOfBegunTxns, is(1)); forceNewRequestContext(); final CompletionStage<Optional<String>> get2 = cache.get("three"); assertThat(invocationsOfBegunTxns, is(2)); unsafeJoin(get2.toCompletableFuture()).get(); assertThat(get2, successfulWith(is(Optional.of("drei")))); assertThat(invocationsOfBegunTxns, is(2)); }
@Test public void transactionalExternalCache_normal_marshaller() { final ExternalCacheSettings settings = new ExternalCacheSettingsBuilder() .defaultTtl(Duration.ofSeconds(60)) .entryGrowthRateHint(ChangeRate.LOW_CHANGE) .dataChangeRateHint(ChangeRate.LOW_CHANGE) .entryCountHint(100) .build(); @SuppressWarnings("deprecation") final TransactionalExternalCache<String> cache = vCacheFactory().getTransactionalExternalCache( "my-txn-cache", MarshallerFactory.stringMarshaller(), settings); assertThat(cache, notNullValue()); assertThat(cache.getName(), is("my-txn-cache")); assertThat(invocationsOfBegunTxns, is(0)); cache.removeAll(); assertThat(invocationsOfBegunTxns, is(1)); cache.put("three", "drei", PUT_ALWAYS); final CompletionStage<Optional<String>> get1 = cache.get("three"); assertThat(get1, successfulWith(is(Optional.of("drei")))); vCacheLifecycleManager().transactionSync(currentRequestContext()); assertThat(invocationsOfBegunTxns, is(1)); forceNewRequestContext(); final CompletionStage<Optional<String>> get2 = cache.get("three"); assertThat(invocationsOfBegunTxns, is(2)); unsafeJoin(get2.toCompletableFuture()).get(); assertThat(get2, successfulWith(is(Optional.of("drei")))); assertThat(invocationsOfBegunTxns, is(2)); }
@Test public void txnexternalcache_duplicate_obtain() { final TransactionalExternalCache<String> firstTime = vCacheFactory().getTransactionalExternalCache( "duplicate", dodgyPair("first"), new ExternalCacheSettingsBuilder().build()); firstTime.put("key", "ignored", PUT_ALWAYS); vCacheLifecycleManager().transactionSync(currentRequestContext()); forceNewRequestContext(); final CompletionStage<Optional<String>> get1 = firstTime.get("key"); assertThat(VCacheUtils.unsafeJoin(get1), is(Optional.of("first"))); final TransactionalExternalCache<String> secondTime = vCacheFactory().getTransactionalExternalCache( "duplicate", dodgyPair("second"), new ExternalCacheSettingsBuilder().build()); forceNewRequestContext(); final CompletionStage<Optional<String>> get2 = secondTime.get("key"); assertThat(VCacheUtils.unsafeJoin(get2), is(Optional.of("second"))); }
@Test public void handle_legal_recursive_get_supplier() { final ExternalCacheSettings settings = new ExternalCacheSettingsBuilder() .defaultTtl(Duration.ofSeconds(60)) .entryGrowthRateHint(ChangeRate.LOW_CHANGE) .dataChangeRateHint(ChangeRate.LOW_CHANGE) .entryCountHint(100) .build(); final StableReadExternalCache<String> cache = vCacheFactory().getStableReadExternalCache( "my-stable-read-cache", StringMarshalling.pair(), settings); final CompletionStage<String> get1 = cache.get("recursive", () -> "ignored-" + unsafeJoin(cache.get("recursive", () -> "2"))); assertThat(get1, successfulWith(is("2"))); }
@Test public void handle_legal_recursive_get_supplier2() { final ExternalCacheSettings settings = new ExternalCacheSettingsBuilder() .defaultTtl(Duration.ofSeconds(60)) .entryGrowthRateHint(ChangeRate.LOW_CHANGE) .dataChangeRateHint(ChangeRate.LOW_CHANGE) .entryCountHint(100) .build(); final StableReadExternalCache<String> cache = vCacheFactory().getStableReadExternalCache( "my-stable-read-cache", StringMarshalling.pair(), settings); final CompletionStage<String> get1 = cache.get("recursive", () -> unsafeJoin(cache.get("recursive2", () -> "pass"))); assertThat(get1, successfulWith(is("pass"))); } }