public MergePolicyCustomTest(CacheMode cacheMode, int owners, String description, int[] partition1, int[] partition2) { super(cacheMode, owners, description, AvailabilityMode.AVAILABLE, partition1, partition2); this.mergePolicy = ((preferredEntry, otherEntries) -> TestInternalCacheEntryFactory.create(conflictKey, "Custom")); this.valueAfterMerge = "Custom"; } }
public void testInternalCacheValueMarshalling() throws Exception { ImmortalCacheValue value1 = (ImmortalCacheValue) TestInternalCacheEntryFactory.createValue("value", System.currentTimeMillis() - 1000, -1, System.currentTimeMillis(), -1); byte[] bytes = marshaller.objectToByteBuffer(value1); ImmortalCacheValue rvalue1 = (ImmortalCacheValue) marshaller.objectFromByteBuffer(bytes); assert rvalue1.getValue().equals(value1.getValue()) : "Writen[" + rvalue1.getValue() + "] and read[" + value1.getValue() + "] objects should be the same"; MortalCacheValue value2 = (MortalCacheValue) TestInternalCacheEntryFactory.createValue("value", System.currentTimeMillis() - 1000, 200000, System.currentTimeMillis(), -1); bytes = marshaller.objectToByteBuffer(value2); MortalCacheValue rvalue2 = (MortalCacheValue) marshaller.objectFromByteBuffer(bytes); assert rvalue2.getValue().equals(value2.getValue()) : "Writen[" + rvalue2.getValue() + "] and read[" + value2.getValue() + "] objects should be the same"; TransientCacheValue value3 = (TransientCacheValue) TestInternalCacheEntryFactory.createValue("value", System.currentTimeMillis() - 1000, -1, System.currentTimeMillis(), 4000000); bytes = marshaller.objectToByteBuffer(value3); TransientCacheValue rvalue3 = (TransientCacheValue) marshaller.objectFromByteBuffer(bytes); assert rvalue3.getValue().equals(value3.getValue()) : "Writen[" + rvalue3.getValue() + "] and read[" + value3.getValue() + "] objects should be the same"; TransientMortalCacheValue value4 = (TransientMortalCacheValue) TestInternalCacheEntryFactory.createValue("value", System.currentTimeMillis() - 1000, 200000, System.currentTimeMillis(), 4000000); bytes = marshaller.objectToByteBuffer(value4); TransientMortalCacheValue rvalue4 = (TransientMortalCacheValue) marshaller.objectFromByteBuffer(bytes); assert rvalue4.getValue().equals(value4.getValue()) : "Writen[" + rvalue4.getValue() + "] and read[" + value4.getValue() + "] objects should be the same"; }
public static InternalCacheEntry create(Object key, Object value, long lifespan) { return create(FACTORY, key, value, lifespan); }
public static InternalCacheEntry create(Object key, Object value, long lifespan, long maxIdle) { return create(FACTORY, key, value, lifespan, maxIdle); }
@Override public void run() { try { int randomInt = r.nextInt(10); store(TestInternalCacheEntryFactory.create(keys[randomInt], values[randomInt])); } catch (Throwable e) { throwables.add(e); } } };
public void testInternalCacheEntryMarshalling() throws Exception { ImmortalCacheEntry entry1 = (ImmortalCacheEntry) TestInternalCacheEntryFactory.create("key", "value", System.currentTimeMillis() - 1000, -1, System.currentTimeMillis(), -1); marshallAndAssertEquality(entry1); MortalCacheEntry entry2 = (MortalCacheEntry) TestInternalCacheEntryFactory.create("key", "value", System.currentTimeMillis() - 1000, 200000, System.currentTimeMillis(), -1); marshallAndAssertEquality(entry2); TransientCacheEntry entry3 = (TransientCacheEntry) TestInternalCacheEntryFactory.create("key", "value", System.currentTimeMillis() - 1000, -1, System.currentTimeMillis(), 4000000); marshallAndAssertEquality(entry3); TransientMortalCacheEntry entry4 = (TransientMortalCacheEntry) TestInternalCacheEntryFactory.create("key", "value", System.currentTimeMillis() - 1000, 200000, System.currentTimeMillis(), 4000000); marshallAndAssertEquality(entry4); }
public PojoWithAttributes(int age, String key) { this.age = age; this.entry = TestInternalCacheEntryFactory.create( "internalkey-" + key, "internalvalue-" + age, (age * 17)); this.key = new Key(key, false); this.uuid = UUID.randomUUID(); }
private InternalCacheEntry<Object, Object> internalCacheEntry(String key, String value, long lifespan, long idle) { return TestInternalCacheEntryFactory.<Object, Object>create(factory, key, wrap(key, value), lifespan, idle); }
@Override public CacheEntry merge(CacheEntry preferredEntry, List otherEntries) { return TestInternalCacheEntryFactory.create(preferredEntry.getKey(), "customValue"); }
public OperationsDuringMergeConflictTest(MergeAction mergeAction) { super(DIST_SYNC, null, new int[]{0,1}, new int[]{2,3}); this.mergePolicy = ((preferredEntry, otherEntries) -> TestInternalCacheEntryFactory.create(conflictKey, MERGE_RESULT)); this.description = mergeAction.toString(); this.mergeAction = mergeAction; this.valueAfterMerge = mergeAction.value; }
protected final InternalCacheEntry<Object, Object> internalCacheEntry(String key, String value, long lifespan) { return TestInternalCacheEntryFactory.<Object, Object>create(factory, key, wrap(key, value), lifespan); }
public void testPreload() throws CacheLoaderException { store(TestInternalCacheEntryFactory.create("k1", "v1")); store(TestInternalCacheEntryFactory.create("k2", "v2")); store(TestInternalCacheEntryFactory.create("k3", "v3")); Set<InternalCacheEntry> set = loadAll(); assert set.size() == 3; Set expected = new HashSet(); expected.add("k1"); expected.add("k2"); expected.add("k3"); for (InternalCacheEntry se : set) assert expected.remove(se.getKey()); assert expected.isEmpty(); }
@Test public void testDoWorkOnStore() throws Exception { CacheStore cs = mock(CacheStore.class); Store store = mock(Store.class); InternalCacheEntry entry = TestInternalCacheEntryFactory.create("1", "2"); when(store.getType()).thenReturn(Modification.Type.STORE); when(store.getStoredEntry()).thenReturn(entry); cs.store(entry); ModificationsTransactionWorker worker = new ModificationsTransactionWorker(cs, Collections.singletonList(store)); worker.doWork(); }
public void testStreamingAPI() throws IOException, ClassNotFoundException, CacheLoaderException { store(TestInternalCacheEntryFactory.create("k1", "v1")); store(TestInternalCacheEntryFactory.create("k2", "v2")); store(TestInternalCacheEntryFactory.create("k3", "v3")); ByteArrayOutputStream out = new ByteArrayOutputStream(); toStream(out); out.close(); cacheMap.clear(); fromStream(new ByteArrayInputStream(out.toByteArray())); Set<InternalCacheEntry> set = loadAll(); assert set.size() == 3; Set expected = new HashSet(); expected.add("k1"); expected.add("k2"); expected.add("k3"); for (InternalCacheEntry se : set) assert expected.remove(se.getKey()); assert expected.isEmpty(); }
public void testCommitAndRollbackWithoutPrepare() throws CacheLoaderException { store(TestInternalCacheEntryFactory.create("old", "old")); Transaction tx = mock(Transaction.class); commit(tx); store(TestInternalCacheEntryFactory.create("old", "old")); rollback(tx); assert cacheMap.containsKey("old"); }
public void testClearOnAbortFromStream() throws Exception { start(); InternalCacheEntry entry = TestInternalCacheEntryFactory.create("key", "value"); when(cacheMap.put(entry.getKey(), entry)).thenReturn(null); ObjectInput ois = mock(ObjectInput.class); when(ois.readLong()).thenReturn((long) 1); com.sleepycat.je.Transaction txn = mock(com.sleepycat.je.Transaction.class); when(currentTransaction.beginTransaction(null)).thenReturn(txn); Cursor cursor = mock(Cursor.class); when(cacheDb.openCursor(txn, null)).thenReturn(cursor); IOException ex = new IOException(); when(ois.readObject()).thenThrow(ex); cacheMap.clear(); expiryMap.clear(); cs.start(); try { cs.store(entry); cs.fromStream(ois); assert false : "should have gotten an exception"; } catch (CacheLoaderException e) { assert ex.equals(e.getCause()); return; } assert false : "should have returned"; } }
public void testPurgeExpired() throws Exception { long now = System.currentTimeMillis(); long lifespan = 1000; store(TestInternalCacheEntryFactory.create("k1", "v1", lifespan)); store(TestInternalCacheEntryFactory.create("k2", "v2", lifespan)); store(TestInternalCacheEntryFactory.create("k3", "v3", lifespan)); assert cacheMap.containsKey("k1"); assert cacheMap.containsKey("k2"); assert cacheMap.containsKey("k3"); Thread.sleep(lifespan + 100); purgeExpired(); assert !cacheMap.containsKey("k1"); assert !cacheMap.containsKey("k2"); assert !cacheMap.containsKey("k3"); }
@Override public void run() { ThreadLocalRandom random = ThreadLocalRandom.current(); InternalCacheEntry ice; while (!terminate) { long lifespan; String key = key(random); switch (random.nextInt(3)) { case 0: lifespan = random.nextInt(3) == 0 ? random.nextInt(10) : -1; ice = TestInternalCacheEntryFactory.<Object, Object>create(factory, key(random), String.valueOf(random.nextInt()), lifespan); store.write(TestingUtil.marshalledEntry(ice, marshaller)); break; case 1: store.delete(key); break; case 2: store.load(key); } } } }
public void testOnePhaseCommit() throws CacheLoaderException { List<Modification> mods = new ArrayList<Modification>(); mods.add(new Store(TestInternalCacheEntryFactory.create("k1", "v1"))); mods.add(new Store(TestInternalCacheEntryFactory.create("k2", "v2"))); mods.add(new Remove("k1")); Transaction tx = mock(Transaction.class); prepare(mods, tx, true); Set s = loadAll(); assert load("k2").getValue().equals("v2"); assert !cacheMap.containsKey("k1"); cacheMap.clear(); mods = new ArrayList<Modification>(); mods.add(new Store(TestInternalCacheEntryFactory.create("k1", "v1"))); mods.add(new Store(TestInternalCacheEntryFactory.create("k2", "v2"))); mods.add(new Clear()); mods.add(new Store(TestInternalCacheEntryFactory.create("k3", "v3"))); prepare(mods, tx, true); assert !cacheMap.containsKey("k1"); assert !cacheMap.containsKey("k2"); assert cacheMap.containsKey("k3"); }