Merge "Always cancel requests during tearDown" am: 42b5c0e197 am: 25dbbfbc49
Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/2430372 Change-Id: I84993985ae634fc52a41176931132d91a0f23720 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
commit
3ef39d2776
2 changed files with 37 additions and 6 deletions
|
|
@ -47,6 +47,7 @@ public class HttpEngineTest {
|
|||
|
||||
private HttpEngine.Builder mEngineBuilder;
|
||||
private TestUrlRequestCallback mCallback;
|
||||
private UrlRequest mRequest;
|
||||
private HttpEngine mEngine;
|
||||
|
||||
@Before
|
||||
|
|
@ -59,6 +60,10 @@ public class HttpEngineTest {
|
|||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
if (mRequest != null) {
|
||||
mRequest.cancel();
|
||||
mCallback.blockForDone();
|
||||
}
|
||||
if (mEngine != null) {
|
||||
mEngine.shutdown();
|
||||
}
|
||||
|
|
@ -73,9 +78,12 @@ public class HttpEngineTest {
|
|||
mEngine = mEngineBuilder.build();
|
||||
UrlRequest.Builder builder =
|
||||
mEngine.newUrlRequestBuilder(URL, mCallback, mCallback.getExecutor());
|
||||
builder.build().start();
|
||||
mRequest = builder.build();
|
||||
mRequest.start();
|
||||
|
||||
mCallback.expectCallback(ResponseStep.ON_SUCCEEDED);
|
||||
// This tests uses a non-hermetic server. Instead of asserting, assume the next callback.
|
||||
// This way, if the request were to fail, the test would just be skipped instead of failing.
|
||||
mCallback.assumeCallback(ResponseStep.ON_SUCCEEDED);
|
||||
UrlResponseInfo info = mCallback.mResponseInfo;
|
||||
assertOKStatusCode(info);
|
||||
assertEquals("h2", info.getNegotiatedProtocol());
|
||||
|
|
@ -86,9 +94,12 @@ public class HttpEngineTest {
|
|||
mEngine = mEngineBuilder.setEnableHttp2(false).build();
|
||||
UrlRequest.Builder builder =
|
||||
mEngine.newUrlRequestBuilder(URL, mCallback, mCallback.getExecutor());
|
||||
builder.build().start();
|
||||
mRequest = builder.build();
|
||||
mRequest.start();
|
||||
|
||||
mCallback.expectCallback(ResponseStep.ON_SUCCEEDED);
|
||||
// This tests uses a non-hermetic server. Instead of asserting, assume the next callback.
|
||||
// This way, if the request were to fail, the test would just be skipped instead of failing.
|
||||
mCallback.assumeCallback(ResponseStep.ON_SUCCEEDED);
|
||||
UrlResponseInfo info = mCallback.mResponseInfo;
|
||||
assertOKStatusCode(info);
|
||||
assertEquals("http/1.1", info.getNegotiatedProtocol());
|
||||
|
|
@ -104,9 +115,13 @@ public class HttpEngineTest {
|
|||
mCallback = new TestUrlRequestCallback();
|
||||
UrlRequest.Builder builder =
|
||||
mEngine.newUrlRequestBuilder(URL, mCallback, mCallback.getExecutor());
|
||||
builder.build().start();
|
||||
mRequest = builder.build();
|
||||
mRequest.start();
|
||||
|
||||
mCallback.expectCallback(ResponseStep.ON_SUCCEEDED);
|
||||
// This tests uses a non-hermetic server. Instead of asserting, assume the next
|
||||
// callback. This way, if the request were to fail, the test would just be skipped
|
||||
// instead of failing.
|
||||
mCallback.assumeCallback(ResponseStep.ON_SUCCEEDED);
|
||||
UrlResponseInfo info = mCallback.mResponseInfo;
|
||||
assumeOKStatusCode(info);
|
||||
quicWasUsed = isQuic(info.getNegotiatedProtocol());
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package android.net.http.cts.util;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
|
@ -23,6 +24,8 @@ import static org.junit.Assert.assertNull;
|
|||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.Assume.assumeThat;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
|
||||
import android.net.http.CallbackException;
|
||||
import android.net.http.HttpException;
|
||||
|
|
@ -231,6 +234,19 @@ public class TestUrlRequestCallback extends UrlRequest.Callback {
|
|||
assertSame(expectedStep, mResponseStep);
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for a terminal callback to complete execution before skipping the test if the
|
||||
* callback is not the expected one
|
||||
*
|
||||
* @param expectedStep the expected callback step
|
||||
*/
|
||||
public void assumeCallback(ResponseStep expectedStep) {
|
||||
if (isTerminalCallback(expectedStep)) {
|
||||
assumeTrue("Did not receive terminal callback before timeout", blockForDone());
|
||||
}
|
||||
assumeThat(expectedStep, equalTo(mResponseStep));
|
||||
}
|
||||
|
||||
/**
|
||||
* Blocks the calling thread until one of the final states has been called.
|
||||
* This is called before the callback has finished executed.
|
||||
|
|
|
|||
Loading…
Reference in a new issue