Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import android.os.ConditionVariable; | 7 import android.os.ConditionVariable; |
| 8 import android.os.StrictMode; | 8 import android.os.StrictMode; |
| 9 | 9 |
| 10 import static junit.framework.Assert.assertEquals; | 10 import static junit.framework.Assert.assertEquals; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 | 39 |
| 40 public int mHttpResponseDataLength = 0; | 40 public int mHttpResponseDataLength = 0; |
| 41 public String mResponseAsString = ""; | 41 public String mResponseAsString = ""; |
| 42 | 42 |
| 43 private static final int READ_BUFFER_SIZE = 32 * 1024; | 43 private static final int READ_BUFFER_SIZE = 32 * 1024; |
| 44 | 44 |
| 45 // When false, the consumer is responsible for all calls into the request | 45 // When false, the consumer is responsible for all calls into the request |
| 46 // that advance it. | 46 // that advance it. |
| 47 private boolean mAutoAdvance = true; | 47 private boolean mAutoAdvance = true; |
| 48 | 48 |
| 49 // Whether to permit calls on the network thread. | |
| 50 private boolean mAllowDirectExecutor = false; | |
| 51 | |
| 49 // Conditionally fail on certain steps. | 52 // Conditionally fail on certain steps. |
| 50 private FailureType mFailureType = FailureType.NONE; | 53 private FailureType mFailureType = FailureType.NONE; |
| 51 private ResponseStep mFailureStep = ResponseStep.NOTHING; | 54 private ResponseStep mFailureStep = ResponseStep.NOTHING; |
| 52 | 55 |
| 53 // Signals when request is done either successfully or not. | 56 // Signals when request is done either successfully or not. |
| 54 private final ConditionVariable mDone = new ConditionVariable(); | 57 private final ConditionVariable mDone = new ConditionVariable(); |
| 55 | 58 |
| 56 // Signaled on each step when mAutoAdvance is false. | 59 // Signaled on each step when mAutoAdvance is false. |
| 57 private final ConditionVariable mStepBlock = new ConditionVariable(); | 60 private final ConditionVariable mStepBlock = new ConditionVariable(); |
| 58 | 61 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 // Same as above, but continues to advance the request after posting | 104 // Same as above, but continues to advance the request after posting |
| 102 // the cancellation task. | 105 // the cancellation task. |
| 103 CANCEL_ASYNC_WITHOUT_PAUSE, | 106 CANCEL_ASYNC_WITHOUT_PAUSE, |
| 104 THROW_SYNC | 107 THROW_SYNC |
| 105 } | 108 } |
| 106 | 109 |
| 107 public void setAutoAdvance(boolean autoAdvance) { | 110 public void setAutoAdvance(boolean autoAdvance) { |
| 108 mAutoAdvance = autoAdvance; | 111 mAutoAdvance = autoAdvance; |
| 109 } | 112 } |
| 110 | 113 |
| 114 public void setAllowDirectExecutor(boolean allowed) { | |
|
mef
2016/08/30 17:55:47
maybe instead of setAllowDirectExecutor() use setU
Charles
2016/08/30 20:11:06
That wouldn't fly for javaurlrequest, which has a
| |
| 115 mAllowDirectExecutor = allowed; | |
| 116 } | |
| 117 | |
| 111 public void setFailure(FailureType failureType, ResponseStep failureStep) { | 118 public void setFailure(FailureType failureType, ResponseStep failureStep) { |
| 112 mFailureStep = failureStep; | 119 mFailureStep = failureStep; |
| 113 mFailureType = failureType; | 120 mFailureType = failureType; |
| 114 } | 121 } |
| 115 | 122 |
| 116 public void blockForDone() { | 123 public void blockForDone() { |
| 117 mDone.block(); | 124 mDone.block(); |
| 118 } | 125 } |
| 119 | 126 |
| 120 public void waitForNextStep() { | 127 public void waitForNextStep() { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 144 mRedirectResponseInfoList.add(info); | 151 mRedirectResponseInfoList.add(info); |
| 145 ++mRedirectCount; | 152 ++mRedirectCount; |
| 146 if (maybeThrowCancelOrPause(request)) { | 153 if (maybeThrowCancelOrPause(request)) { |
| 147 return; | 154 return; |
| 148 } | 155 } |
| 149 request.followRedirect(); | 156 request.followRedirect(); |
| 150 } | 157 } |
| 151 | 158 |
| 152 @Override | 159 @Override |
| 153 public void onResponseStarted(UrlRequest request, UrlResponseInfo info) { | 160 public void onResponseStarted(UrlRequest request, UrlResponseInfo info) { |
| 154 assertEquals(mExecutorThread, Thread.currentThread()); | 161 if (!mAllowDirectExecutor) { |
| 162 assertEquals(mExecutorThread, Thread.currentThread()); | |
| 163 } | |
| 155 assertFalse(request.isDone()); | 164 assertFalse(request.isDone()); |
| 156 assertTrue(mResponseStep == ResponseStep.NOTHING | 165 assertTrue(mResponseStep == ResponseStep.NOTHING |
| 157 || mResponseStep == ResponseStep.ON_RECEIVED_REDIRECT); | 166 || mResponseStep == ResponseStep.ON_RECEIVED_REDIRECT); |
| 158 assertNull(mError); | 167 assertNull(mError); |
| 159 | 168 |
| 160 mResponseStep = ResponseStep.ON_RESPONSE_STARTED; | 169 mResponseStep = ResponseStep.ON_RESPONSE_STARTED; |
| 161 mResponseInfo = info; | 170 mResponseInfo = info; |
| 162 if (maybeThrowCancelOrPause(request)) { | 171 if (maybeThrowCancelOrPause(request)) { |
| 163 return; | 172 return; |
| 164 } | 173 } |
| 165 startNextRead(request); | 174 startNextRead(request); |
| 166 } | 175 } |
| 167 | 176 |
| 168 @Override | 177 @Override |
| 169 public void onReadCompleted(UrlRequest request, UrlResponseInfo info, ByteBu ffer byteBuffer) { | 178 public void onReadCompleted(UrlRequest request, UrlResponseInfo info, ByteBu ffer byteBuffer) { |
| 170 assertEquals(mExecutorThread, Thread.currentThread()); | 179 if (!mAllowDirectExecutor) { |
| 180 assertEquals(mExecutorThread, Thread.currentThread()); | |
| 181 } | |
| 171 assertFalse(request.isDone()); | 182 assertFalse(request.isDone()); |
| 172 assertTrue(mResponseStep == ResponseStep.ON_RESPONSE_STARTED | 183 assertTrue(mResponseStep == ResponseStep.ON_RESPONSE_STARTED |
| 173 || mResponseStep == ResponseStep.ON_READ_COMPLETED); | 184 || mResponseStep == ResponseStep.ON_READ_COMPLETED); |
| 174 assertNull(mError); | 185 assertNull(mError); |
| 175 | 186 |
| 176 mResponseStep = ResponseStep.ON_READ_COMPLETED; | 187 mResponseStep = ResponseStep.ON_READ_COMPLETED; |
| 177 | 188 |
| 178 final byte[] lastDataReceivedAsBytes; | 189 final byte[] lastDataReceivedAsBytes; |
| 179 final int bytesRead = byteBuffer.position() - mBufferPositionBeforeRead; | 190 final int bytesRead = byteBuffer.position() - mBufferPositionBeforeRead; |
| 180 mHttpResponseDataLength += bytesRead; | 191 mHttpResponseDataLength += bytesRead; |
| 181 lastDataReceivedAsBytes = new byte[bytesRead]; | 192 lastDataReceivedAsBytes = new byte[bytesRead]; |
| 182 // Rewind |byteBuffer.position()| to pre-read() position. | 193 // Rewind |byteBuffer.position()| to pre-read() position. |
| 183 byteBuffer.position(mBufferPositionBeforeRead); | 194 byteBuffer.position(mBufferPositionBeforeRead); |
| 184 // This restores |byteBuffer.position()| to its value on entrance to | 195 // This restores |byteBuffer.position()| to its value on entrance to |
| 185 // this function. | 196 // this function. |
| 186 byteBuffer.get(lastDataReceivedAsBytes); | 197 byteBuffer.get(lastDataReceivedAsBytes); |
| 187 mResponseAsString += new String(lastDataReceivedAsBytes); | 198 mResponseAsString += new String(lastDataReceivedAsBytes); |
| 188 | 199 |
| 189 if (maybeThrowCancelOrPause(request)) { | 200 if (maybeThrowCancelOrPause(request)) { |
| 190 return; | 201 return; |
| 191 } | 202 } |
| 192 startNextRead(request); | 203 startNextRead(request); |
| 193 } | 204 } |
| 194 | 205 |
| 195 @Override | 206 @Override |
| 196 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { | 207 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { |
| 197 assertEquals(mExecutorThread, Thread.currentThread()); | 208 if (!mAllowDirectExecutor) { |
| 209 assertEquals(mExecutorThread, Thread.currentThread()); | |
| 210 } | |
| 198 assertTrue(request.isDone()); | 211 assertTrue(request.isDone()); |
| 199 assertTrue(mResponseStep == ResponseStep.ON_RESPONSE_STARTED | 212 assertTrue(mResponseStep == ResponseStep.ON_RESPONSE_STARTED |
| 200 || mResponseStep == ResponseStep.ON_READ_COMPLETED); | 213 || mResponseStep == ResponseStep.ON_READ_COMPLETED); |
| 201 assertFalse(mOnErrorCalled); | 214 assertFalse(mOnErrorCalled); |
| 202 assertFalse(mOnCanceledCalled); | 215 assertFalse(mOnCanceledCalled); |
| 203 assertNull(mError); | 216 assertNull(mError); |
| 204 | 217 |
| 205 mResponseStep = ResponseStep.ON_SUCCEEDED; | 218 mResponseStep = ResponseStep.ON_SUCCEEDED; |
| 206 mResponseInfo = info; | 219 mResponseInfo = info; |
| 207 openDone(); | 220 openDone(); |
| 208 maybeThrowCancelOrPause(request); | 221 maybeThrowCancelOrPause(request); |
| 209 } | 222 } |
| 210 | 223 |
| 211 @Override | 224 @Override |
| 212 public void onFailed(UrlRequest request, UrlResponseInfo info, UrlRequestExc eption error) { | 225 public void onFailed(UrlRequest request, UrlResponseInfo info, UrlRequestExc eption error) { |
| 213 assertEquals(mExecutorThread, Thread.currentThread()); | 226 // If the failure is because of prohibited direct execution, the test sh ouldn't fail |
| 227 // since the request already did. | |
| 228 if (!mAllowDirectExecutor | |
| 229 && !(error.getCause() instanceof InlineExecutionProhibitedExcept ion)) { | |
| 230 assertEquals(mExecutorThread, Thread.currentThread()); | |
| 231 } | |
| 214 assertTrue(request.isDone()); | 232 assertTrue(request.isDone()); |
| 215 // Shouldn't happen after success. | 233 // Shouldn't happen after success. |
| 216 assertTrue(mResponseStep != ResponseStep.ON_SUCCEEDED); | 234 assertTrue(mResponseStep != ResponseStep.ON_SUCCEEDED); |
| 217 // Should happen at most once for a single request. | 235 // Should happen at most once for a single request. |
| 218 assertFalse(mOnErrorCalled); | 236 assertFalse(mOnErrorCalled); |
| 219 assertFalse(mOnCanceledCalled); | 237 assertFalse(mOnCanceledCalled); |
| 220 assertNull(mError); | 238 assertNull(mError); |
| 221 if (mFailureType == FailureType.THROW_SYNC) { | 239 if (mFailureType == FailureType.THROW_SYNC) { |
| 222 assertEquals(UrlRequestError.LISTENER_EXCEPTION_THROWN, error.getErr orCode()); | 240 assertEquals(UrlRequestError.LISTENER_EXCEPTION_THROWN, error.getErr orCode()); |
| 223 assertEquals(0, error.getCronetInternalErrorCode()); | 241 assertEquals(0, error.getCronetInternalErrorCode()); |
| 224 assertEquals("Exception received from UrlRequest.Callback", error.ge tMessage()); | 242 assertEquals("Exception received from UrlRequest.Callback", error.ge tMessage()); |
| 225 assertNotNull(error.getCause()); | 243 assertNotNull(error.getCause()); |
| 226 assertTrue(error.getCause() instanceof IllegalStateException); | 244 assertTrue(error.getCause() instanceof IllegalStateException); |
| 227 assertEquals("Listener Exception.", error.getCause().getMessage()); | 245 assertEquals("Listener Exception.", error.getCause().getMessage()); |
| 228 assertFalse(error.immediatelyRetryable()); | 246 assertFalse(error.immediatelyRetryable()); |
| 229 } | 247 } |
| 230 | 248 |
| 231 mOnErrorCalled = true; | 249 mOnErrorCalled = true; |
| 232 mError = error; | 250 mError = error; |
| 233 openDone(); | 251 openDone(); |
| 252 System.out.println("opened"); | |
|
mef
2016/08/30 17:55:47
nit: remove
Charles
2016/08/30 20:11:06
Done.
| |
| 234 maybeThrowCancelOrPause(request); | 253 maybeThrowCancelOrPause(request); |
| 235 } | 254 } |
| 236 | 255 |
| 237 @Override | 256 @Override |
| 238 public void onCanceled(UrlRequest request, UrlResponseInfo info) { | 257 public void onCanceled(UrlRequest request, UrlResponseInfo info) { |
| 239 assertEquals(mExecutorThread, Thread.currentThread()); | 258 if (!mAllowDirectExecutor) { |
| 259 assertEquals(mExecutorThread, Thread.currentThread()); | |
| 260 } | |
| 240 assertTrue(request.isDone()); | 261 assertTrue(request.isDone()); |
| 241 // Should happen at most once for a single request. | 262 // Should happen at most once for a single request. |
| 242 assertFalse(mOnCanceledCalled); | 263 assertFalse(mOnCanceledCalled); |
| 243 assertFalse(mOnErrorCalled); | 264 assertFalse(mOnErrorCalled); |
| 244 assertNull(mError); | 265 assertNull(mError); |
| 245 | 266 |
| 246 mOnCanceledCalled = true; | 267 mOnCanceledCalled = true; |
| 247 openDone(); | 268 openDone(); |
| 248 maybeThrowCancelOrPause(request); | 269 maybeThrowCancelOrPause(request); |
| 249 } | 270 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 }; | 312 }; |
| 292 if (mFailureType == FailureType.CANCEL_ASYNC | 313 if (mFailureType == FailureType.CANCEL_ASYNC |
| 293 || mFailureType == FailureType.CANCEL_ASYNC_WITHOUT_PAUSE) { | 314 || mFailureType == FailureType.CANCEL_ASYNC_WITHOUT_PAUSE) { |
| 294 getExecutor().execute(task); | 315 getExecutor().execute(task); |
| 295 } else { | 316 } else { |
| 296 task.run(); | 317 task.run(); |
| 297 } | 318 } |
| 298 return mFailureType != FailureType.CANCEL_ASYNC_WITHOUT_PAUSE; | 319 return mFailureType != FailureType.CANCEL_ASYNC_WITHOUT_PAUSE; |
| 299 } | 320 } |
| 300 } | 321 } |
| OLD | NEW |