| OLD | NEW | 
| (Empty) |  | 
 |    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 | 
 |    3 // found in the LICENSE file. | 
 |    4  | 
 |    5 package org.chromium.net; | 
 |    6  | 
 |    7 import android.os.ConditionVariable; | 
 |    8  | 
 |    9 import java.io.IOException; | 
 |   10 import java.nio.ByteBuffer; | 
 |   11 import java.util.List; | 
 |   12 import java.util.concurrent.Executor; | 
 |   13  | 
 |   14 /** | 
 |   15  * An UploadDataProvider that allows tests to invoke {@code onReadSucceeded} | 
 |   16  * and {@code onRewindSucceeded} on the UploadDataSink directly. | 
 |   17  * Chunked mode is not supported here, since the main interest is to test | 
 |   18  * different order of init/read/rewind calls. | 
 |   19  */ | 
 |   20 class TestDrivenDataProvider implements UploadDataProvider { | 
 |   21     private final Executor mExecutor; | 
 |   22     private final List<byte[]> mReads; | 
 |   23     private final ConditionVariable mWaitForReadRequest = | 
 |   24             new ConditionVariable(); | 
 |   25     private final ConditionVariable mWaitForRewindRequest = | 
 |   26             new ConditionVariable(); | 
 |   27     // Lock used to synchronize access to mReadPending and mRewindPending. | 
 |   28     private final Object mLock = new Object(); | 
 |   29  | 
 |   30     private int mNextRead = 0; | 
 |   31  | 
 |   32     // Only accessible when holding mLock. | 
 |   33  | 
 |   34     private boolean mReadPending = false; | 
 |   35     private boolean mRewindPending = false; | 
 |   36     private int mNumRewindCalls = 0; | 
 |   37     private int mNumReadCalls = 0; | 
 |   38  | 
 |   39     /** | 
 |   40      * Constructor. | 
 |   41      * @param Executor executor. Executor to run callbacks of UploadDataSink. | 
 |   42      * @param List<byte[]> reads. Results to be returned by successful read | 
 |   43      *            requests. Returned bytes must all fit within the read buffer | 
 |   44      *            provided by Cronet. After a rewind, if there is one, all reads | 
 |   45      *            will be repeated. | 
 |   46      */ | 
 |   47     TestDrivenDataProvider(Executor executor, List<byte[]> reads) { | 
 |   48         mExecutor = executor; | 
 |   49         mReads = reads; | 
 |   50     } | 
 |   51  | 
 |   52     // Called by UploadDataSink on the main thread. | 
 |   53     @Override | 
 |   54     public long getLength() { | 
 |   55         long length = 0; | 
 |   56         for (byte[] read : mReads) { | 
 |   57             length += read.length; | 
 |   58         } | 
 |   59         return length; | 
 |   60     } | 
 |   61  | 
 |   62     // Called by UploadDataSink on the executor thread. | 
 |   63     @Override | 
 |   64     public void read(final UploadDataSink uploadDataSink, | 
 |   65                      final ByteBuffer byteBuffer) throws IOException { | 
 |   66         synchronized (mLock) { | 
 |   67             ++mNumReadCalls; | 
 |   68             assertIdle(); | 
 |   69  | 
 |   70             mReadPending = true; | 
 |   71             if (mNextRead != mReads.size()) { | 
 |   72                 if ((byteBuffer.limit() - byteBuffer.position()) | 
 |   73                         < mReads.get(mNextRead).length) { | 
 |   74                     throw new IllegalStateException("Read buffer smaller than ex
     pected."); | 
 |   75                 } | 
 |   76                 byteBuffer.put(mReads.get(mNextRead)); | 
 |   77                 ++mNextRead; | 
 |   78             } else { | 
 |   79                 throw new IllegalStateException("Too many reads: " + mNextRead); | 
 |   80             } | 
 |   81             mWaitForReadRequest.open(); | 
 |   82         } | 
 |   83     } | 
 |   84  | 
 |   85     // Called by UploadDataSink on the executor thread. | 
 |   86     @Override | 
 |   87     public void rewind(final UploadDataSink uploadDataSink) throws IOException { | 
 |   88         synchronized (mLock) { | 
 |   89             ++mNumRewindCalls; | 
 |   90             assertIdle(); | 
 |   91  | 
 |   92             if (mNextRead == 0) { | 
 |   93                 // Should never try and rewind when rewinding does nothing. | 
 |   94                 throw new IllegalStateException( | 
 |   95                         "Unexpected rewind when already at beginning"); | 
 |   96             } | 
 |   97             mRewindPending = true; | 
 |   98             mNextRead = 0; | 
 |   99             mWaitForRewindRequest.open(); | 
 |  100         } | 
 |  101     } | 
 |  102  | 
 |  103     // Called by test fixture on the main thread. | 
 |  104     public void onReadSucceeded(final UploadDataSink uploadDataSink) { | 
 |  105         Runnable completeRunnable = new Runnable() { | 
 |  106             @Override | 
 |  107             public void run() { | 
 |  108                 synchronized (mLock) { | 
 |  109                     if (!mReadPending) { | 
 |  110                         throw new IllegalStateException("No read pending."); | 
 |  111                     } | 
 |  112                     mReadPending = false; | 
 |  113                     uploadDataSink.onReadSucceeded(false); | 
 |  114                 } | 
 |  115             } | 
 |  116         }; | 
 |  117         mExecutor.execute(completeRunnable); | 
 |  118     } | 
 |  119  | 
 |  120  | 
 |  121     // Called by test fixture on the main thread. | 
 |  122     public void onRewindSucceeded(final UploadDataSink uploadDataSink) { | 
 |  123         Runnable completeRunnable = new Runnable() { | 
 |  124             @Override | 
 |  125             public void run() { | 
 |  126                 synchronized (mLock) { | 
 |  127                     if (!mRewindPending) { | 
 |  128                         throw new IllegalStateException("No rewind pending."); | 
 |  129                     } | 
 |  130                     mRewindPending = false; | 
 |  131                     uploadDataSink.onRewindSucceeded(); | 
 |  132                 } | 
 |  133             } | 
 |  134         }; | 
 |  135         mExecutor.execute(completeRunnable); | 
 |  136     } | 
 |  137  | 
 |  138     // Called by test fixture on the main thread. | 
 |  139     public int getNumReadCalls() { | 
 |  140         synchronized (mLock) { | 
 |  141             return mNumReadCalls; | 
 |  142         } | 
 |  143     } | 
 |  144  | 
 |  145     // Called by test fixture on the main thread. | 
 |  146     public int getNumRewindCalls() { | 
 |  147         synchronized (mLock) { | 
 |  148             return mNumRewindCalls; | 
 |  149         } | 
 |  150     } | 
 |  151  | 
 |  152     // Called by test fixture on the main thread. | 
 |  153     public void waitForReadRequest() { | 
 |  154         mWaitForReadRequest.block(); | 
 |  155     } | 
 |  156  | 
 |  157     // Called by test fixture on the main thread. | 
 |  158     public void resetWaitForReadRequest() { | 
 |  159         mWaitForReadRequest.close(); | 
 |  160     } | 
 |  161  | 
 |  162     // Called by test fixture on the main thread. | 
 |  163     public void waitForRewindRequest() { | 
 |  164         mWaitForRewindRequest.block(); | 
 |  165     } | 
 |  166  | 
 |  167     // Called by test fixture on the main thread. | 
 |  168     public void assertReadNotPending() { | 
 |  169         synchronized (mLock) { | 
 |  170             if (mReadPending) { | 
 |  171                 throw new IllegalStateException("Read is pending."); | 
 |  172             } | 
 |  173         } | 
 |  174     } | 
 |  175  | 
 |  176     // Called by test fixture on the main thread. | 
 |  177     public void assertRewindNotPending() { | 
 |  178         synchronized (mLock) { | 
 |  179             if (mRewindPending) { | 
 |  180                 throw new IllegalStateException("Rewind is pending."); | 
 |  181             } | 
 |  182         } | 
 |  183     } | 
 |  184  | 
 |  185     /** | 
 |  186      * Helper method to ensure no read or rewind is in progress. | 
 |  187      */ | 
 |  188     private void assertIdle() { | 
 |  189         assertReadNotPending(); | 
 |  190         assertRewindNotPending(); | 
 |  191     } | 
 |  192 } | 
| OLD | NEW |