@Override public V put(final K key, final V value) throws CacheException { V previousValue; while (true) { previousValue = cache.get(key); if (previousValue == null) { if (cache.putIfAbsent(key, value)) { break; } } else { if (cache.replace(key, value)) { break; } } } return previousValue; }
@Test public void testSimpleReplace3ArgsWithLoaderAndWriter_absent() throws Exception { when(cacheLoader.load(eq(1))).thenAnswer(invocation -> null); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.replace(1, "un", "one"), is(false)); verifyZeroInteractions(cacheLoader); verifyZeroInteractions(cacheWriter); }
@Test public void testSimpleReplace2ArgsWithLoaderAndWriter_absent() throws Exception { when(cacheLoader.load(eq(1))).thenAnswer(invocation -> null); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.replace(1, "one"), is(false)); assertThat(testCache.containsKey(1), is(false)); verifyZeroInteractions(cacheLoader); verifyZeroInteractions(cacheWriter); }
@Test public void testSimpleReplace2ArgsWithLoaderAndWriter_existsInSor() throws Exception { when(cacheLoader.load(eq(1))).thenAnswer(invocation -> "un"); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.replace(1, "one"), is(false)); assertThat(testCache.containsKey(1), is(false)); verifyZeroInteractions(cacheLoader); verifyZeroInteractions(cacheWriter); }
@Test public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInSor() throws Exception { when(cacheLoader.load(eq(1))).thenAnswer(invocation -> "un"); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.replace(1, "un", "one"), is(false)); assertThat(testCache.containsKey(1), is(false)); verifyZeroInteractions(cacheLoader); verifyZeroInteractions(cacheWriter); }
@Test public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInStore_notEquals() throws Exception { testCache.put(1, "un"); reset(cacheWriter); assertThat(testCache.replace(1, "uno", "one"), is(false)); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un")); verifyZeroInteractions(cacheLoader); verifyZeroInteractions(cacheWriter); }
/** * Tries to set the global cleanup node id to current node. * * @param grid Grid. * @param metaCache Meta cache. * * @return True if successfully set the flag indicating that current node performs the cleanup; otherwise false. */ private boolean trySetGlobalCleanupFlag(Ignite grid, final Cache<CleanupNodeId, UUID> metaCache) { final UUID localNodeId = grid.cluster().localNode().id(); while (true) { // Get the node performing cleanup. UUID nodeId = metaCache.get(CLEANUP_NODE_ID); if (nodeId == null) { if (metaCache.putIfAbsent(CLEANUP_NODE_ID, localNodeId)) return true; // Successfully reserved cleanup to local node. // Failed putIfAbsent: someone else may have started cleanup. Retry the check. continue; } if (nodeId.equals(localNodeId)) return false; // Current node already performs cleanup. if (grid.cluster().node(nodeId) != null) return false; // Another node already performs cleanup and is alive. // Node that performs cleanup has disconnected. if (metaCache.replace(CLEANUP_NODE_ID, nodeId, localNodeId)) return true; // Successfully replaced disconnected node id with our id. // Replace failed: someone else started cleanup. return false; } }
@Test public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInStore() throws Exception { testCache.put(1, "un"); reset(cacheWriter); assertThat(testCache.replace(1, "un", "one"), is(true)); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one")); verifyZeroInteractions(cacheLoader); verify(cacheWriter, times(1)).write(new Eh107CacheLoaderWriter.Entry<Number, CharSequence>(1, "one")); }
@Test public void testSimpleReplace2ArgsWithLoaderAndWriter_existsInStore() throws Exception { testCache.put(1, "un"); reset(cacheWriter); assertThat(testCache.replace(1, "one"), is(true)); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one")); verifyZeroInteractions(cacheLoader); verify(cacheWriter, times(1)).write(new Eh107CacheLoaderWriter.Entry<Number, CharSequence>(1, "one")); }
@Override protected void linkRefreshTokenToAccessToken(RefreshToken rt, ServerAccessToken at) { super.linkRefreshTokenToAccessToken(rt, at); if (!isStoreJwtTokenKeyOnly()) { accessTokenCache.replace(at.getTokenKey(), at); } }
@Override public boolean replace(K key, V oldValue, V newValue) { return cache.replace( passingKeyTransformer.compact(key), passingValueTransformer.compact(oldValue), valueTransformer.compact(newValue)); }
@Test public void replace_3arg() throws Exception { long now = System.currentTimeMillis(); Date key = new Date(now); Date value = new Date(now); cache.put(key, value); Date nextValue = new Date(now + 1); assertTrue(cache.replace(key, value, nextValue)); assertSame(nextValue, cache.get(key)); }
@Test public void replace_3arg_NullKey() { try { assertFalse(cache.replace(null, "1", "2")); fail("should have thrown an exception - null key not allowed"); } catch (NullPointerException e) { //good } }
@Test public void replace_2arg_NullKey() { try { assertFalse(cache.replace(null, "")); fail("should have thrown an exception - null key not allowed"); } catch (NullPointerException e) { //good } }
@Test public void replace_2arg_NullValue() { try { assertFalse(cache.replace(1L, null)); fail("should have thrown an exception - null value not allowed"); } catch (NullPointerException e) { //good } }
@Test public void replace_3arg_NullValue2() { try { assertFalse(cache.replace(1L, "1", null)); fail("should have thrown an exception - null value not allowed"); } catch (NullPointerException e) { //good } }
@Test public void replace_3arg() throws Exception { Long key = System.currentTimeMillis(); String value = "value" + key; cache.put(key, value); String nextValue = "value" + key + 1; assertTrue(cache.replace(key, value, nextValue)); assertEquals(nextValue, cache.get(key)); }
private <K> void doReplace(Supplier<K> keySupplier, Cache<K, String> readCache, Cache<K, String> writeCache) { K key = keySupplier.get(); assertEquals(null, readCache.get(key)); assertFalse(writeCache.replace(key, "xxx")); assertEquals(null, writeCache.getAndPut(key, "one")); assertEquals("one", readCache.get(key)); assertTrue(writeCache.replace(key, "uno")); assertEquals("uno", readCache.get(key)); assertTrue(writeCache.remove(key)); assertEquals(null, readCache.get(key)); }