diff options
author | boliu@chromium.org <boliu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-24 17:38:46 +0000 |
---|---|---|
committer | boliu@chromium.org <boliu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-24 17:38:46 +0000 |
commit | 75f94f279954227244aad06b34630d4e8ea854ef (patch) | |
tree | 94f5809d3f60c8fae51257b28cdab2a369b73de6 /net | |
parent | 31b8b58786dc448cca375f2a68b04bf68e854dbf (diff) | |
download | chromium_src-75f94f279954227244aad06b34630d4e8ea854ef.zip chromium_src-75f94f279954227244aad06b34630d4e8ea854ef.tar.gz chromium_src-75f94f279954227244aad06b34630d4e8ea854ef.tar.bz2 |
Synchronize android test server internal maps
Count, request, and response maps are modified on two different
threads in tests are possibly causing some tests to be flaky.
So add synchronization.
BUG=171765
Android only change. Ran through android bots.
NOTRY=true
Review URL: https://chromiumcodereview.appspot.com/12036063
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@178601 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java | 61 |
1 files changed, 41 insertions, 20 deletions
diff --git a/net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java b/net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java index 5ce3726..c892afe 100644 --- a/net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java +++ b/net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java @@ -88,6 +88,9 @@ public class TestWebServer { } } + // The Maps below are modified on both the client thread and the internal server thread, so + // need to use a lock when accessing them. + private final Object mLock = new Object(); private Map<String, Response> mResponseMap = new HashMap<String, Response>(); private Map<String, Integer> mResponseCountMap = new HashMap<String, Integer>(); private Map<String, HttpRequest> mLastRequestMap = new HashMap<String, HttpRequest>(); @@ -157,9 +160,12 @@ public class TestWebServer { List<Pair<String, String>> responseHeaders, int status) { final boolean isRedirect = (status == RESPONSE_STATUS_MOVED_TEMPORARILY); - mResponseMap.put(requestPath, new Response(responseData, responseHeaders, isRedirect)); - mResponseCountMap.put(requestPath, Integer.valueOf(0)); - mLastRequestMap.put(requestPath, null); + + synchronized (mLock) { + mResponseMap.put(requestPath, new Response(responseData, responseHeaders, isRedirect)); + mResponseCountMap.put(requestPath, Integer.valueOf(0)); + mLastRequestMap.put(requestPath, null); + } return getResponseUrl(requestPath); } @@ -235,7 +241,10 @@ public class TestWebServer { * Get the number of requests was made at this path since it was last set. */ public int getRequestCount(String requestPath) { - Integer count = mResponseCountMap.get(requestPath); + Integer count = null; + synchronized (mLock) { + count = mResponseCountMap.get(requestPath); + } if (count == null) throw new IllegalArgumentException("Path not set: " + requestPath); return count.intValue(); } @@ -244,9 +253,11 @@ public class TestWebServer { * Returns the last HttpRequest at this path. Can return null if it is never requested. */ public HttpRequest getLastRequest(String requestPath) { - if (!mLastRequestMap.containsKey(requestPath)) - throw new IllegalArgumentException("Path not set: " + requestPath); - return mLastRequestMap.get(requestPath); + synchronized (mLock) { + if (!mLastRequestMap.containsKey(requestPath)) + throw new IllegalArgumentException("Path not set: " + requestPath); + return mLastRequestMap.get(requestPath); + } } public String getBaseUrl() { @@ -310,9 +321,11 @@ public class TestWebServer { } private void servedResponseFor(String path, HttpRequest request) { - mResponseCountMap.put(path, Integer.valueOf( - mResponseCountMap.get(path).intValue() + 1)); - mLastRequestMap.put(path, request); + synchronized (mLock) { + mResponseCountMap.put(path, Integer.valueOf( + mResponseCountMap.get(path).intValue() + 1)); + mLastRequestMap.put(path, request); + } } /** @@ -327,7 +340,10 @@ public class TestWebServer { URI uri = URI.create(uriString); String path = uri.getPath(); - Response response = mResponseMap.get(path); + Response response = null; + synchronized (mLock) { + response = mResponseMap.get(path); + } if (path.equals(SHUTDOWN_PREFIX)) { httpResponse = createResponse(HttpStatus.SC_OK); } else if (response == null) { @@ -362,16 +378,21 @@ public class TestWebServer { */ private HttpResponse createResponse(int status) { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_0, status, null); - - if (sReasons == null) { - sReasons = new Hashtable<Integer, String>(); - sReasons.put(HttpStatus.SC_UNAUTHORIZED, "Unauthorized"); - sReasons.put(HttpStatus.SC_NOT_FOUND, "Not Found"); - sReasons.put(HttpStatus.SC_FORBIDDEN, "Forbidden"); - sReasons.put(HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); + String reason = null; + + // This synchronized silences findbugs. + synchronized (TestWebServer.class) { + if (sReasons == null) { + sReasons = new Hashtable<Integer, String>(); + sReasons.put(HttpStatus.SC_UNAUTHORIZED, "Unauthorized"); + sReasons.put(HttpStatus.SC_NOT_FOUND, "Not Found"); + sReasons.put(HttpStatus.SC_FORBIDDEN, "Forbidden"); + sReasons.put(HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); + } + // Fill in error reason. Avoid use of the ReasonPhraseCatalog, which is + // Locale-dependent. + reason = sReasons.get(status); } - // Fill in error reason. Avoid use of the ReasonPhraseCatalog, which is Locale-dependent. - String reason = sReasons.get(status); if (reason != null) { StringBuffer buf = new StringBuffer("<html><head><title>"); |