Search in sources :

Example 11 with Duration

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);
}
Also used : WebComponent(org.apache.wicket.markup.html.WebComponent) MockPageWithLinkAndComponent(org.apache.wicket.MockPageWithLinkAndComponent) Duration(org.apache.wicket.util.time.Duration) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) Test(org.junit.Test)

Example 12 with Duration

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);
}
Also used : MockPageWithLinkAndComponent(org.apache.wicket.MockPageWithLinkAndComponent) Label(org.apache.wicket.markup.html.basic.Label) Duration(org.apache.wicket.util.time.Duration) Test(org.junit.Test)

Example 13 with Duration

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));
}
Also used : Duration(org.apache.wicket.util.time.Duration) Test(org.junit.Test)

Example 14 with Duration

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();
    }
}
Also used : Duration(org.apache.wicket.util.time.Duration) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 15 with Duration

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();
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response) WebResponse(org.apache.wicket.request.http.WebResponse) Duration(org.apache.wicket.util.time.Duration)

Aggregations

Duration (org.apache.wicket.util.time.Duration)17 Test (org.junit.Test)11 MockPageWithLinkAndComponent (org.apache.wicket.MockPageWithLinkAndComponent)4 Time (org.apache.wicket.util.time.Time)4 Label (org.apache.wicket.markup.html.basic.Label)3 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)2 AbstractResource (org.apache.wicket.request.resource.AbstractResource)2 BaseWicketTester (org.apache.wicket.util.tester.BaseWicketTester)2 JSONArray (com.github.openjson.JSONArray)1 JSONException (com.github.openjson.JSONException)1 JSONObject (com.github.openjson.JSONObject)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Page (org.apache.wicket.Page)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 Method (org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method)1 IAjaxCallListener (org.apache.wicket.ajax.attributes.IAjaxCallListener)1 ThrottlingSettings (org.apache.wicket.ajax.attributes.ThrottlingSettings)1 JSONFunction (org.apache.wicket.ajax.json.JSONFunction)1