private SoftExpiringCache<String, String> makeSoftExpiringCache() { SoftExpiringCache<String, String> expiringCache = new SoftExpiringCache<String, String>(cache); expiringCache.setTimeSource(timeSource); return expiringCache; }
Object obj = null; if (!query.ignoreCache) { SoftExpiringCache.CachedObject<Object> cached = cache.getElement(query.specUri); if (cached != null) { obj = cached.obj; cache.addElement(query.specUri, obj, refresh); executor.execute(new SpecUpdater(query, obj)); cache.addElement(query.specUri, obj, refresh);
@Test(expected = SpecParserException.class) public void negativeCachingEnforced() throws Exception { specFactory.cache.addElement(SPEC_URL, new SpecParserException("broken"), 1000); specFactory.getGadgetSpec(createContext(SPEC_URL, false)); }
@Test public void testMissingValue() { SoftExpiringCache<String, String> expiringCache = makeSoftExpiringCache(); assertNull(expiringCache.getElement("not set")); } }
/** * @param clazz the class for spec objects. * @param executor for asynchronously updating specs * @param pipeline the request pipeline for fetching new specs * @param cache a cache for parsed spec objects * @param refresh the frequency at which to update specs, independent of cache expiration policy */ public AbstractSpecFactory(Class<T> clazz, ExecutorService executor, RequestPipeline pipeline, Cache<String, Object> cache, long refresh) { this.clazz = clazz; this.executor = executor; this.pipeline = pipeline; this.cache = new SoftExpiringCache<String, Object>(cache); this.refresh = refresh; }
@Test public void badResponseServedFromCache() throws Exception { HttpResponse expiredResponse = new HttpResponseBuilder() .setResponse(BASIC_BUNDLE.getBytes("UTF-8")) .addHeader("Pragma", "no-cache") .create(); HttpResponse badResponse = HttpResponse.error(); expect(pipeline.execute(isA(HttpRequest.class))) .andReturn(expiredResponse).once(); expect(pipeline.execute(isA(HttpRequest.class))) .andReturn(badResponse).once(); replay(pipeline); final AtomicLong time = new AtomicLong(); bundleFactory.cache.setTimeSource(new TimeSource() { @Override public long currentTimeMillis() { return time.get(); } }); time.set(System.currentTimeMillis()); MessageBundle bundle0 = bundleFactory.getBundle(gadgetSpec, LOCALE, false, ContainerConfig.DEFAULT_CONTAINER, null); time.set(time.get() + MAX_AGE + 1); MessageBundle bundle1 = bundleFactory.getBundle(gadgetSpec, LOCALE, false, ContainerConfig.DEFAULT_CONTAINER, null); verify(pipeline); assertEquals(bundle0.getMessages().get(MSG_0_NAME), bundle1.getMessages().get(MSG_0_NAME)); }
@Test(expected = SpecParserException.class) public void negativeCachingEnforced() throws Exception { specFactory.cache.addElement(SPEC_URL.toString(), new SpecParserException("broken"), 1000); specFactory.getGadgetSpec(createContext(SPEC_URL, false)); }
@Test public void testMissingValue() { SoftExpiringCache<String, String> expiringCache = makeSoftExpiringCache(); assertNull(expiringCache.getElement("not set")); } }
/** * @param clazz the class for spec objects. * @param executor for asynchronously updating specs * @param pipeline the request pipeline for fetching new specs * @param cache a cache for parsed spec objects * @param refresh the frequency at which to update specs, independent of cache expiration policy */ public AbstractSpecFactory(Class<T> clazz, ExecutorService executor, RequestPipeline pipeline, Cache<Uri, Object> cache, long refresh) { this.clazz = clazz; this.executor = executor; this.pipeline = pipeline; this.cache = new SoftExpiringCache<Uri, Object>(cache); this.refresh = refresh; }
@Test public void badResponseServedFromCache() throws Exception { HttpResponse expiredResponse = new HttpResponseBuilder() .setResponse(BASIC_BUNDLE.getBytes("UTF-8")) .addHeader("Pragma", "no-cache") .create(); HttpResponse badResponse = HttpResponse.error(); expect(pipeline.execute(isA(HttpRequest.class))) .andReturn(expiredResponse).once(); expect(pipeline.execute(isA(HttpRequest.class))) .andReturn(badResponse).once(); replay(pipeline); final AtomicLong time = new AtomicLong(); bundleFactory.cache.setTimeSource(new TimeSource() { @Override public long currentTimeMillis() { return time.get(); } }); time.set(System.currentTimeMillis()); MessageBundle bundle0 = bundleFactory.getBundle(gadgetSpec, LOCALE, false, ContainerConfig.DEFAULT_CONTAINER, null); time.set(time.get() + MAX_AGE + 1); MessageBundle bundle1 = bundleFactory.getBundle(gadgetSpec, LOCALE, false, ContainerConfig.DEFAULT_CONTAINER, null); verify(pipeline); assertEquals(bundle0.getMessages().get(MSG_0_NAME), bundle1.getMessages().get(MSG_0_NAME)); }
Object obj = null; if (!query.ignoreCache) { SoftExpiringCache.CachedObject<Object> cached = cache.getElement(query.specUri); if (cached != null) { obj = cached.obj; cache.addElement(query.specUri, obj, refresh); executor.execute(new SpecUpdater(query, obj)); cache.addElement(query.specUri, obj, refresh);
private SoftExpiringCache<String, String> makeSoftExpiringCache() { SoftExpiringCache<String, String> expiringCache = new SoftExpiringCache<String, String>(cache); expiringCache.setTimeSource(timeSource); return expiringCache; }
@Test(expected = SpecParserException.class) public void negativeCachingEnforced() throws Exception { specFactory.cache.addElement(SPEC_URL.toString(), new SpecParserException("broken"), 1000); specFactory.getGadgetSpec(createContext(SPEC_URL, false)); }
@Test public void testMissingValue() { SoftExpiringCache<String, String> expiringCache = makeSoftExpiringCache(); assertNull(expiringCache.getElement("not set")); } }
/** * @param clazz the class for spec objects. * @param executor for asynchronously updating specs * @param pipeline the request pipeline for fetching new specs * @param cache a cache for parsed spec objects * @param refresh the frequency at which to update specs, independent of cache expiration policy */ public AbstractSpecFactory(Class<T> clazz, ExecutorService executor, RequestPipeline pipeline, Cache<Uri, Object> cache, long refresh) { this.clazz = clazz; this.executor = executor; this.pipeline = pipeline; this.cache = new SoftExpiringCache<Uri, Object>(cache); this.refresh = refresh; }
@Test public void badResponseServedFromCache() throws Exception { HttpResponse expiredResponse = new HttpResponseBuilder() .setResponse(BASIC_BUNDLE.getBytes("UTF-8")) .addHeader("Pragma", "no-cache") .create(); HttpResponse badResponse = HttpResponse.error(); expect(pipeline.execute(isA(HttpRequest.class))) .andReturn(expiredResponse).once(); expect(pipeline.execute(isA(HttpRequest.class))) .andReturn(badResponse).once(); replay(pipeline); final AtomicLong time = new AtomicLong(); bundleFactory.cache.setTimeSource(new TimeSource() { @Override public long currentTimeMillis() { return time.get(); } }); time.set(System.currentTimeMillis()); MessageBundle bundle0 = bundleFactory.getBundle(gadgetSpec, LOCALE, false, ContainerConfig.DEFAULT_CONTAINER); time.set(time.get() + MAX_AGE + 1); MessageBundle bundle1 = bundleFactory.getBundle(gadgetSpec, LOCALE, false, ContainerConfig.DEFAULT_CONTAINER); verify(pipeline); assertEquals(bundle0.getMessages().get(MSG_0_NAME), bundle1.getMessages().get(MSG_0_NAME)); }
Object obj = null; if (!query.ignoreCache) { SoftExpiringCache.CachedObject<Object> cached = cache.getElement(query.specUri.toString()); if (cached != null) { obj = cached.obj; cache.addElement(query.specUri.toString(), obj, refresh); executor.execute(new SpecUpdater(query, obj)); cache.addElement(query.specUri.toString(), obj, refresh);
private SoftExpiringCache<String, String> makeSoftExpiringCache() { SoftExpiringCache<String, String> expiringCache = new SoftExpiringCache<String, String>(cache); expiringCache.setTimeSource(timeSource); return expiringCache; }
@Test public void specRefetchedAsync() throws Exception { HttpRequest request = createCacheableRequest(); HttpResponse response = new HttpResponse(ALT_LOCAL_SPEC_XML); expect(pipeline.execute(request)).andReturn(response); replay(pipeline); specFactory.cache.addElement( SPEC_URL, new GadgetSpec(SPEC_URL, LOCAL_SPEC_XML), -1); GadgetSpec spec = specFactory.getGadgetSpec(createContext(SPEC_URL, false)); assertEquals(LOCAL_CONTENT, spec.getView(GadgetSpec.DEFAULT_VIEW).getContent()); spec = specFactory.getGadgetSpec(createContext(SPEC_URL, false)); assertEquals(ALT_LOCAL_CONTENT, spec.getView(GadgetSpec.DEFAULT_VIEW).getContent()); assertEquals(1, executor.runnableCount); }
@Test public void testMissingValue() { SoftExpiringCache<String, String> expiringCache = makeSoftExpiringCache(); assertNull(expiringCache.getElement("not set")); } }