public void storeMoney(ObjectStore<Long> objectStore, Long money) throws Exception { objectStore.store("money", money); }
public static <T extends Exception> T testException(T exception, MuleContext muleContext) { ObjectStore<T> os = getObjectStore(muleContext); try { os.store(key, exception); return os.retrieve(key); } catch (Exception e) { throw new RuntimeException(e); } finally { try { os.clear(); } catch (ObjectStoreException e) { throw new RuntimeException(e); } } }
@Test public void testStoreWithExistingKey() throws ObjectStoreException { String key = createKey(); Serializable value = getStorableValue(); ObjectStore<Serializable> objectStore = getObjectStore(); // storing for the first time must work objectStore.store(key, value); // storing with the same key again must fail try { objectStore.store(key, value); fail("store() with an existing key must throw ObjectAlreadyExistsException"); } catch (ObjectAlreadyExistsException oaee) { // this one was expected } }
@Test public void clear() throws ObjectStoreException { String key = this.createKey(); Serializable value = this.getStorableValue(); ObjectStore<Serializable> objectStore = this.getObjectStore(); objectStore.store(key, value); Assert.assertTrue(objectStore.contains(key)); objectStore.clear(); Assert.assertFalse(objectStore.contains(key)); // check it's still usable objectStore.store(key, value); Assert.assertTrue(objectStore.contains(key)); }
@Test public void testStoreWithNullKey() { try { Serializable value = getStorableValue(); getObjectStore().store(null, value); fail("store() called with null key must throw ObjectStoreException"); } catch (ObjectStoreException ose) { // this one was expected } }
@Test public void retrieveCacheWhenExists() throws ObjectStoreException { when(objectStore.contains(SOME_KEY)).thenReturn(true); when(objectStore.retrieve(SOME_KEY)).thenReturn(mockCache); MetadataCache actual = cacheManager.getOrCreateCache(SOME_KEY); assertThat(actual, is(mockCache)); verify(objectStore).contains(SOME_KEY); verify(objectStore).retrieve(SOME_KEY); verify(objectStore, never()).store(anyString(), any(MetadataCache.class)); }
@Test public void createCacheWhenMissingKey() throws ObjectStoreException { when(objectStore.contains(SOME_KEY)).thenReturn(false); MetadataCache cache = cacheManager.getOrCreateCache(SOME_KEY); assertThat(cache, instanceOf(DefaultMetadataCache.class)); verify(objectStore).contains(SOME_KEY); verify(objectStore, never()).retrieve(SOME_KEY); verify(objectStore).store(SOME_KEY, cache); }
@Test public void storeUsingBigKey() throws Exception { ObjectStore os = objectStoreFactory.createObjectStore("myOs"); StringBuilder bigKey = new StringBuilder(); for (int i = 0; i < 50; i++) { bigKey.append("abcdefghijklmnopqrstuvwxyz"); } os.store(bigKey.toString(), 1); assertThat(os.retrieve(bigKey.toString()), Is.is(1)); }
private void storeObjects(ObjectStore<String> objectStore, int start, int stop) throws ObjectStoreException { for (int i = start; i < stop; i++) { objectStore.store("key" + i, "value" + i); assertEquals("value" + i, objectStore.retrieve("key" + i)); } }
private void updateRecentlyProcessedIds() throws ObjectStoreException { Lock osClearingLock = lockFactory.createLock(UPDATE_PROCESSED_LOCK); try { osClearingLock.lock(); List<String> strings = recentlyProcessedIds.allKeys(); idsOnUpdatedWatermark.clear(); strings.forEach(key -> { try { idsOnUpdatedWatermark.store(key, recentlyProcessedIds.retrieve(key)); } catch (ObjectStoreException e) { throw new MuleRuntimeException(createStaticMessage("An error occurred while updating the watermark Ids. Failed to update key '%s' in Watermark-IDs ObjectStore: %s", key, e.getMessage()), e); } }); recentlyProcessedIds.clear(); } finally { osClearingLock.unlock(); } }
@Test public void updateCache() throws ObjectStoreException { when(objectStore.contains(SOME_KEY)).thenReturn(true); when(objectStore.remove(SOME_KEY)).thenReturn(mockCache); DefaultMetadataCache cache = new DefaultMetadataCache(); cacheManager.updateCache(SOME_KEY, cache); verify(objectStore).contains(SOME_KEY); verify(objectStore).remove(SOME_KEY); verify(objectStore).store(SOME_KEY, cache); verify(objectStore, never()).retrieve(SOME_KEY); }
@Test public void expirationIntervalWithHighTTLPersistentObjectStore() throws Exception { int maxEntries = 5; int entryTTL = 10000; ObjectStore os = objectStoreFactory.createObjectStore("myOs", maxEntries, entryTTL, 100); os.store("0", 0); ensureMilisecondChanged(); for (int i = 1; i < maxEntries; i++) { os.store(valueOf(i), i); } os.store(OBJECT_KEY, OBJECT_KEY_VALUE_1); Thread.sleep(entryTTL / 5); assertThat(os.allKeys().size(), is(maxEntries)); for (int i = 1; i < maxEntries; i++) { assertThat(os.contains(valueOf(i)), is(true)); } assertThat(os.contains(OBJECT_KEY), is(true)); }
private void updateWatermark(Serializable value, Comparator comparator) { try { if (watermarkObjectStore.contains(WATERMARK_OS_KEY)) { Serializable currentValue = watermarkObjectStore.retrieve(WATERMARK_OS_KEY); if (compareWatermarks(currentValue, value, comparator) >= 0) { return; } watermarkObjectStore.remove(WATERMARK_OS_KEY); } updateRecentlyProcessedIds(); watermarkObjectStore.store(WATERMARK_OS_KEY, value); } catch (ObjectStoreException e) { throw new MuleRuntimeException( createStaticMessage(format("Failed to update watermark value for message source at location '%s'. %s", flowName, e.getMessage())), e); } }
@Test public void storesNullValue() throws Exception { try { getObjectStore().store("key", null); fail("store() called with null value must throw ObjectStoreException"); } catch (ObjectStoreException ose) { // this one was expected } } }
@Test public void partitionObjectStoreDoesNotCollide() throws Exception { ObjectStore os = objectStoreFactory.createObjectStore("myOs"); ObjectStore os2 = objectStoreFactory.createObjectStore("myOs2"); os.store(OBJECT_KEY, OBJECT_KEY_VALUE_1); os2.store(OBJECT_KEY, OBJECT_KEY_VALUE_2); assertThat(os.contains(OBJECT_KEY), is(true)); assertThat(os.retrieve(OBJECT_KEY), is(OBJECT_KEY_VALUE_1)); assertThat(os2.contains(OBJECT_KEY), is(true)); assertThat(os2.retrieve(OBJECT_KEY), is(OBJECT_KEY_VALUE_2)); assertThat(os.remove(OBJECT_KEY), is(OBJECT_KEY_VALUE_1)); assertThat(os2.remove(OBJECT_KEY), is(OBJECT_KEY_VALUE_2)); }
@Test public void expirationIntervalWithLowTTL() throws Exception { int maxEntries = 5; int entryTTL = 10; int expirationInterval = 100; final ObjectStore os = objectStoreFactory.createObjectStore("myOs", maxEntries, entryTTL, expirationInterval); for (int i = 0; i < maxEntries; i++) { os.store(valueOf(i), i); } PollingProber prober = new PollingProber(1000, expirationInterval); prober.check(new JUnitProbe() { @Override public boolean test() throws Exception { return os.allKeys().isEmpty(); } @Override public String describeFailure() { return "not all entries were evicted"; } }); }
@Override public CoreEvent process(CoreEvent event) throws MuleException { ObjectStore<String> myPartition = objectStoreManager.getOrCreateObjectStore("myPartition", ObjectStoreSettings.builder().persistent(true).build()); String key = "value"; if (myPartition.contains(key)) { myPartition.remove(key); } myPartition.store(key, key); return event; } }
@Test public void serialization() throws Exception { final String key = "key"; ObjectStore<DateTime> os = muleContext.getObjectStoreManager().createObjectStore("DateTimeTestCase", unmanagedPersistent()); try { os.store(key, now); DateTime recovered = os.retrieve(key); assertEquals(now, recovered); } finally { os.clear(); } }
private void testObjectStoreExpiry(ObjectStoreManager manager, String storeName, ObjectStoreSettings settings) throws ObjectStoreException, InterruptedException { ObjectStore<String> objectStore = manager.createObjectStore(storeName, settings); try { objectStore.store("key1", "value1"); assertEquals("value1", objectStore.retrieve("key1")); new PollingProber(2000, 50).check(new JUnitLambdaProbe(() -> { try { assertFalse("Object with key1 still exists.", objectStore.contains("key1")); } catch (Exception e) { fail(e.getMessage()); } return true; })); } finally { manager.disposeStore(storeName); } }
@Test public void testClearPartition() throws ObjectStoreException, InterruptedException, RegistrationException { getRegistry().registerObject(BASE_IN_MEMORY_OBJECT_STORE_KEY, new SimpleMemoryObjectStore<String>()); ObjectStoreManager manager = getRegistry().lookupObject(OBJECT_STORE_MANAGER); ObjectStore<String> partition1 = manager.createObjectStore("inMemoryPart1", unmanagedTransient()); ObjectStore<String> partition2 = manager.createObjectStore("inMemoryPart2", unmanagedTransient()); partition1.store("key1", "value1"); partition2.store("key2", "value2"); assertEquals("value1", partition1.retrieve("key1")); assertEquals("value2", partition2.retrieve("key2")); partition1.clear(); assertEquals("value2", partition2.retrieve("key2")); }