private void assertKeysValues(Map<?, ?> expected, Map<?, ?> atomicMap) { for (Entry<?, ?> entry : expected.entrySet()) { assertKeyValue(atomicMap, entry.getKey(), entry.getValue()); } }
@SuppressWarnings("UnusedDeclaration") public void testNoTx() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); final Cache<String, Object> cache2 = cache(1, "atomic"); assertSize(cache1, 0); assertSize(cache2, 0); final Map<String, String> map = createAtomicMap(cache1, "testNoTx", true); map.put("existing", "existing"); map.put("blah", "blah"); Map<Object, Object> expectedMap = createMap("existing", "existing", "blah", "blah"); assertMap(expectedMap, map); }
public void testConcurrentWritesAndIteration() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); assertSize(cache1, 0); final Map<String, String> map = createAtomicMap(cache1, "testConcurrentWritesAndIteration", true); assertSize(map, 0); runConcurrently(1, TimeUnit.MINUTES, () -> { Map<String, String> map1 = createAtomicMap(cache1, "testConcurrentWritesAndIteration", true); for (int i = 0; i < 250; i++) { map1.put("key-" + i, "value-" + i); } }, () -> { Map<String, String> map12 = createAtomicMap(cache1, "testConcurrentWritesAndIteration", true); for (int i = 0; i < 250; i++) { map12.keySet(); } }); }
@SuppressWarnings("UnusedDeclaration") public void testReadUncommittedValues() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); final Cache<String, Object> cache2 = cache(1, "atomic"); assertSize(cache1, 0); assertSize(cache2, 0); Map<String, String> map = createAtomicMap(cache1, "testReadUncommittedValues"); tm(cache1).begin(); map.put("key one", "value one"); map.put("blah", "blah"); Map<Object, Object> expectedMap = createMap("key one", "value one", "blah", "blah"); assertMap(expectedMap, map); Map<String, String> sameAsMap = createAtomicMap(cache1, "testReadUncommittedValues"); assertMap(expectedMap, sameAsMap); tm(cache1).commit(); assertMap(expectedMap, map); assertMap(expectedMap, sameAsMap); expectedMap = createMap("blah", "blah"); tm(cache1).begin(); map = createAtomicMap(cache1, "testReadUncommittedValues"); String removed = map.remove("key one");
private void testInsertDeleteCycle(MagicKey key) throws Exception { final Cache<MagicKey, Object> cache1 = cache(0, "atomic"); final Cache<MagicKey, Object> cache2 = cache(1, "atomic"); assertSize(cache1, 0); assertSize(cache2, 0); { TestingUtil.getTransactionManager(cache1).begin(); Map<String, Object> am = createAtomicMap(cache1, key); am.put("k1", "v1"); TestingUtil.getTransactionManager(cache1).commit(); } { TestingUtil.getTransactionManager(cache1).begin(); Map<String, Object> am = createAtomicMap(cache1, key); am.put("k3", "v3"); removeAtomicMap(cache1, key); //! am = createAtomicMap(cache1, key); am.put("k2", "v2"); TestingUtil.getTransactionManager(cache1).commit(); } final boolean fails; { TestingUtil.getTransactionManager(cache1).begin(); Map<String, Object> am = createAtomicMap(cache1, key); //'k1' should no longer exist as we had killed the whole am instance in previous transaction fails = am.containsKey("k1"); TestingUtil.getTransactionManager(cache1).commit(); } assertFalse(fails); }
protected void assertMap(Map<?, ?> expected, Map<?, ?> atomicMap) { // do a simple equals() check first assertEquals(expected, new HashMap<Object, Object>(atomicMap)); //check size() and isEmpty() assertEquals("Wrong map '" + atomicMap + "'.size()", expected.size(), atomicMap.size()); assertEquals("Wrong map '" + atomicMap + "'.isEmpty()", expected.isEmpty(), atomicMap.isEmpty()); assertEquals("Wrong map '" + atomicMap + "'.keySet().size()", expected.keySet().size(), atomicMap.keySet().size()); assertEquals("Wrong map '" + atomicMap + "'.keySet().isEmpty()", expected.keySet().isEmpty(), atomicMap.keySet().isEmpty()); assertEquals("Wrong map '" + atomicMap + "'.values().size()", expected.values().size(), atomicMap.values().size()); assertEquals("Wrong map '" + atomicMap + "'.values().isEmpty()", expected.values().isEmpty(), atomicMap.values().isEmpty()); assertEquals("Wrong map '" + atomicMap + "'.entrySet().size()", expected.entrySet().size(), atomicMap.entrySet().size()); assertEquals("Wrong map '" + atomicMap + "'.entrySet().isEmpty()", expected.entrySet().isEmpty(), atomicMap.entrySet().isEmpty()); //check the collections content assertEquals("Wrong map '" + atomicMap + "'.keySet() content.", expected.keySet(), atomicMap.keySet()); assertEquals("Wrong map '" + atomicMap + "'.values() content.", new HashSet<Object>(expected.values()), new HashSet<Object>(atomicMap.values())); for (Entry<?, ?> entry : atomicMap.entrySet()) { assertEquals("Wrong value for key " + entry.getKey(), expected.get(entry.getKey()), entry.getValue()); } //check the containsKey() for (Object key : expected.keySet()) { assertContainsKey(atomicMap, key); } //check the containsValue() for (Object value : expected.values()) { assertContainsValue(atomicMap, value); } //check the get() assertKeysValues(expected, atomicMap); }
protected void assertSize(Map<?, ?> map, int expectedSize) { final int size; if (map instanceof Cache) { size = ((Cache)map).getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL).size(); } else { size = map.size(); } assertEquals("Wrong size in map '" + map + "'.", expectedSize, size); if (size == 0) { assertEmpty(map); } else { assertNotEmpty(map); } }
public void testCreateMapInTx() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); final Cache<String, Object> cache2 = cache(1, "atomic"); assertSize(cache1, 0); assertSize(cache2, 0); tm(0, "atomic").begin(); Map<String, String> map1 = createAtomicMap(cache1, "testCreateMapInTx", true); map1.put("k1", "v1"); tm(0, "atomic").commit(); Map<Object, Object> expectedMap = createMap("k1", "v1"); assertMap(expectedMap, map1); final Map<String, String> map2 = createAtomicMap(cache2, "testCreateMapInTx", true); assertMap(expectedMap, map2); }
public void testRollback() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); final Cache<String, Object> cache2 = cache(1, "atomic"); assertSize(cache1, 0); assertSize(cache2, 0); final Map<String, String> map1 = createAtomicMap(cache1, "testRollback", true); tm(0, "atomic").begin(); map1.put("k1", "v"); map1.put("k2", "v2"); tm(0, "atomic").rollback(); Map<Object, Object> instance = createAtomicMap(cache2, "testRollback", true); assertMap(Collections.emptyMap(), instance); assertMap(Collections.emptyMap(), map1); }
public void testDuplicateValue() { final Cache<String, Object> cache = cache(0, "atomic"); assertSize(cache, 0); final Map<String, String> map = createAtomicMap(cache, "duplicateValues", true); map.put("k1", "value"); map.put("k2", "value"); map.put("k3", "value"); map.put("k4", "value"); final Map<Object, Object> expectedMap = createMap("k1", "value", "k2", "value", "k3", "value", "k4", "value"); assertMap(expectedMap, map); }
public void testConcurrentReadsOnExistingMap() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); assertSize(cache1, 0); final Map<String, String> map = createAtomicMap(cache1, "testConcurrentReadsOnExistingMap", true); map.put("the-1", "my preciousss"); final Map<Object, Object> expectedMap = createMap("the-1", "my preciousss"); tm(0, "atomic").begin(); assertMap(expectedMap, map); map.put("the-2", "a minor"); Future<Void> f = fork(TestingUtil.withTxCallable(tm(0, "atomic"), (Callable<Void>) () -> { Map<String, String> map1 = createAtomicMap(cache1, "testConcurrentReadsOnExistingMap", true); assertMap(expectedMap, map1); assertNotContainsKey(map1, "the-2"); return null; })); f.get(10, TimeUnit.SECONDS); tm(0, "atomic").commit(); }
public void testReplicationPutCommit() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); final Cache<String, Object> cache2 = cache(1, "atomic"); assertSize(cache1, 0); assertSize(cache2, 0); final Map<String, String> map = createAtomicMap(cache1, "testReplicationPutCommit"); Map<Object, Object> expectedMap = createMap("existing", "existing", "blah", "blah"); tm(cache1).begin(); map.put("existing", "existing"); map.put("blah", "blah"); tm(cache1).commit(); assertMap(expectedMap, map); final Map<Object, Object> other = createAtomicMap(cache2, "testReplicationPutCommit", false); assertMap(expectedMap, other); //ok, do another tx with delta changes tm(cache2).begin(); other.put("existing", "not existing"); other.put("not existing", "peace on Earth"); tm(cache2).commit(); expectedMap = createMap("blah", "blah", "existing", "not existing", "not existing", "peace on Earth"); assertMap(expectedMap, map); assertMap(expectedMap, other); }
public void testRollbackAndThenCommit() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); final Cache<String, Object> cache2 = cache(1, "atomic"); assertSize(cache1, 0); assertSize(cache2, 0); final Map<String, String> map1 = createAtomicMap(cache1, "testRollbackAndThenCommit", true); tm(0, "atomic").begin(); map1.put("k1", "v"); map1.put("k2", "v2"); tm(0, "atomic").rollback(); Map<Object, Object> expectedMap = Collections.emptyMap(); Map<Object, Object> map2 = createAtomicMap(cache2, "testRollbackAndThenCommit", true); assertMap(expectedMap, map2); assertMap(expectedMap, map1); tm(0, "atomic").begin(); map1.put("k3", "v3"); map1.put("k4", "v4"); tm(0, "atomic").commit(); expectedMap = createMap("k3", "v3", "k4", "v4"); assertMap(expectedMap, map2); assertMap(expectedMap, map1); }
public void testReplicationRemoveCommit() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); final Cache<String, Object> cache2 = cache(1, "atomic"); assertSize(cache1, 0); assertSize(cache2, 0); final Map<String, String> map = createAtomicMap(cache1, "testReplicationRemoveCommit"); Map<Object, Object> expectedMap = createMap("existing", "existing", "blah", "blah"); tm(cache1).begin(); map.put("existing", "existing"); map.put("blah", "blah"); tm(cache1).commit(); assertMap(expectedMap, map); final Map<Object, Object> other = createAtomicMap(cache2, "testReplicationRemoveCommit", false); assertMap(expectedMap, other); //ok, do another tx with delta changes tm(cache2).begin(); String removed = map.remove("existing"); assertEquals("Wrong value removed from map '" + map + "'.", "existing", removed); tm(cache2).commit(); expectedMap = createMap("blah", "blah"); assertMap(expectedMap, map); assertMap(expectedMap, other); }
@SuppressWarnings("UnusedDeclaration") public void testCommitReadUncommittedValues() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); final Cache<String, Object> cache2 = cache(1, "atomic"); assertSize(cache1, 0); assertSize(cache2, 0); final Map<String, String> map = createAtomicMap(cache1, "testCommitReadUncommittedValues"); tm(cache1).begin(); map.put("existing", "existing"); map.put("hey", "blah"); tm(cache1).commit(); tm(cache1).begin(); map.put("key one", "fake one"); map.put("key one", "value one"); map.put("blah", "montevideo"); map.put("blah", "buenos aires"); map.remove("blah"); map.put("blah", "toronto"); Map<Object, Object> expectedMap = createMap("key one", "value one", "blah", "toronto", "existing", "existing", "hey", "blah"); assertMap(expectedMap, map); Map<String, String> sameAsMap = createAtomicMap(cache1, "testCommitReadUncommittedValues"); assertMap(expectedMap, sameAsMap); tm(cache1).commit(); assertMap(expectedMap, map); assertMap(expectedMap, sameAsMap); }
public void testConcurrentTx() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); final Cache<String, Object> cache2 = cache(1, "atomic"); assertSize(cache1, 0); assertSize(cache2, 0); final TransactionManager tm1 = tm(cache1); final TransactionManager tm2 = tm(cache2); final Map<String, String> map1 = createAtomicMap(cache1, "testConcurrentTx", true); Map<Object, Object> expectedMap = createMap("k1", "initial"); tm1.begin(); map1.put("k1", "initial"); tm1.commit(); final Map<String, String> map2 = createAtomicMap(cache2, "testConcurrentTx", false); assertMap(expectedMap, map1); assertMap(expectedMap, map2); Future<Void> t1 = fork(TestingUtil.withTxCallable(tm1, (Callable<Void>) () -> { map1.put("k1", "tx1Value"); return null; })); Future<Void> t2 = fork(TestingUtil.withTxCallable(tm2, (Callable<Void>) () -> { map2.put("k2", "tx2Value"); return null; })); t2.get(); t1.get(); expectedMap = createMap("k1", "tx1Value", "k2", "tx2Value"); assertMap(expectedMap, map1); assertMap(expectedMap, map2); }
public void testReplicationPutAndClearCommit() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); final Cache<String, Object> cache2 = cache(1, "atomic"); assertSize(cache1, 0); assertSize(cache2, 0); final Map<String, String> map = createAtomicMap(cache1, "map"); final Map<String, String> map2 = createAtomicMap(cache2, "map", false); Map<Object, Object> expectedMap = createMap("existing", "existing", "blah", "blah"); TestingUtil.getTransactionManager(cache1).begin(); map.put("existing", "existing"); map.put("blah", "blah"); map.size(); TestingUtil.getTransactionManager(cache1).commit(); assertMap(expectedMap, map); assertMap(expectedMap, map2); //ok, do another tx with clear delta changes tm(cache2).begin(); map2.clear(); tm(cache2).commit(); expectedMap = Collections.emptyMap(); assertMap(expectedMap, map); assertMap(expectedMap, map2); }
public void testMultipleTx() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); final Cache<String, Object> cache2 = cache(1, "atomic"); assertSize(cache1, 0); assertSize(cache2, 0); final TransactionManager tm1 = tm(cache1); final Map<String, String> map1 = createAtomicMap(cache1, "testMultipleTx", true); final Map<String, String> map2 = createAtomicMap(cache2, "testMultipleTx", false); assertMap(expectedMap, map1); assertMap(expectedMap, map2); tm1.commit(); expectedMap = createMap("k1", "initial"); assertMap(expectedMap, map1); assertMap(expectedMap, map2); tm1.commit(); expectedMap = createMap("k1", "v1", "k2", "v2", "k3", "v3"); assertMap(expectedMap, map1); assertMap(expectedMap, map2); expectedMap = createMap("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5", "k6", "v6"); assertMap(expectedMap, map1); assertMap(expectedMap, map2);
public void testConcurrentWritesOnExistingMap() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); assertSize(cache1, 0); final Map<String, String> map = createAtomicMap(cache1, "testConcurrentReadsOnExistingMap", true); map.put("the-1", "my preciousss"); final Map<Object, Object> expectedMap = createMap("the-1", "my preciousss"); tm(0, "atomic").begin(); assertMap(expectedMap, map); map.put("the-2", "a minor"); Future<Void> f = fork(TestingUtil.withTxCallable(tm(0, "atomic"), (Callable<Void>) () -> { Map<String, String> map1 = createAtomicMap(cache1, "testConcurrentReadsOnExistingMap", true); assertMap(expectedMap, map1); assertNotContainsKey(map1, "the-2"); map1.put("the-2", "a minor-different"); // We're in pessimistic locking, so this put is going to block return null; tm(0, "atomic").commit();
public void testReadEntriesCommittedInConcurrentTx() throws Exception { final Cache<String, Object> cache1 = cache(0, "atomic"); final Cache<String, Object> cache2 = cache(1, "atomic"); assertSize(cache1, 0); assertSize(cache2, 0); final TransactionManager tm1 = tm(cache1); final TransactionManager tm2 = tm(cache2); final Map<String, String> writeMap = createAtomicMap(cache1, "repeatableReadMap", true); assertMap(emptyMap, writeMap); Map<Object, Object> initialMap = createMap("k1", "initial"); assertMap(initialMap, writeMap); Map<String, String> readMap = createAtomicMap(cache2, "repeatableReadMap", false); assertMap(initialMap, readMap); Transaction tx2 = tm2.suspend(); Map<Object, Object> modifiedMap = createMap("k1", "v1", "k2", "v2", "k3", "v3"); assertMap(modifiedMap, writeMap); tm1.commit(); assertMap(initialMap, readMap); tm2.commit(); assertMap(modifiedMap, writeMap); assertMap(modifiedMap, readMap);