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

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

Issue 1131803002: [Android] Fix race condition in BaseTestServer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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
« no previous file with comments | « net/net.gyp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..c54de3d8ddae0647d4cb91c23c07a919cb207c50 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,23 +4,54 @@
package org.chromium.net.test;
+import org.chromium.base.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 = Log.makeTag("net.test");
+
private AtomicBoolean mKeepRunning;
+ private final Object mLock;
+ private boolean mRunning;
/** Creates a test server. */
public BaseTestServer() {
mKeepRunning = new AtomicBoolean(true);
+ mLock = new Object();
}
/** Accepts incoming connections until stopped via stop(). */
public void run() {
- mKeepRunning.set(true);
+ serverHasStarted();
+
+ try {
+ while (mKeepRunning.get()) {
+ accept();
+ }
+ } finally {
+ serverHasStopped();
+ }
+ }
- while (mKeepRunning.get()) {
- 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);
+ }
+ }
+ }
+ }
+
+ private void serverHasStarted() {
+ synchronized (mLock) {
+ mRunning = true;
+ mLock.notifyAll();
}
}
@@ -34,4 +65,11 @@ public abstract class BaseTestServer implements Runnable {
public void stop() {
mKeepRunning.set(false);
}
+
+ private void serverHasStopped() {
+ synchronized (mLock) {
+ mRunning = false;
+ mLock.notifyAll();
+ }
+ }
}
« no previous file with comments | « net/net.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698