Search in sources :

Example 1 with JavaScriptJobManager

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

the class WebClient method getAggregateJobCount.

/**
 * Returns the aggregate background JavaScript job count across all windows.
 * @return the aggregate background JavaScript job count across all windows
 */
private int getAggregateJobCount() {
    int count = 0;
    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 int jobCount = jobManager.getJobCount();
        count += jobCount;
    }
    return count;
}
Also used : JavaScriptJobManager(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager) ConcurrentModificationException(java.util.ConcurrentModificationException) WeakReference(java.lang.ref.WeakReference)

Example 2 with JavaScriptJobManager

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

the class ThreadSynchronizer method waitWhenLastJobStartsNewOne.

/**
 * When waitForBackgroundJavaScriptStartingBefore is called and a new job is scheduled after the one that
 * is first found as the last one within the delay (a job starts a new one or simply a setInterval),
 * a few retries should be done to see if new jobs exists.
 * @throws Exception if the test fails
 */
@Test
public void waitWhenLastJobStartsNewOne() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <title>test</title>\n" + "  <script>\n" + "    function test() {\n" + "      setTimeout(doWork1, 200);\n" + "    }\n" + "    function doWork1() {\n" + "      alert('work1');\n" + "      setTimeout(doWork2, 200);\n" + "    }\n" + "    function doWork2() {\n" + "      alert('work2');\n" + "    }\n" + "  </script>\n" + "</head>\n" + "<body onload='test()'>\n" + "</body>\n" + "</html>";
    final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
    final HtmlPage page = loadPage(html, collectedAlerts);
    final JavaScriptJobManager jobManager = page.getEnclosingWindow().getJobManager();
    assertNotNull(jobManager);
    assertEquals(1, jobManager.getJobCount());
    startTimedTest();
    assertEquals(0, page.getWebClient().waitForBackgroundJavaScriptStartingBefore(20_000));
    assertMaxTestRunTime(1000);
    assertEquals(0, jobManager.getJobCount());
    final String[] expectedAlerts = { "work1", "work2" };
    assertEquals(expectedAlerts, collectedAlerts);
}
Also used : JavaScriptJobManager(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Test(org.junit.Test)

Example 3 with JavaScriptJobManager

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

the class ThreadSynchronizer method waitCalledDuringJobExecution.

/**
 * When waitForBackgroundJavaScriptStartingBefore is called while a job is being executed, it has
 * to wait for this job to finish, even if this clearXXX has been called for it.
 * @throws Exception if the test fails
 */
@Test
@Tries(3)
public void waitCalledDuringJobExecution() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <title>test</title>\n" + "  <script>\n" + "    var intervalId;\n" + "    function test() {\n" + "      intervalId = setTimeout(doWork, 100);\n" + "    }\n" + "    function doWork() {\n" + "      clearTimeout(intervalId);\n" + "      // waitForBackgroundJavaScriptStartingBefore should be called when JS execution is here\n" + "      var request = new XMLHttpRequest();\n" + "      request.open('GET', 'wait', false);\n" + "      request.send('');\n" + "      alert('end work');\n" + "    }\n" + "  </script>\n" + "</head>\n" + "<body onload='test()'>\n" + "</body>\n" + "</html>";
    final ThreadSynchronizer threadSynchronizer = new ThreadSynchronizer();
    final MockWebConnection webConnection = new MockWebConnection() {

        @Override
        public WebResponse getResponse(final WebRequest request) throws IOException {
            if (request.getUrl().toExternalForm().endsWith("/wait")) {
                threadSynchronizer.waitForState("just before waitForBackgroundJavaScriptStartingBefore");
                // main thread need to be able to process next instruction
                threadSynchronizer.sleep(400);
            }
            return super.getResponse(request);
        }
    };
    webConnection.setResponse(URL_FIRST, html);
    webConnection.setDefaultResponse("");
    final WebClient client = getWebClient();
    client.setWebConnection(webConnection);
    final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final HtmlPage page = client.getPage(URL_FIRST);
    final JavaScriptJobManager jobManager = page.getEnclosingWindow().getJobManager();
    assertNotNull(jobManager);
    assertEquals(1, jobManager.getJobCount());
    startTimedTest();
    threadSynchronizer.setState("just before waitForBackgroundJavaScriptStartingBefore");
    assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(20_000));
    assertMaxTestRunTime(600);
    assertEquals(0, jobManager.getJobCount());
    final String[] expectedAlerts = { "end work" };
    assertEquals(expectedAlerts, collectedAlerts);
}
Also used : JavaScriptJobManager(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Test(org.junit.Test) Tries(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Tries)

Example 4 with JavaScriptJobManager

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

the class ThreadSynchronizer method dontWaitWhenUnnecessary.

/**
 * @throws Exception if the test fails
 */
@Test
public void dontWaitWhenUnnecessary() throws Exception {
    final String content = "<html>\n" + "<head>\n" + "  <title>test</title>\n" + "  <script>\n" + "    var threadID;\n" + "    function test() {\n" + "      threadID = setTimeout(doAlert, 10000);\n" + "    }\n" + "    function doAlert() {\n" + "      alert('blah');\n" + "    }\n" + "  </script>\n" + "</head>\n" + "<body onload='test()'>\n" + "</body>\n" + "</html>";
    final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
    final HtmlPage page = loadPage(content, collectedAlerts);
    final JavaScriptJobManager jobManager = page.getEnclosingWindow().getJobManager();
    assertNotNull(jobManager);
    assertEquals(1, jobManager.getJobCount());
    startTimedTest();
    assertEquals(1, page.getWebClient().waitForBackgroundJavaScriptStartingBefore(7000));
    assertMaxTestRunTime(100);
    assertEquals(1, jobManager.getJobCount());
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
Also used : JavaScriptJobManager(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Test(org.junit.Test)

Example 5 with JavaScriptJobManager

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

the class ThreadSynchronizer method newJobStartedAfterWait.

/**
 * Test the case where a job is being executed at the time where waitForBackgroundJavaScriptStartingBefore
 * and where this jobs schedules a new job after call to waitForBackgroundJavaScriptStartingBefore,
 * .
 * @throws Exception if the test fails
 */
@Test
@Tries(3)
public void newJobStartedAfterWait() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <title>test</title>\n" + "  <script>\n" + "    var request;\n" + "    function onReadyStateChange() {\n" + "      if (request.readyState == 4) {\n" + "        alert('xhr onchange');\n" + "        setTimeout(doWork1, 200);\n" + "      }\n" + "    }\n" + "    function test() {\n" + "      request = new XMLHttpRequest();\n" + "      request.open('GET', 'wait', true);\n" + "      request.onreadystatechange = onReadyStateChange;\n" + "      // waitForBackgroundJavaScriptStartingBefore should be called when JS execution is in send()\n" + "      request.send('');\n" + "    }\n" + "    function doWork1() {\n" + "      alert('work1');\n" + "    }\n" + "  </script>\n" + "</head>\n" + "<body onload='test()'>\n" + "</body>\n" + "</html>";
    final ThreadSynchronizer threadSynchronizer = new ThreadSynchronizer();
    final MockWebConnection webConnection = new MockWebConnection() {

        @Override
        public WebResponse getResponse(final WebRequest request) throws IOException {
            if (request.getUrl().toExternalForm().endsWith("/wait")) {
                threadSynchronizer.waitForState("just before waitForBackgroundJavaScriptStartingBefore");
                // main thread need to be able to process next instruction
                threadSynchronizer.sleep(400);
            }
            return super.getResponse(request);
        }
    };
    webConnection.setResponse(URL_FIRST, html);
    webConnection.setDefaultResponse("");
    final WebClient client = getWebClient();
    client.setWebConnection(webConnection);
    final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final HtmlPage page = client.getPage(URL_FIRST);
    final JavaScriptJobManager jobManager = page.getEnclosingWindow().getJobManager();
    assertNotNull(jobManager);
    assertEquals(1, jobManager.getJobCount());
    startTimedTest();
    threadSynchronizer.setState("just before waitForBackgroundJavaScriptStartingBefore");
    assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(20_000));
    assertMaxTestRunTime(1000);
    assertEquals(0, jobManager.getJobCount());
    final String[] expectedAlerts = { "xhr onchange", "work1" };
    assertEquals(expectedAlerts, collectedAlerts);
}
Also used : JavaScriptJobManager(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Test(org.junit.Test) Tries(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Tries)

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