public void testConditionalReplaceFromNonOwner() { initAndTest(); Cache<Object, String> nonOwner = getFirstNonOwner("k1"); boolean retval = nonOwner.replace("k1", "valueX", "value2"); if (testRetVals) assertFalse("Should not have replaced", retval); asyncWaitOnPrimary("k1", ReplaceCommand.class); assertOnAllCachesAndOwnership("k1", "value"); assertFalse(extractComponent(nonOwner, DistributionManager.class).getCacheTopology().isWriteOwner("k1")); retval = nonOwner.replace("k1", "value", "value2"); asyncWait("k1", cmd -> Stream.of(ReplaceCommand.class, PutKeyValueCommand.class) .anyMatch(clazz -> clazz.isInstance(cmd))); if (testRetVals) assertTrue("Should have replaced", retval); assertOnAllCachesAndOwnership("k1", "value2"); }
public void testPutFromNonOwner() { initAndTest(); Cache<Object, String> nonOwner = getFirstNonOwner("k1"); Object retval = nonOwner.put("k1", "value2"); asyncWait("k1", PutKeyValueCommand.class); if (testRetVals) assertEquals("value", retval); assertOnAllCachesAndOwnership("k1", "value2"); }
public void testReplaceFromNonOwner() { initAndTest(); Object retval = getFirstNonOwner("k1").replace("k1", "value2"); if (testRetVals) assertEquals("value", retval); // Replace going to backup owners becomes PKVC asyncWait("k1", cmd -> Stream.of(ReplaceCommand.class, PutKeyValueCommand.class) .anyMatch(clazz -> clazz.isInstance(cmd))); assertOnAllCachesAndOwnership("k1", "value2"); c1.clear(); asyncWait(null, ClearCommand.class); retval = getFirstNonOwner("k1").replace("k1", "value2"); if (testRetVals) assertNull(retval); assertRemovedOnAllCaches("k1"); }
public void testPutIfAbsentFromNonOwner() { initAndTest(); log.trace("Here it begins"); Object retval = getFirstNonOwner("k1").putIfAbsent("k1", "value2"); if (testRetVals) assertEquals("value", retval); asyncWaitOnPrimary("k1", PutKeyValueCommand.class); assertOnAllCachesAndOwnership("k1", "value"); c1.clear(); asyncWait(null, ClearCommand.class); retval = getFirstNonOwner("k1").putIfAbsent("k1", "value2"); asyncWait("k1", PutKeyValueCommand.class); assertOnAllCachesAndOwnership("k1", "value2"); if (testRetVals) assertNull(retval); }
public void testClear() throws InterruptedException { for (Cache<Object, String> c : caches) assertTrue(c.isEmpty()); for (int i = 0; i < 10; i++) { getOwners("k" + i)[0].put("k" + i, "value" + i); // There will be no caches to invalidate as this is the first command of the test asyncWait("k" + i, PutKeyValueCommand.class); assertOnAllCachesAndOwnership("k" + i, "value" + i); } // this will fill up L1 as well for (int i = 0; i < 10; i++) assertOnAllCachesAndOwnership("k" + i, "value" + i); for (Cache<Object, String> c : caches) assertFalse(c.isEmpty()); c1.clear(); asyncWait(null, ClearCommand.class); for (Cache<Object, String> c : caches) assertTrue(c.isEmpty()); }
public void testMergeFromNonOwner() { initAndTest(); // exception raised by the user RuntimeException mergeException = new RuntimeException("hi there"); expectException(RemoteException.class, () -> getFirstNonOwner("k1").merge("k1", "ex", (k, v) -> { throw mergeException; })); asyncWaitOnPrimary("k1", ReadWriteKeyCommand.class); assertOnAllCachesAndOwnership("k1", "value"); // merge function applied Object retval = getFirstNonOwner("k1").merge("k1", "value2", (v1, v2) -> "merged_" + v1 + "_" + v2); asyncWait("k1", ReadWriteKeyCommand.class); if (testRetVals) assertEquals("merged_value_value2", retval); assertOnAllCachesAndOwnership("k1", "merged_value_value2"); // remove when null retval = getFirstNonOwner("k1").merge("k1", "valueRem", (v1, v2) -> null); asyncWait("k1", ReadWriteKeyCommand.class); if (testRetVals) assertNull(retval); assertRemovedOnAllCaches("k1"); // put if absent retval = getFirstNonOwner("notThere").merge("notThere", "value2", (v1, v2) -> "merged_" + v1 + "_" + v2); asyncWait("notThere", ReadWriteKeyCommand.class); if (testRetVals) assertEquals("value2", retval); assertOnAllCachesAndOwnership("notThere", "value2"); } }
public void testConditionalRemoveFromNonOwner() { initAndTest(); log.trace("Here we start"); boolean retval = getFirstNonOwner("k1").remove("k1", "value2"); if (testRetVals) assertFalse("Should not have removed entry", retval); asyncWaitOnPrimary("k1", RemoveCommand.class); assertOnAllCachesAndOwnership("k1", "value"); assertEquals("value", caches.get(1).get("k1")); retval = getFirstNonOwner("k1").remove("k1", "value"); asyncWait("k1", RemoveCommand.class); if (testRetVals) assertTrue("Should have removed entry", retval); assertNull("expected null but received " + caches.get(1).get("k1"), caches.get(1).get("k1")); assertRemovedOnAllCaches("k1"); }
public void testBasicDistribution() throws Throwable { for (Cache<Object, String> c : caches) assertTrue(c.isEmpty()); final Object k1 = getKeyForCache(caches.get(1)); getOwners(k1)[0].put(k1, "value"); // No non-owners have requested the key, so no invalidations asyncWait(k1, PutKeyValueCommand.class); // should be available everywhere! assertOnAllCachesAndOwnership(k1, "value"); // and should now be in L1 if (l1CacheEnabled) { for (Cache<Object, String> c : caches) { if (isOwner(c, k1)) { assertIsInContainerImmortal(c, k1); } else { assertIsInL1(c, k1); } } } }
public void testComputeIfAbsentFromNonOwner() throws InterruptedException { // do nothing if value exists initAndTest(); Object retval = getFirstNonOwner("k1").computeIfAbsent("k1", (k) -> "computed_" + k); if (testRetVals) assertEquals("value", retval); // Since the command fails on primary it won't be replicated to the other nodes asyncWaitOnPrimary("k1", ComputeIfAbsentCommand.class); assertOnAllCachesAndOwnership("k1", "value"); // Compute key and add result value if absent retval = getFirstNonOwner("notExists").computeIfAbsent("notExists", (k) -> "computed_" + k); if (testRetVals) assertEquals("computed_notExists", retval); asyncWait("notExists", ComputeIfAbsentCommand.class); assertOnAllCachesAndOwnership("notExists", "computed_notExists"); // do nothing if function result is null retval = getFirstNonOwner("doNothing").computeIfAbsent("doNothing", k -> null); asyncWaitOnPrimary("doNothing", ComputeIfAbsentCommand.class); if (testRetVals) assertNull(retval); assertRemovedOnAllCaches("doNothing"); RuntimeException computeRaisedException = new RuntimeException("hi there"); SerializableFunction<Object, String> mappingToException = k -> { throw computeRaisedException; }; expectException(RemoteException.class, () -> getFirstNonOwner("somethingWrong").computeIfAbsent("somethingWrong", mappingToException)); }
public void testComputeFromNonOwner() throws InterruptedException { // compute function applied initAndTest(); Object retval = getFirstNonOwner("k1").compute("k1", (k, v) -> "computed_" + k + "_" + v); asyncWait("k1", ComputeCommand.class); if (testRetVals) assertEquals("computed_k1_value", retval); assertOnAllCachesAndOwnership("k1", "computed_k1_value"); // remove if after compute value is null retval = getFirstNonOwner("k1").compute("k1", (v1, v2) -> null); asyncWait("k1", ComputeCommand.class); if (testRetVals) assertNull(retval); assertRemovedOnAllCaches("k1"); // put computed value if absent retval = getFirstNonOwner("notThere").compute("notThere", (k, v) -> "add_" + k); asyncWait("notThere", ComputeCommand.class); if (testRetVals) assertEquals("add_notThere", retval); assertOnAllCachesAndOwnership("notThere", "add_notThere"); RuntimeException computeRaisedException = new RuntimeException("hi there"); SerializableBiFunction<Object, Object, String> mappingToException = (k, v) -> { throw computeRaisedException; }; expectException(RemoteException.class, () -> getFirstNonOwner("k1").compute("k1", mappingToException)); }
public void testComputeIfPresentFromNonOwner() throws InterruptedException { // compute function applied initAndTest(); Object retval = getFirstNonOwner("k1").computeIfPresent("k1", (k, v) -> "computed_" + k + "_" + v); if (testRetVals) assertEquals("computed_k1_value", retval); asyncWait("k1", ComputeCommand.class); assertOnAllCachesAndOwnership("k1", "computed_k1_value"); RuntimeException computeRaisedException = new RuntimeException("hi there"); SerializableBiFunction<Object, Object, String> mappingToException = (k, v) -> { throw computeRaisedException; }; expectException(RemoteException.class, () -> getFirstNonOwner("k1").computeIfPresent("k1", mappingToException)); // remove if after compute value is null retval = getFirstNonOwner("k1").computeIfPresent("k1", (v1, v2) -> null); asyncWait("k1", ComputeCommand.class); if (testRetVals) assertNull(retval); assertRemovedOnAllCaches("k1"); // do nothing if absent retval = getFirstNonOwner("notThere").computeIfPresent("notThere", (k, v) -> "add_" + k); asyncWaitOnPrimary("notThere", ComputeCommand.class); if (testRetVals) assertNull(retval); assertRemovedOnAllCaches("notThere"); }
public void testLockedStreamSetValue() { int size = 5; for (int i = 0; i < size; i++) { getOwners("k" + i)[0].put("k" + i, "value" + i); // There will be no caches to invalidate as this is the first command of the test asyncWait("k" + i, PutKeyValueCommand.class); assertOnAllCachesAndOwnership("k" + i, "value" + i); } c1.getAdvancedCache().lockedStream().forEach((c, e) -> e.setValue(e.getValue() + "-changed")); for (int i = 0; i < size; i++) { String key = "k" + i; asyncWait(key, c -> commandIsPutForKey(key, c)); Cache<Object, String>[] caches = getOwners(key); for (Cache<Object, String> cache : caches) { assertEquals("value" + i + "-changed", cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL).get(key)); } } }
public void testLockedStreamPutValue() { int size = 5; for (int i = 0; i < size; i++) { getOwners("k" + i)[0].put("k" + i, "value" + i); // There will be no caches to invalidate as this is the first command of the test asyncWait("k" + i, PutKeyValueCommand.class); assertOnAllCachesAndOwnership("k" + i, "value" + i); } c1.getAdvancedCache().lockedStream().forEach((c, e) -> c.put(e.getKey(), e.getValue() + "-changed")); for (int i = 0; i < size; i++) { String key = "k" + i; asyncWait(key, c -> commandIsPutForKey(key, c)); Cache<Object, String>[] caches = getOwners(key); for (Cache<Object, String> cache : caches) { assertEquals("value" + i + "-changed", cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL).get(key)); } } }