private <K> void testReadOnMissingValues(K[] keys, FunctionalMap.ReadOnlyMap<K, String> ro, ReadMethod method) throws Exception { tm.begin(); for (K key : keys) { Assert.assertEquals(ro.eval(key, view -> view.find().isPresent()).join(), Boolean.FALSE); } tm.commit(); tm.begin(); for (K key : keys) { expectExceptionNonStrict(CompletionException.class, CacheException.class, NoSuchElementException.class, () -> method.eval(key, ro, EntryView.ReadEntryView::get)); // The first exception should cause the whole transaction to fail assertEquals(Status.STATUS_MARKED_ROLLBACK, tm.getStatus()); tm.rollback(); break; } if (tm.getStatus() == Status.STATUS_ACTIVE) { tm.commit(); } }
@DataProvider(name = "readMethods") public static Object[][] methods() { return Stream.of(ReadMethod.values()).map(m -> new Object[] { m }).toArray(Object[][]::new); }
@Test(dataProvider = "owningModeAndReadMethod") public void testReadLoad(boolean isOwner, ReadMethod method) { Object key = getKey(isOwner, DIST); assertTrue(method.eval(key, ro, view -> { assertFalse(view.find().isPresent()); return true; })); // we can't add from read-only cache, so we put manually: cache(0, DIST).put(key, "value"); caches(DIST).forEach(cache -> assertEquals(cache.get(key), "value", getAddress(cache).toString())); caches(DIST).forEach(cache -> { if (cache.getAdvancedCache().getDistributionManager().getLocality(key).isLocal()) { assertTrue(cacheContainsKey(key, cache), getAddress(cache).toString()); } else { assertFalse(cacheContainsKey(key, cache), getAddress(cache).toString()); } }); assertEquals(method.eval(key, ro, view -> { assertTrue(view.find().isPresent()); assertEquals(view.get(), "value"); return "OK"; }), "OK"); }
@Test(dataProvider = "owningModeAndReadMethod") public void testReadLoad(boolean isSourceOwner, ReadMethod method) { Object key = getKey(isSourceOwner, DIST); List<Cache<Object, Object>> owners = caches(DIST).stream() .filter(cache -> cache.getAdvancedCache().getDistributionManager().getLocality(key).isLocal()) .collect(Collectors.toList()); assertTrue(method.eval(key, ro, view -> { assertFalse(view.find().isPresent()); return true; })); // we can't add from read-only cache, so we put manually: cache(0, DIST).put(key, "value"); caches(DIST).forEach(cache -> assertEquals(cache.get(key), "value", getAddress(cache).toString())); caches(DIST).forEach(cache -> cache.evict(key)); caches(DIST).forEach(cache -> assertFalse(cache.getAdvancedCache().getDataContainer().containsKey(key), getAddress(cache).toString())); owners.forEach(cache -> { Set<DummyInMemoryStore> stores = cache.getAdvancedCache().getComponentRegistry().getComponent(PersistenceManager.class).getStores(DummyInMemoryStore.class); DummyInMemoryStore store = stores.iterator().next(); assertTrue(store.contains(key), getAddress(cache).toString()); }); assertEquals(method.eval(key, ro, view -> { assertTrue(view.find().isPresent()); assertEquals(view.get(), "value"); return "OK"; }), "OK"); } }
@Test(dataProvider = "readMethods") public void testReadLoadLocal(ReadMethod method) { Integer key = 1; assertTrue((Boolean) method.eval(key, lro, view -> { assertFalse(view.find().isPresent()); return true; })); // we can't add from read-only cache, so we put manually: Cache<Integer, String> cache = cacheManagers.get(0).getCache(); cache.put(key, "value"); assertEquals(cache.get(key), "value"); assertEquals(method.eval(key, lro, view -> { assertTrue(view.find().isPresent()); assertEquals(view.get(), "value"); return "OK"; }), "OK"); }
@Test(dataProvider = "owningModeAndReadMethod") public void testReadLoads(boolean isOwner, ReadMethod method) throws Exception { Object[] keys = getKeys(isOwner, NUM_KEYS); for (Object key : keys) { cache(0, DIST).put(key, key); } tm.begin(); for (Object key : keys) { assertEquals(method.eval(key, ro, e -> { assertTrue(e.find().isPresent()); assertEquals(e.get(), e.key()); return "OK"; }), "OK"); } tm.commit(); }
@Test(dataProvider = "readMethods") public void testReadLoadsLocal(ReadMethod method) throws Exception { Integer[] keys = INT_KEYS; for (Integer key : keys) { cache(0).put(key, key); } tm.begin(); for (Integer key : keys) { assertEquals(method.eval(key, lro, e -> { assertTrue(e.find().isPresent()); assertEquals(e.get(), e.key()); return "OK"; }), "OK"); } tm.commit(); }
@Test(dataProvider = "owningModeAndReadMethod") public void testReadLoad(boolean isOwner, ReadMethod method) { Object key = getKey(isOwner, SCATTERED); assertTrue(method.eval(key, sro, view -> { assertFalse(view.find().isPresent()); return true; })); // we can't add from read-only cache, so we put manually: cache(0, SCATTERED).put(key, "value"); caches(SCATTERED).forEach(cache -> assertEquals(cache.get(key), "value", getAddress(cache).toString())); Utils.assertOwnershipAndNonOwnership(caches(SCATTERED), key); assertEquals(method.eval(key, sro, view -> { assertTrue(view.find().isPresent()); assertEquals(view.get(), "value"); return "OK"; }), "OK"); }
protected <K> void testReadOnMissingValue(K key, FunctionalMap.ReadOnlyMap<K, String> ro, ReadMethod method) { assertEquals(ro.eval(key, view -> view.find().isPresent()).join(), Boolean.FALSE); expectExceptionNonStrict(CompletionException.class, CacheException.class, NoSuchElementException.class, () -> method.eval(key, ro, view -> view.get()) ); }
@Test(dataProvider = "owningModeAndReadMethod") public void testReadsAfterMods(boolean isOwner, ReadMethod method) throws Exception { Object KEY = getKey(isOwner, DIST); cache(0, DIST).put(KEY, "a"); tm.begin(); assertEquals("a", rw.eval(KEY, append("b")).join()); assertEquals("ab", rw.evalMany(Collections.singleton(KEY), append("c")).findAny().get()); assertEquals(null, rw.eval("otherKey", append("d")).join()); assertEquals("abc", method.eval(KEY, ro, MarshallableFunctions.returnReadOnlyFindOrNull())); tm.commit(); }
@DataProvider(name = "owningModeAndReadMethod") public static Object[][] owningModeAndMethod() { return Stream.of(Boolean.TRUE, Boolean.FALSE) .flatMap(isSourceOwner -> Stream.of(ReadMethod.values()) .map(method -> new Object[] { isSourceOwner, method })) .toArray(Object[][]::new); }