@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 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 testReplaceIfMissing() throws Exception { final boolean replaced = cache.replaceIf("aqua", "water", "water2"); assertThat(replaced, is(false)); assertThat(cache.get("aqua"), is(Optional.empty())); }
@Test public void testRemoveMissing() throws Exception { cache.remove("aqua"); assertThat(cache.get("aqua"), is(Optional.empty())); }
@Test public void testRemoveIfMissing() throws Exception { final boolean removed = cache.removeIf("aqua", "water"); assertThat(removed, is(false)); assertThat(cache.get("aqua"), is(Optional.empty())); }
@Test public void testReplaceIfOkay() throws Exception { cache.put("aqua", "water"); final boolean replaced = cache.replaceIf("aqua", "water", "water2"); assertThat(replaced, is(true)); assertThat(cache.get("aqua"), is(Optional.of("water2"))); }
@Test public void testRemoveIfMismatch() throws Exception { cache.put("aqua", "water"); final boolean removed = cache.removeIf("aqua", "tea"); assertThat(removed, is(false)); assertThat(cache.get("aqua"), 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 testRemoveOkay() throws Exception { cache.put("aqua", "water"); cache.remove("aqua"); assertThat(cache.get("aqua"), is(Optional.empty())); }
@Test public void testReplaceIfMismatch() throws Exception { cache.put("aqua", "water"); final boolean replaced = cache.replaceIf("aqua", "tea", "water2"); assertThat(replaced, is(false)); assertThat(cache.get("aqua"), is(Optional.of("water"))); }
@Test public void testRemoveIfOkay() throws Exception { cache.put("aqua", "water"); final boolean removed = cache.removeIf("aqua", "water"); assertThat(removed, is(true)); assertThat(cache.get("aqua"), is(Optional.empty())); }
@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"))); }