Пример #1
0
 /**
  * Consumes a portion of the response.
  *
  * @param byteBuffer The ByteBuffer to append. Must be a direct buffer, and no references to it
  *     may be retained after the method ends, as it wraps code managed on the native heap.
  */
 @CalledByNative
 protected void onBytesRead(ByteBuffer byteBuffer) {
   try {
     while (byteBuffer.hasRemaining()) {
       mSink.write(byteBuffer);
     }
   } catch (IOException e) {
     mSinkException = e;
     cancel();
   }
 }
  @SmallTest
  @Feature({"Cronet"})
  public void testNoErrorWhenCanceledDuringStart() throws Exception {
    TestUrlRequestCallback callback = new TestUrlRequestCallback();
    UrlRequest.Builder builder =
        new UrlRequest.Builder(
            NativeTestServer.getEchoBodyURL(),
            callback,
            callback.getExecutor(),
            mTestFramework.mCronetEngine);
    final ConditionVariable first = new ConditionVariable();
    final ConditionVariable second = new ConditionVariable();
    builder.addHeader("Content-Type", "useless/string");
    builder.setUploadDataProvider(
        new UploadDataProvider() {
          @Override
          public long getLength() throws IOException {
            first.open();
            second.block();
            return 0;
          }

          @Override
          public void read(UploadDataSink uploadDataSink, ByteBuffer byteBuffer)
              throws IOException {}

          @Override
          public void rewind(UploadDataSink uploadDataSink) throws IOException {}
        },
        callback.getExecutor());
    UrlRequest urlRequest = builder.build();
    urlRequest.start();
    first.block();
    urlRequest.cancel();
    second.open();
    callback.blockForDone();
    assertTrue(callback.mOnCanceledCalled);
  }
 @SmallTest
 @Feature({"Cronet"})
 public void testShutdownAfterCancel() throws Exception {
   mActivity = launchCronetTestApp();
   TestUrlRequestListener listener = new TestUrlRequestListener();
   // Block listener when response starts to verify that shutdown fails
   // if there are active requests.
   listener.setAutoAdvance(false);
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEngine);
   UrlRequest urlRequest = urlRequestBuilder.build();
   urlRequest.start();
   try {
     mActivity.mCronetEngine.shutdown();
     fail("Should throw an exception");
   } catch (Exception e) {
     assertEquals("Cannot shutdown with active requests.", e.getMessage());
   }
   listener.waitForNextStep();
   assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep);
   urlRequest.cancel();
   mActivity.mCronetEngine.shutdown();
 }