@Override public boolean test() throws Exception { return os.allKeys().isEmpty(); }
@Override public boolean test() throws Exception { assertThat(os.allKeys().size(), is(maxEntries)); for (int i = 1; i < maxEntries + 1; i++) { assertThat(os.contains(valueOf(i)), is(true)); } return true; }
@Before public void setUp() throws Exception { this.values = new HashMap<>(); this.values.put("fruit", "banana"); this.values.put("icecream", "chocolate"); this.values.put("drink", "coke"); when(this.objectStore.retrieve(Mockito.anyString())).thenAnswer(invocation -> { Serializable value = values.get(invocation.getArguments()[0]); if (value == null) { throw new ObjectDoesNotExistException(); } return value; }); when(this.objectStore.allKeys()).thenReturn(new ArrayList<>(this.values.keySet())); this.producer = new ObjectStoreProducer<>(this.objectStore); }
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 disposePartialId() throws ObjectStoreException { final String partialId = "1874947571-1840879217"; when(objectStore.allKeys()).thenReturn(Arrays.asList(SOME_KEY, OTHER_KEY)); when(objectStore.remove(partialId)).thenThrow(new ObjectDoesNotExistException()); cacheManager.dispose(partialId); verify(objectStore).remove(SOME_KEY); verify(objectStore).remove(OTHER_KEY); }
@Test public void expireWithoutMaxEntries() throws Exception { when(settings.getMaxEntries()).thenReturn(empty()); when(settings.getEntryTTL()).thenReturn(of(1L)); StoredObject value = new StoredObject("", 0L, KEY); when(objectStore.retrieve(KEY)).thenReturn(value); when(objectStore.allKeys()).thenReturn(asList(KEY)); when(objectStore.contains(KEY)).thenReturn(true); when(objectStore.remove(KEY)).thenReturn(value); wrapper = new MonitoredObjectStoreWrapper(objectStore, settings); wrapper.expire(); check(5000, 100, () -> { verify(objectStore).remove(KEY); return true; }); } }
@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)); }
@Test public void disposeCacheForConfig() throws Exception { // using config setLocation(OUTPUT_AND_METADATA_KEY_CACHE_RESOLVER); getSuccessComponentDynamicMetadata(PERSON_METADATA_KEY); // using alternative-config setLocation(CONTENT_AND_OUTPUT_CACHE_RESOLVER_WITH_ALTERNATIVE_CONFIG); getSuccessComponentDynamicMetadata(); assertThat(getMetadataObjectStore().allKeys(), hasSize(2)); getMetadataObjectStore().retrieve(OUTPUT_AND_METADATA_KEY_CACHE_ID); getMetadataObjectStore().retrieve(CONTENT_AND_OUTPUT_CACHE_ID); metadataService.disposeCache(CONTENT_AND_OUTPUT_CACHE_ID); assertThat(getMetadataObjectStore().allKeys(), hasSize(1)); getMetadataObjectStore().retrieve(OUTPUT_AND_METADATA_KEY_CACHE_ID); try { getMetadataObjectStore().retrieve(CONTENT_AND_OUTPUT_CACHE_ID); fail(); } catch (ObjectDoesNotExistException success) { } }
@Test public void multipleCaches() throws Exception { // using config setLocation(OUTPUT_AND_METADATA_KEY_CACHE_RESOLVER); metadataService.getMetadataKeys(location); getSuccessComponentDynamicMetadata(PERSON_METADATA_KEY); setLocation(OUTPUT_METADATA_WITHOUT_KEY_PARAM); getSuccessComponentDynamicMetadata(NULL_METADATA_KEY); // using alternative-config setLocation(CONTENT_AND_OUTPUT_CACHE_RESOLVER_WITH_ALTERNATIVE_CONFIG); getSuccessComponentDynamicMetadata(PERSON_METADATA_KEY); // re-use same key setLocation(OUTPUT_AND_METADATA_KEY_CACHE_RESOLVER); getSuccessComponentDynamicMetadata(PERSON_METADATA_KEY); List<String> actualKeys = getMetadataObjectStore().allKeys(); assertThat(actualKeys, hasSize(3)); assertThat(actualKeys, hasItems(OUTPUT_AND_METADATA_KEY_CACHE_ID, OUTPUT_METADATA_WITHOUT_KEY_CACHE_ID, CONTENT_AND_OUTPUT_CACHE_ID)); }
public ObjectStoreProducer(ObjectStore<T> objectStore) { if (objectStore == null) { throw new IllegalArgumentException("Cannot construct a producer with a null object store"); } this.objectStore = objectStore; try { List<String> allKeys = new ArrayList<>(objectStore.allKeys()); this.keys = allKeys.iterator(); this.size = allKeys.size(); } catch (ObjectStoreException e) { throw new RuntimeException("Could not construct producer because exception was found retrieving keys", e); } }
private void testObjectStoreMaxEntries(ObjectStoreManager manager, String storeName, ObjectStoreSettings settings) throws ObjectStoreException, InterruptedException { ObjectStore<String> objectStore = manager.createObjectStore(storeName, settings); try { storeObjects(objectStore, 0, 90); ensureMillisecondChanged(); storeObjects(objectStore, 90, 100); new PollingProber(2000, 50).check(new JUnitLambdaProbe(() -> { try { assertEquals(10, objectStore.allKeys().size()); for (int i = 90; i < 100; i++) { assertTrue("Checking that key" + i + " exists", objectStore.contains("key" + i)); } } catch (Exception e) { fail(e.getMessage()); } return true; })); } finally { manager.disposeStore(storeName); } }
@Test public void disposeCacheForPartialId() throws Exception { // using config setLocation(OUTPUT_AND_METADATA_KEY_CACHE_RESOLVER); getSuccessComponentDynamicMetadata(PERSON_METADATA_KEY); // using alternative-config setLocation(CONTENT_AND_OUTPUT_CACHE_RESOLVER_WITH_ALTERNATIVE_CONFIG); getSuccessComponentDynamicMetadata(); assertThat(getMetadataObjectStore().allKeys(), hasSize(2)); getMetadataObjectStore().retrieve(OUTPUT_AND_METADATA_KEY_CACHE_ID); getMetadataObjectStore().retrieve(CONTENT_AND_OUTPUT_CACHE_ID); metadataService.disposeCache("1874947571-1840879217"); assertThat(getMetadataObjectStore().allKeys(), hasSize(0)); try { getMetadataObjectStore().retrieve(OUTPUT_AND_METADATA_KEY_CACHE_ID); fail(); } catch (ObjectDoesNotExistException success) { } try { getMetadataObjectStore().retrieve(CONTENT_AND_OUTPUT_CACHE_ID); fail(); } catch (ObjectDoesNotExistException success) { } }
@Override public List<String> allKeys() throws ObjectStoreException { return getWrapped().allKeys(); }
@Override public List<String> allKeys() throws ObjectStoreException { return getStore().allKeys(); }
@Override public List<String> allKeys() throws ObjectStoreException { return getStore().allKeys().stream() .map(this::splitKey) .filter(key -> key.getFirst().equals(partitionName)) .map(Pair::getSecond) .collect(toList()); }
@Override public boolean test() throws Exception { return os.allKeys().isEmpty(); }
@Before public void setUp() throws Exception { this.values = new HashMap<>(); this.values.put("fruit", "banana"); this.values.put("icecream", "chocolate"); this.values.put("drink", "coke"); when(this.objectStore.retrieve(Mockito.anyString())).thenAnswer(invocation -> { Serializable value = values.get(invocation.getArguments()[0]); if (value == null) { throw new ObjectDoesNotExistException(); } return value; }); when(this.objectStore.allKeys()).thenReturn(new ArrayList<>(this.values.keySet())); this.producer = new ObjectStoreProducer<>(this.objectStore); }
@Override public boolean test() throws Exception { assertThat(os.allKeys().size(), is(maxEntries)); for (int i = 1; i < maxEntries + 1; i++) { assertThat(os.contains(valueOf(i)), is(true)); } return true; }
@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)); }