/** * Returns the values that are associated with the specified keys. It is equivalent to calling * {@link #getBulk(Function, Iterable)} passing {@code factory} and {@code Arrays.asList(keys)} as the parameters. * * @param factory used to generate the values for the keys that do not have entries. The factory must return a * map containing only a non-null value for each supplied key. Extra keys are not allowed. * @param keys the keys to retrieve * @return A {@link Map} that is keyed on the {@code keys} specified. Each entry in the {@link Map} will have * the value associated with the key. * @see #getBulk(Function, Iterable) */ @SuppressWarnings("unchecked") default Map<K, V> getBulk(Function<Set<K>, Map<K, V>> factory, K... keys) { return getBulk(factory, Arrays.asList(keys)); }
@Test public void testRemoveAll() throws Exception { cache.put("k1", "v1"); cache.put("k2", "v2"); assertThat(cache.get("k1"), is(Optional.of("v1"))); assertThat(cache.get("k2"), is(Optional.of("v2"))); cache.removeAll(); assertThat(cache.get("k1"), is(Optional.empty())); assertThat(cache.get("k2"), is(Optional.empty())); }
@Test public void testPut() throws Exception { final Optional<String> get1 = cache.get("aqua"); assertThat(get1, notNullValue()); assertThat(get1, is(Optional.empty())); cache.put("aqua", "water"); final Optional<String> put1 = cache.get("aqua"); assertThat(put1, notNullValue()); assertThat(put1, is(Optional.of("water"))); }
@Test public void testPutIfAbsent() throws Exception { final Optional<String> get1 = cache.get("aqua"); assertThat(get1, notNullValue()); assertThat(get1, is(Optional.empty())); final Optional<String> put1 = cache.putIfAbsent("aqua", "water"); assertThat(put1, notNullValue()); assertThat(put1, is(Optional.empty())); final Optional<String> put2 = cache.putIfAbsent("aqua", "water2"); assertThat(put2, notNullValue()); assertThat(put2, is(Optional.of("water"))); final Optional<String> get2 = cache.get("aqua"); assertThat(get2, notNullValue()); assertThat(get2, is(Optional.of("water"))); }
@Test public void testGet() throws Exception { final Optional<String> result1 = cache.get("Parlez-Vouis Francais"); assertThat(result1, notNullValue()); assertThat(result1, is(Optional.empty())); final String supplied = cache.get("Parlez-Vouis Francais", () -> "Magic Fountain"); assertThat(supplied, is("Magic Fountain")); final Optional<String> result2 = cache.get("Parlez-Vouis Francais"); assertThat(result2, notNullValue()); assertThat(result2, is(Optional.of("Magic Fountain"))); }
@Test public void handle_legal_recursion_factory() { final LocalCacheOperations<String, String> cache = createCache("abc"); final Map<String, String> result = cache.getBulk( strings1 -> cache.getBulk(strings2 -> { return strings2.stream().collect(Collectors.toMap(k -> k, k -> k + "-2")); }, strings1), "first"); assertThat(result, hasEntry("first", "first-2")); assertThat(result.keySet(), contains("first")); } }
@Test public void deal_with_deadlock_scenario() { // T1: get with supplier and await signal // T2: get with supplier that returns "T2" // T1: signalled, supplier returns "T1" // Confirm both T1 & T2 return "T1" final Barrier blockedInSupplier = new Barrier(); final Barrier resumeInSupplier = new Barrier(); final CompletableFuture<String> t1Result = runAndWaitForStart(() -> cache.get("alive", () -> { log.info("{}: About to await on barrierBulk", Thread.currentThread().getName()); blockedInSupplier.signal(); resumeInSupplier.await(); log.info("{}: resuming", Thread.currentThread().getName()); return "T1"; })); blockedInSupplier.await(); final CompletableFuture<String> t2Result = runAndWaitForStart(() -> cache.get("alive", () -> "T2")); // Wait for the result from T2 to ensure it has run to completion. assertThat(t2Result, successfulWith(is("T2"))); log.info("{}: About to signal on barrierBulk", Thread.currentThread().getName()); resumeInSupplier.signal(); //Wait until T2 cache.get calls join log.info("{}: and now", Thread.currentThread().getName()); assertThat(t1Result, successfulWith(is("T2"))); }
@Test public void getBulkFactory() { final Map<String, String> get1 = cache.getBulk( strings -> strings.stream().collect(Collectors.toMap(k -> k, k -> k + "-1"))); assertThat(get1.keySet(), empty()); assertThat(get1.values(), empty()); final Map<String, String> get2 = cache.getBulk( strings -> strings.stream().collect(Collectors.toMap(k -> k, k -> k + "-1")), "claira", "josie", "josie"); assertThat(get2.keySet(), containsInAnyOrder("claira", "josie")); assertThat(get2.values(), containsInAnyOrder("claira-1", "josie-1")); final Map<String, String> get3 = cache.getBulk( strings -> strings.stream().collect(Collectors.toMap(k -> k, k -> k + "-2")), "claira", "josie", "jasmin"); assertThat(get3.keySet(), containsInAnyOrder("claira", "josie", "jasmin")); assertThat(get3.values(), containsInAnyOrder("claira-1", "josie-1", "jasmin-2")); }