Commit 5f685c86 authored by Michael Thiessen's avatar Michael Thiessen Committed by Commit Bot

Allow EmbeddedTestServer reuse across batched tests.

Migrates EmbeddedTestServer to a TestRule-based implementation, which
allows it to be used as an @ClassRule that can be reused across batched
tests.

Also removes a bunch of unnecessary locking that probably wasn't
necessary in the first place, and almost certainly isn't necessary now.

Bug: 989569
Change-Id: I1b5454b02251c08cd085eb4c9d70859acc1ed29a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2300327
Commit-Queue: Michael Thiessen <mthiesse@chromium.org>
Reviewed-by: default avatarMatt Mueller <mattm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#789522}
parent 5151f379
...@@ -7,41 +7,38 @@ package org.chromium.net.test; ...@@ -7,41 +7,38 @@ package org.chromium.net.test;
import android.content.Context; import android.content.Context;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import androidx.annotation.GuardedBy; import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description; import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/** /**
* Junit4 rule for starting embedded test server when necessary (i.e. when accessed via * Junit4 rule for starting embedded test server when necessary (i.e. when accessed via
* {@link #getServer()}), and shutting it down when the test finishes. * {@link #getServer()}), and shutting it down when the test finishes.
*/ */
public class EmbeddedTestServerRule extends TestWatcher { public class EmbeddedTestServerRule implements TestRule {
private final Object mLock = new Object();
@GuardedBy("mLock")
private EmbeddedTestServer mServer; private EmbeddedTestServer mServer;
// The default value of 0 will result in the same behavior as createAndStartServer // The default value of 0 will result in the same behavior as createAndStartServer
// (auto-selected port). // (auto-selected port).
@GuardedBy("mLock")
private int mServerPort; private int mServerPort;
@GuardedBy("mLock")
private boolean mUseHttps; private boolean mUseHttps;
@GuardedBy("mLock")
@ServerCertificate @ServerCertificate
private int mCertificateType = ServerCertificate.CERT_OK; private int mCertificateType = ServerCertificate.CERT_OK;
@Override @Override
protected void finished(Description description) { public Statement apply(Statement base, Description description) {
super.finished(description); return new Statement() {
synchronized (mLock) { @Override
if (mServer != null) { public void evaluate() throws Throwable {
mServer.stopAndDestroyServer(); try {
mServer = null; base.evaluate();
} finally {
if (mServer != null) mServer.stopAndDestroyServer();
} }
} }
};
} }
/** /**
...@@ -50,7 +47,6 @@ public class EmbeddedTestServerRule extends TestWatcher { ...@@ -50,7 +47,6 @@ public class EmbeddedTestServerRule extends TestWatcher {
* @return the test server. * @return the test server.
*/ */
public EmbeddedTestServer getServer() { public EmbeddedTestServer getServer() {
synchronized (mLock) {
if (mServer == null) { if (mServer == null) {
Context context = InstrumentationRegistry.getContext(); Context context = InstrumentationRegistry.getContext();
mServer = mUseHttps mServer = mUseHttps
...@@ -60,7 +56,6 @@ public class EmbeddedTestServerRule extends TestWatcher { ...@@ -60,7 +56,6 @@ public class EmbeddedTestServerRule extends TestWatcher {
} }
return mServer; return mServer;
} }
}
public String getOrigin() { public String getOrigin() {
return getServer().getURL("/"); return getServer().getURL("/");
...@@ -73,25 +68,19 @@ public class EmbeddedTestServerRule extends TestWatcher { ...@@ -73,25 +68,19 @@ public class EmbeddedTestServerRule extends TestWatcher {
* @param port the port to start the server with, or 0 for an automatically selected one. * @param port the port to start the server with, or 0 for an automatically selected one.
*/ */
public void setServerPort(int port) { public void setServerPort(int port) {
synchronized (mLock) {
assert mServer == null; assert mServer == null;
mServerPort = port; mServerPort = port;
} }
}
/** Sets whether to create an HTTPS (vs HTTP) server. */ /** Sets whether to create an HTTPS (vs HTTP) server. */
public void setServerUsesHttps(boolean useHttps) { public void setServerUsesHttps(boolean useHttps) {
synchronized (mLock) {
assert mServer == null; assert mServer == null;
mUseHttps = useHttps; mUseHttps = useHttps;
} }
}
/** Sets what type of certificate the server uses when running as an HTTPS server. */ /** Sets what type of certificate the server uses when running as an HTTPS server. */
public void setCertificateType(@ServerCertificate int certificateType) { public void setCertificateType(@ServerCertificate int certificateType) {
synchronized (mLock) {
assert mServer == null; assert mServer == null;
mCertificateType = certificateType; mCertificateType = certificateType;
} }
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment