Skip to content

Commit

Permalink
Simplified the Test Server startup
Browse files Browse the repository at this point in the history
Removed the counting latch as Jetty will only start if it's not already running, so the latch was redundant. The stop wasn't being called anyway (which is good - we want the same server to run throughout the test session, vs restarting for each test class).

Also ensure the server binds only to localhost, vs to 0.0.0.0.

Fixes #1822
  • Loading branch information
jhy committed Aug 9, 2022
1 parent c58112a commit 5ed84f6
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 62 deletions.
6 changes: 0 additions & 6 deletions src/test/java/org/jsoup/integration/ConnectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.jsoup.parser.HtmlTreeBuilder;
import org.jsoup.parser.Parser;
import org.jsoup.parser.XmlTreeBuilder;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -41,11 +40,6 @@ public static void setUp() {
echoUrl = EchoServlet.Url;
}

@AfterAll
public static void tearDown() {
TestServer.stop();
}

@Test
public void canConnectToLocalServer() throws IOException {
String url = HelloServlet.Url;
Expand Down
7 changes: 0 additions & 7 deletions src/test/java/org/jsoup/integration/SessionIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.UncheckedIOException;
import org.jsoup.integration.servlets.EchoServlet;
import org.jsoup.integration.servlets.FileServlet;
import org.jsoup.integration.servlets.SlowRider;
import org.jsoup.nodes.Document;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -23,11 +21,6 @@ public static void setUp() {
TestServer.start();
}

@AfterAll
public static void tearDown() {
TestServer.stop();
}

@Test
public void multiThread() throws InterruptedException {
int numThreads = 20;
Expand Down
6 changes: 0 additions & 6 deletions src/test/java/org/jsoup/integration/SessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -24,11 +23,6 @@ public static void setUp() {
TestServer.start();
}

@AfterAll
public static void tearDown() {
TestServer.stop();
}

private static Elements keyEls(String key, Document doc) {
return doc.select("th:contains(" + key + ") + td");
}
Expand Down
29 changes: 6 additions & 23 deletions src/test/java/org/jsoup/integration/TestServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import org.eclipse.jetty.servlet.ServletHandler;
import org.jsoup.integration.servlets.BaseServlet;

import java.util.concurrent.atomic.AtomicInteger;
import java.net.InetSocketAddress;

public class TestServer {
private static final Server jetty = new Server(0);
private static final Server jetty = new Server(new InetSocketAddress("localhost", 0));
private static final ServletHandler handler = new ServletHandler();
private static AtomicInteger latch = new AtomicInteger(0);

static {
jetty.setHandler(handler);
Expand All @@ -21,26 +20,10 @@ private TestServer() {

public static void start() {
synchronized (jetty) {
int count = latch.getAndIncrement();
if (count == 0) {
try {
jetty.start();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
}

public static void stop() {
synchronized (jetty) {
int count = latch.getAndDecrement();
if (count == 0) {
try {
jetty.stop();
} catch (Exception e) {
throw new IllegalStateException(e);
}
try {
jetty.start(); // jetty will safely no-op a start on an already running instance
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/test/java/org/jsoup/nodes/FormElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.jsoup.integration.servlets.EchoServlet;
import org.jsoup.integration.servlets.FileServlet;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -27,11 +26,6 @@ public static void setUp() {
TestServer.start();
}

@AfterAll
public static void tearDown() {
TestServer.stop();
}

@Test public void hasAssociatedControls() {
//"button", "fieldset", "input", "keygen", "object", "output", "select", "textarea"
String html = "<form id=1><button id=1><fieldset id=2 /><input id=3><keygen id=4><object id=5><output id=6>" +
Expand Down
14 changes: 0 additions & 14 deletions src/test/java/org/jsoup/nodes/PositionTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package org.jsoup.nodes;

import org.jsoup.Jsoup;
import org.jsoup.integration.TestServer;
import org.jsoup.integration.servlets.EchoServlet;
import org.jsoup.integration.servlets.FileServlet;
import org.jsoup.parser.Parser;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand Down Expand Up @@ -145,16 +141,6 @@ class PositionTest {
assertEquals("6,1:80-6,17:96", comment.sourceRange().toString());
}

@BeforeAll
static void setUp() {
TestServer.start();
}

@AfterAll
static void tearDown() {
TestServer.stop();
}

@Test void tracksFromFetch() throws IOException {
String url = FileServlet.urlTo("/htmltests/large.html"); // 280 K
Document doc = Jsoup.connect(url).parser(TrackingParser).get();
Expand Down

0 comments on commit 5ed84f6

Please sign in to comment.