use of org.apache.wicket.util.time.Duration in project wicket by apache.
the class AjaxTimerBehaviorTest method addedInAjaxSetsTimout.
/**
* Tests timer behavior in a component added to an AjaxRequestTarget
*/
@Test
public void addedInAjaxSetsTimout() {
Duration dur = Duration.seconds(20);
final AjaxSelfUpdatingTimerBehavior timer = new AjaxSelfUpdatingTimerBehavior(dur);
final MockPageWithLinkAndComponent page = new MockPageWithLinkAndComponent();
page.add(new WebComponent(MockPageWithLinkAndComponent.COMPONENT_ID).setOutputMarkupId(true));
page.add(new AjaxLink<Void>(MockPageWithLinkAndComponent.LINK_ID) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
WebMarkupContainer wmc = new WebMarkupContainer(MockPageWithLinkAndComponent.COMPONENT_ID);
wmc.setOutputMarkupId(true);
wmc.add(timer);
page.replace(wmc);
target.add(wmc);
}
});
tester.startPage(page);
tester.clickLink(MockPageWithLinkAndComponent.LINK_ID);
// first render sets timeout
assertMatches("Wicket.Timer.set", 1);
tester.executeBehavior(timer);
assertMatches("Wicket.Timer.set", 1);
}
use of org.apache.wicket.util.time.Duration in project wicket by apache.
the class AjaxTimerBehaviorTest method ajaxUpdateDoesNotSetTimeout.
/**
* tests timer behavior in a WebPage.
*/
@Test
public void ajaxUpdateDoesNotSetTimeout() {
Duration dur = Duration.seconds(20);
final AjaxSelfUpdatingTimerBehavior timer = new AjaxSelfUpdatingTimerBehavior(dur);
final MockPageWithLinkAndComponent page = new MockPageWithLinkAndComponent();
final Label label = new Label(MockPageWithLinkAndComponent.COMPONENT_ID, "Hello");
page.add(label);
page.add(new AjaxLink<Void>(MockPageWithLinkAndComponent.LINK_ID) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
target.add(label);
}
});
label.setOutputMarkupId(true);
label.add(timer);
tester.startPage(page);
assertMatches("Wicket.Timer.set", 1);
tester.clickLink(MockPageWithLinkAndComponent.LINK_ID);
assertMatches("Wicket.Timer.set", 1);
tester.executeBehavior(timer);
assertMatches("Wicket.Timer.set", 1);
}
use of org.apache.wicket.util.time.Duration in project wicket by apache.
the class PageAccessSynchronizerTest method testSerialization.
/**
* @throws Exception
*/
@Test
public void testSerialization() throws Exception {
// a simple worker that acquires a lock on page 5
class Locker extends Thread {
private final PageAccessSynchronizer sync;
public Locker(PageAccessSynchronizer sync) {
this.sync = sync;
}
@Override
public void run() {
sync.lockPage(5);
}
}
// set up a synchronizer and lock page 5 with locker1
final Duration timeout = Duration.seconds(30);
final PageAccessSynchronizer sync = new PageAccessSynchronizer(timeout);
Locker locker1 = new Locker(sync);
final long start = System.currentTimeMillis();
locker1.run();
// make sure we can serialize the synchronizer
final PageAccessSynchronizer sync2 = WicketObjects.cloneObject(sync);
assertTrue(sync != sync2);
// make sure the clone does not retain locks by attempting to lock page locked by locker1 in
// locker2
Locker locker2 = new Locker(sync2);
locker2.run();
assertTrue(Duration.milliseconds(System.currentTimeMillis() - start).lessThan(timeout));
}
use of org.apache.wicket.util.time.Duration in project wicket by apache.
the class PageAccessSynchronizerTest method failToReleaseUnderLoad.
/**
* https://issues.apache.org/jira/browse/WICKET-5316
*
* @throws Exception
*/
@Test
public void failToReleaseUnderLoad() throws Exception {
final Duration duration = Duration.seconds(20);
/* seconds */
final ConcurrentLinkedQueue<Exception> errors = new ConcurrentLinkedQueue<Exception>();
final long endTime = System.currentTimeMillis() + duration.getMilliseconds();
// set the synchronizer timeout one second longer than the test runs to prevent
// starvation to become an issue
final PageAccessSynchronizer sync = new PageAccessSynchronizer(duration.add(Duration.ONE_SECOND));
final CountDownLatch latch = new CountDownLatch(100);
for (int count = 0; count < 100; count++) {
new Thread() {
@Override
public void run() {
try {
while (System.currentTimeMillis() < endTime) {
try {
logger.debug(Thread.currentThread().getName() + " locking");
sync.lockPage(0);
Thread.sleep(1);
logger.debug(Thread.currentThread().getName() + " locked");
sync.unlockAllPages();
logger.debug(Thread.currentThread().getName() + " unlocked");
Thread.sleep(5);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
errors.add(e);
} finally {
latch.countDown();
}
}
}.start();
}
latch.await();
if (!errors.isEmpty()) {
logger.error("Number of lock errors that occurred: {}", errors.size());
throw errors.remove();
}
}
use of org.apache.wicket.util.time.Duration in project wicket by apache.
the class AbstractResource method configureCache.
/**
* Configure the web response header for client cache control.
*
* @param data
* resource data
* @param attributes
* request attributes
*/
protected void configureCache(final ResourceResponse data, final Attributes attributes) {
Response response = attributes.getResponse();
if (response instanceof WebResponse) {
Duration duration = data.getCacheDuration();
WebResponse webResponse = (WebResponse) response;
if (duration.compareTo(Duration.NONE) > 0) {
webResponse.enableCaching(duration, data.getCacheScope());
} else {
webResponse.disableCaching();
}
}
}
Aggregations