/** * Verifies that getting a value which is expired will return <code>null</code>. * * @throws Exception */ @Test public void getExpiredValue() throws Exception { Time start = Time.now(); Duration timeout = Duration.milliseconds(50); StoredResponsesMap map = new StoredResponsesMap(1000, timeout); assertEquals(0, map.size()); map.put("1", new BufferedWebResponse(null)); assertEquals(1, map.size()); TimeUnit.MILLISECONDS.sleep( timeout.getMilliseconds() * 2); // sleep for twice longer than the timeout assertTrue("The timeout has passed.", Time.now().subtract(start).compareTo(timeout) == 1); Object value = map.get("1"); assertNull(value); }
protected ResourceResponse newResourceResponse(final IResource.Attributes attributes) { final ResourceResponse response = new ResourceResponse(); if (this.lastModifiedTime != null) { response.setLastModified(this.lastModifiedTime); } else { response.setLastModified(Time.now()); } if (response.dataNeedsToBeWritten(attributes)) { response.setContentType("image/" + this.getFormat()); response.setContentDisposition(ContentDisposition.INLINE); final byte[] imageData = this.getImageData(attributes); if (imageData == null) { response.setError(404); } else { response.setWriteCallback( new WriteCallback() { public void writeData(final IResource.Attributes attributes) { attributes.getResponse().write(imageData); } }); this.configureResponse(response, attributes); } } return response; }
@Override public void run() { sync.lockPage(1); t1locks[0] = Time.now(); hold.sleep(); sync.unlockAllPages(); }
/** * Gets the elapsed duration since this application was initialized. * * @return the uptime */ public Duration getUptime() { final DateTime startup = getStartupDate(); if (null != startup) { return Duration.elapsed(Time.valueOf(startup.toDate())); } return Duration.NONE; }
@Override public void run() { Random random = new Random(); Time start = Time.now(); while (start.elapsedSince().lessThan(duration) && error[0] == null) { logger.info( "{} elapsed: {}, duration: {}", new Object[] {Thread.currentThread().getName(), start.elapsedSince(), duration}); int page1 = random.nextInt(counts.length); int page2 = random.nextInt(counts.length); int count = 0; while (page2 == page1 && count < 100) { page2 = random.nextInt(counts.length); count++; } if (page2 == page1) { throw new RuntimeException("orly?"); } try { sync.lockPage(page1); sync.lockPage(page2); // have locks, increment the count counts[page1].incrementAndGet(); counts[page2].incrementAndGet(); hits.incrementAndGet(); // hold the lock for some time try { Thread.sleep(50); } catch (InterruptedException e) { error[0] = "Worker :" + Thread.currentThread().getName() + " interrupted"; } // decrement the counts counts[page1].decrementAndGet(); counts[page2].decrementAndGet(); // release lock } catch (CouldNotLockPageException e) { // ignore } finally { sync.unlockAllPages(); } } }
public static boolean isConnected( final Application application, final String id, final Duration timeout) { final Time time = TimerChannelBehavior.getLastPollEvent(application, id); boolean isConnected; if (time == null) { // the behavior has been cleaned return false; } isConnected = time.elapsedSince().compareTo(timeout) < 0; if (!isConnected) { // timeout expired, the page is probably not connected anymore // we clean the metadata to avoid memory leak TimerChannelBehavior.cleanMetadata(application, id); } return isConnected; }
private static void touch(final Application application, String id) { id = getPageId(application, id); ConcurrentMap<String, Time> eventsTimeById; synchronized (application) { eventsTimeById = application.getMetaData(EVENTS_KEY); if (eventsTimeById == null) { eventsTimeById = new ConcurrentHashMap<String, Time>(); application.setMetaData(EVENTS_KEY, eventsTimeById); } } eventsTimeById.put(id, Time.now()); }
/** * Sets the last-modified header field and the expires field. * * @param when */ protected final void setLastModified(Date when) { if (when == null) { return; } if (when.before(app().getBootDate())) { // last-modified can not be before the Gitblit boot date // this helps ensure that pages are properly refreshed after a // server config change when = app().getBootDate(); } int expires = app().settings().getInteger(Keys.web.pageCacheExpires, 0); WebResponse response = (WebResponse) getResponse(); response.setLastModifiedTime(Time.valueOf(when)); response.setDateHeader( "Expires", System.currentTimeMillis() + Duration.minutes(expires).getMilliseconds()); }
private void setResponse(String response) { resourceStream.clear(); resourceStream.append(response); resourceStream.setLastModified(Time.now()); }