Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(794)

Unified Diff: net/test/android/javatests/src/org/chromium/net/test/BaseTestServer.java

Issue 869083004: [Android] Revised initial commit of the HttpTestServer. (abandoned) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/test/android/javatests/src/org/chromium/net/test/BaseTestServer.java
diff --git a/net/test/android/javatests/src/org/chromium/net/test/BaseTestServer.java b/net/test/android/javatests/src/org/chromium/net/test/BaseTestServer.java
index a9ca6bd6173c71caa9109e36a93fed265bc5ef00..8236dff75ff48a8cf7b5147c3ad368995c1eaabb 100644
--- a/net/test/android/javatests/src/org/chromium/net/test/BaseTestServer.java
+++ b/net/test/android/javatests/src/org/chromium/net/test/BaseTestServer.java
@@ -4,34 +4,72 @@
package org.chromium.net.test;
+import android.util.Log;
+
import java.util.concurrent.atomic.AtomicBoolean;
/** A base class for simple test servers. */
public abstract class BaseTestServer implements Runnable {
+ private static final String TAG = "BaseTestServer";
+
private AtomicBoolean mKeepRunning;
+ private final Object mLock;
+ private boolean mRunning;
/** Creates a test server. */
public BaseTestServer() {
mKeepRunning = new AtomicBoolean(true);
+ mLock = new Object();
}
+ /** Returns the port on which this server is listening for connections. */
+ public abstract int getServerPort();
+
/** Accepts incoming connections until stopped via stop(). */
public void run() {
- mKeepRunning.set(true);
+ serverHasStarted();
- while (mKeepRunning.get()) {
- accept();
+ try {
+ while (mKeepRunning.get()) {
+ accept();
+ }
+ } finally {
+ serverHasStopped();
}
}
- /** Waits for and handles an incoming request. */
- protected abstract void accept();
+ /** Waits for the server to start. */
+ public void waitForServerToStart() {
+ synchronized (mLock) {
+ while (!mRunning) {
+ try {
+ mLock.wait();
+ } catch (InterruptedException e) {
+ Log.e(TAG, "Interrupted while waiting for server to stop: " + e.toString());
+ }
+ }
+ }
+ }
- /** Returns the port on which this server is listening for connections. */
- public abstract int getServerPort();
+ private void serverHasStarted() {
+ synchronized (mLock) {
+ mRunning = true;
+ mLock.notify();
+ }
+ }
/** Stops the server. */
public void stop() {
mKeepRunning.set(false);
}
+
+ /** Waits for and handles an incoming request. */
+ protected abstract void accept();
+
+ private void serverHasStopped() {
+ synchronized (mLock) {
+ mRunning = false;
+ mLock.notify();
+ }
+ }
}

Powered by Google App Engine
This is Rietveld 408576698