Skip to content

Commit

Permalink
Rewamping UrlCheckerTest and adding it to the suite
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Jan 17, 2018
1 parent 57a86e1 commit 6fd9eea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion java/client/src/org/openqa/selenium/net/UrlChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class UrlChecker {

private static final Logger log = Logger.getLogger(UrlChecker.class.getName());

private static final int CONNECT_TIMEOUT_MS = 500;
static final int CONNECT_TIMEOUT_MS = 500;
private static final int READ_TIMEOUT_MS = 1000;
private static final long MAX_POLL_INTERVAL_MS = 320;
private static final long MIN_POLL_INTERVAL_MS = 10;
Expand Down
2 changes: 2 additions & 0 deletions java/client/test/org/openqa/selenium/SmallTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.openqa.selenium.logging.PerformanceLoggingMockTest;
import org.openqa.selenium.net.LinuxEphemeralPortRangeDetectorTest;
import org.openqa.selenium.net.NetworkUtilsTest;
import org.openqa.selenium.net.UrlCheckerTest;
import org.openqa.selenium.os.CommandLineTest;
import org.openqa.selenium.os.WindowsUtilsUnitTest;
import org.openqa.selenium.testing.drivers.IgnoreComparatorUnitTest;
Expand Down Expand Up @@ -56,6 +57,7 @@
PointerInputTest.class,
ProxyTest.class,
TemporaryFilesystemTest.class,
UrlCheckerTest.class,
WebDriverExceptionTest.class,
WindowsUtilsUnitTest.class,
ZipTest.class,
Expand Down
37 changes: 16 additions & 21 deletions java/client/test/org/openqa/selenium/net/UrlCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.openqa.selenium.net;

import static java.lang.System.currentTimeMillis;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertThat;
import static org.openqa.selenium.net.PortProber.findFreePort;
Expand All @@ -40,7 +41,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RunWith(JUnit4.class)
public class UrlCheckerTest {

private final UrlChecker urlChecker = new UrlChecker();
Expand All @@ -66,43 +66,38 @@ public void handle(String s, Request request, HttpServletRequest httpServletRequ

@Test
public void testWaitUntilAvailableIsTimely() throws Exception {
long delay = 200L;

executorService.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
Thread.sleep(10L);
server.start();
return null;
}
executorService.submit(() -> {
Thread.sleep(delay);
server.start();
return null;
});

long start = currentTimeMillis();
urlChecker.waitUntilAvailable(10, TimeUnit.SECONDS, new URL("http://localhost:" + port + "/"));
long elapsed = currentTimeMillis() - start;
assertThat(elapsed, lessThan(450L));
System.out.println(elapsed);
assertThat(elapsed, greaterThan((long) UrlChecker.CONNECT_TIMEOUT_MS)); // first connection is unsuccessful
assertThat(elapsed, lessThan(UrlChecker.CONNECT_TIMEOUT_MS + 100L)); // threshold
}

@Test
public void testWaitUntilUnavailableIsTimely() throws Exception {

long delay = 200L;
server.start();
urlChecker.waitUntilAvailable(10, TimeUnit.SECONDS, new URL("http://localhost:" + port + "/"));

executorService.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
Thread.sleep(10L);
server.stop();
return null;
}
executorService.submit(() -> {
Thread.sleep(delay);
server.stop();
return null;
});

long start = currentTimeMillis();
urlChecker.waitUntilUnavailable(10, TimeUnit.SECONDS,
new URL("http://localhost:" + port + "/"));
urlChecker.waitUntilUnavailable(10, TimeUnit.SECONDS, new URL("http://localhost:" + port + "/"));
long elapsed = currentTimeMillis() - start;
assertThat(elapsed, lessThan(450L));
assertThat(elapsed, greaterThan(UrlChecker.CONNECT_TIMEOUT_MS + delay)); // last connection is unsuccessful
assertThat(elapsed, lessThan(UrlChecker.CONNECT_TIMEOUT_MS + delay + 200L)); // threshold
System.out.println(elapsed);
}

Expand Down

0 comments on commit 6fd9eea

Please sign in to comment.