Search in sources :

Example 11 with JavaScriptJobManager

use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager in project htmlunit by HtmlUnit.

the class WebClient method waitForBackgroundJavaScript.

/**
 * <p><span style="color:red">Experimental API: May be changed in next release
 * and may not yet work perfectly!</span></p>
 *
 * <p>This method blocks until all background JavaScript tasks have finished executing. Background
 * JavaScript tasks are JavaScript tasks scheduled for execution via <tt>window.setTimeout</tt>,
 * <tt>window.setInterval</tt> or asynchronous <tt>XMLHttpRequest</tt>.</p>
 *
 * <p>If a job is scheduled to begin executing after <tt>(now + timeoutMillis)</tt>, this method will
 * wait for <tt>timeoutMillis</tt> milliseconds and then return a value greater than <tt>0</tt>. This
 * method will never block longer than <tt>timeoutMillis</tt> milliseconds.</p>
 *
 * <p>Use this method instead of {@link #waitForBackgroundJavaScriptStartingBefore(long)} if you
 * don't know when your background JavaScript is supposed to start executing, but you're fairly sure
 * that you know how long it should take to finish executing.</p>
 *
 * @param timeoutMillis the maximum amount of time to wait (in milliseconds)
 * @return the number of background JavaScript jobs still executing or waiting to be executed when this
 *         method returns; will be <tt>0</tt> if there are no jobs left to execute
 */
public int waitForBackgroundJavaScript(final long timeoutMillis) {
    int count = 0;
    final long endTime = System.currentTimeMillis() + timeoutMillis;
    for (Iterator<WeakReference<JavaScriptJobManager>> i = jobManagers_.iterator(); i.hasNext(); ) {
        final JavaScriptJobManager jobManager;
        final WeakReference<JavaScriptJobManager> reference;
        try {
            reference = i.next();
            jobManager = reference.get();
            if (jobManager == null) {
                i.remove();
                continue;
            }
        } catch (final ConcurrentModificationException e) {
            i = jobManagers_.iterator();
            count = 0;
            continue;
        }
        final long newTimeout = endTime - System.currentTimeMillis();
        count += jobManager.waitForJobs(newTimeout);
    }
    if (count != getAggregateJobCount()) {
        final long newTimeout = endTime - System.currentTimeMillis();
        return waitForBackgroundJavaScript(newTimeout);
    }
    return count;
}
Also used : JavaScriptJobManager(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager) ConcurrentModificationException(java.util.ConcurrentModificationException) WeakReference(java.lang.ref.WeakReference)

Aggregations

JavaScriptJobManager (com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager)11 Test (org.junit.Test)8 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)7 Tries (com.gargoylesoftware.htmlunit.junit.BrowserRunner.Tries)4 WeakReference (java.lang.ref.WeakReference)3 ConcurrentModificationException (java.util.ConcurrentModificationException)3 MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 JavaScriptJob (com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob)1 MutableInt (org.apache.commons.lang3.mutable.MutableInt)1