| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 | 8 |
| 9 import static junit.framework.Assert.assertEquals; | 9 import static junit.framework.Assert.assertEquals; |
| 10 import static junit.framework.Assert.assertFalse; | 10 import static junit.framework.Assert.assertFalse; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 | 62 |
| 63 // position() of ByteBuffer prior to read() call. | 63 // position() of ByteBuffer prior to read() call. |
| 64 private int mBufferPositionBeforeRead; | 64 private int mBufferPositionBeforeRead; |
| 65 | 65 |
| 66 // Data to write. | 66 // Data to write. |
| 67 private final ArrayList<WriteBuffer> mWriteBuffers = new ArrayList<WriteBuff
er>(); | 67 private final ArrayList<WriteBuffer> mWriteBuffers = new ArrayList<WriteBuff
er>(); |
| 68 | 68 |
| 69 // Buffers that we yet to receive the corresponding onWriteCompleted callbac
k. | 69 // Buffers that we yet to receive the corresponding onWriteCompleted callbac
k. |
| 70 private final ArrayList<WriteBuffer> mWriteBuffersToBeAcked = new ArrayList<
WriteBuffer>(); | 70 private final ArrayList<WriteBuffer> mWriteBuffersToBeAcked = new ArrayList<
WriteBuffer>(); |
| 71 | 71 |
| 72 // Whether to use a direct executor. |
| 73 private final boolean mUseDirectExecutor; |
| 74 private final DirectExecutor mDirectExecutor; |
| 75 |
| 72 private class ExecutorThreadFactory implements ThreadFactory { | 76 private class ExecutorThreadFactory implements ThreadFactory { |
| 73 public Thread newThread(Runnable r) { | 77 public Thread newThread(Runnable r) { |
| 74 mExecutorThread = new Thread(r); | 78 mExecutorThread = new Thread(r); |
| 75 return mExecutorThread; | 79 return mExecutorThread; |
| 76 } | 80 } |
| 77 } | 81 } |
| 78 | 82 |
| 79 private static class WriteBuffer { | 83 private static class WriteBuffer { |
| 80 final ByteBuffer mBuffer; | 84 final ByteBuffer mBuffer; |
| 81 final boolean mFlush; | 85 final boolean mFlush; |
| 82 public WriteBuffer(ByteBuffer buffer, boolean flush) { | 86 public WriteBuffer(ByteBuffer buffer, boolean flush) { |
| 83 mBuffer = buffer; | 87 mBuffer = buffer; |
| 84 mFlush = flush; | 88 mFlush = flush; |
| 85 } | 89 } |
| 86 } | 90 } |
| 87 | 91 |
| 92 private static class DirectExecutor implements Executor { |
| 93 @Override |
| 94 public void execute(Runnable task) { |
| 95 task.run(); |
| 96 } |
| 97 } |
| 88 public enum ResponseStep { | 98 public enum ResponseStep { |
| 89 NOTHING, | 99 NOTHING, |
| 90 ON_STREAM_READY, | 100 ON_STREAM_READY, |
| 91 ON_RESPONSE_STARTED, | 101 ON_RESPONSE_STARTED, |
| 92 ON_READ_COMPLETED, | 102 ON_READ_COMPLETED, |
| 93 ON_WRITE_COMPLETED, | 103 ON_WRITE_COMPLETED, |
| 94 ON_TRAILERS, | 104 ON_TRAILERS, |
| 95 ON_CANCELED, | 105 ON_CANCELED, |
| 96 ON_FAILED, | 106 ON_FAILED, |
| 97 ON_SUCCEEDED, | 107 ON_SUCCEEDED, |
| 98 } | 108 } |
| 99 | 109 |
| 100 public enum FailureType { | 110 public enum FailureType { |
| 101 NONE, | 111 NONE, |
| 102 CANCEL_SYNC, | 112 CANCEL_SYNC, |
| 103 CANCEL_ASYNC, | 113 CANCEL_ASYNC, |
| 104 // Same as above, but continues to advance the stream after posting | 114 // Same as above, but continues to advance the stream after posting |
| 105 // the cancellation task. | 115 // the cancellation task. |
| 106 CANCEL_ASYNC_WITHOUT_PAUSE, | 116 CANCEL_ASYNC_WITHOUT_PAUSE, |
| 107 THROW_SYNC | 117 THROW_SYNC |
| 108 } | 118 } |
| 109 | 119 |
| 120 public TestBidirectionalStreamCallback() { |
| 121 mUseDirectExecutor = false; |
| 122 mDirectExecutor = null; |
| 123 } |
| 124 |
| 125 public TestBidirectionalStreamCallback(boolean useDirectExecutor) { |
| 126 mUseDirectExecutor = useDirectExecutor; |
| 127 mDirectExecutor = new DirectExecutor(); |
| 128 } |
| 129 |
| 110 public void setAutoAdvance(boolean autoAdvance) { | 130 public void setAutoAdvance(boolean autoAdvance) { |
| 111 mAutoAdvance = autoAdvance; | 131 mAutoAdvance = autoAdvance; |
| 112 } | 132 } |
| 113 | 133 |
| 114 public void setFailure(FailureType failureType, ResponseStep failureStep) { | 134 public void setFailure(FailureType failureType, ResponseStep failureStep) { |
| 115 mFailureStep = failureStep; | 135 mFailureStep = failureStep; |
| 116 mFailureType = failureType; | 136 mFailureType = failureType; |
| 117 } | 137 } |
| 118 | 138 |
| 119 public void blockForDone() { | 139 public void blockForDone() { |
| 120 mDone.block(); | 140 mDone.block(); |
| 121 } | 141 } |
| 122 | 142 |
| 123 public void waitForNextReadStep() { | 143 public void waitForNextReadStep() { |
| 124 mReadStepBlock.block(); | 144 mReadStepBlock.block(); |
| 125 mReadStepBlock.close(); | 145 mReadStepBlock.close(); |
| 126 } | 146 } |
| 127 | 147 |
| 128 public void waitForNextWriteStep() { | 148 public void waitForNextWriteStep() { |
| 129 mWriteStepBlock.block(); | 149 mWriteStepBlock.block(); |
| 130 mWriteStepBlock.close(); | 150 mWriteStepBlock.close(); |
| 131 } | 151 } |
| 132 | 152 |
| 133 public Executor getExecutor() { | 153 public Executor getExecutor() { |
| 154 if (mUseDirectExecutor) { |
| 155 return mDirectExecutor; |
| 156 } |
| 134 return mExecutorService; | 157 return mExecutorService; |
| 135 } | 158 } |
| 136 | 159 |
| 137 public void shutdownExecutor() { | 160 public void shutdownExecutor() { |
| 161 if (mUseDirectExecutor) { |
| 162 throw new UnsupportedOperationException("DirectExecutor doesn't supp
ort shutdown"); |
| 163 } |
| 138 mExecutorService.shutdown(); | 164 mExecutorService.shutdown(); |
| 139 } | 165 } |
| 140 | 166 |
| 141 public void addWriteData(byte[] data) { | 167 public void addWriteData(byte[] data) { |
| 142 addWriteData(data, true); | 168 addWriteData(data, true); |
| 143 } | 169 } |
| 144 | 170 |
| 145 public void addWriteData(byte[] data, boolean flush) { | 171 public void addWriteData(byte[] data, boolean flush) { |
| 146 ByteBuffer writeBuffer = ByteBuffer.allocateDirect(data.length); | 172 ByteBuffer writeBuffer = ByteBuffer.allocateDirect(data.length); |
| 147 writeBuffer.put(data); | 173 writeBuffer.put(data); |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 }; | 393 }; |
| 368 if (mFailureType == FailureType.CANCEL_ASYNC | 394 if (mFailureType == FailureType.CANCEL_ASYNC |
| 369 || mFailureType == FailureType.CANCEL_ASYNC_WITHOUT_PAUSE) { | 395 || mFailureType == FailureType.CANCEL_ASYNC_WITHOUT_PAUSE) { |
| 370 getExecutor().execute(task); | 396 getExecutor().execute(task); |
| 371 } else { | 397 } else { |
| 372 task.run(); | 398 task.run(); |
| 373 } | 399 } |
| 374 return mFailureType != FailureType.CANCEL_ASYNC_WITHOUT_PAUSE; | 400 return mFailureType != FailureType.CANCEL_ASYNC_WITHOUT_PAUSE; |
| 375 } | 401 } |
| 376 } | 402 } |
| OLD | NEW |