HttpURLConnection: Add test coverage for concurrent disconnect.

HttpURLConnection is not thread safe, but some clients assume that
they can disconnect() the connection from another thread and that
this quickly unblocks the thread that is blocked reading. While
we don't guarantee that this will always work, we do make a best
effort.

Existing tests (com.squareup.okhttp.internal.http.DisconnectTest,
libcore.java.net.URLConnectionTest) only test disconnecting
from the main thread.

This CL adds coverage to test some common scenarios of the connection
being disconnected from a background thread while the main thread is
blocked
 (a) waiting to start reading the response headers,
 (b) waiting to start reading the response body,
 (c) part-way through reading the response body.

Each test case spawns a background thread that calls disconnect()
after 2 second; the test asserts that the reading thread encounters
the expected IOException after roughly that amount of time (between
1 and 4 seconds). To guard confirm that the test is set up correctly,
the test also checks what part of the response has been read by that
time.

We currently make no attempt to make it safe to also close() the
InputStream from a background thread, so this is not tested.

Fixes: 129771436
Test: atest CtsLibcoreTestCases:libcore.java.net.URLConnectionTest
Test: Checked that all three added tests pass reliably (ran 500x each)
      on a Pixel 2 device even when all time margins are 1/10th of what
      they are in this CL. At 1/20th the durations / margins, running
      the tests 500x gets quite flaky.

Change-Id: I1a848021b661b8675f57761cb30cbbe3da075aa7
2 files changed