-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Workaround OkHttp lack of clean interrupt support #71
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
libraries/datasource_okhttp/src/androidTest/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- Copyright (C) 2021 The Android Open Source Project | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="androidx.media3.datasource.okhttp.test"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET"/> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> | ||
|
||
<application android:usesCleartextTraffic="true" /> | ||
|
||
</manifest> |
145 changes: 145 additions & 0 deletions
145
...rc/androidTest/java/androidx/media3/datasource/okhttp/OkHttpDataSourceCancellationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright (C) 2020 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package androidx.media3.datasource.okhttp | ||
|
||
import androidx.media3.datasource.DataSource | ||
import androidx.media3.datasource.DataSpec | ||
import androidx.media3.datasource.HttpDataSource.HttpDataSourceException | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.google.common.truth.Truth.assertThat | ||
import okhttp3.Call | ||
import okhttp3.OkHttpClient | ||
import org.junit.After | ||
import org.junit.Assert.fail | ||
import org.junit.Assume.assumeTrue | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import java.io.Closeable | ||
import java.net.InetAddress | ||
import java.net.UnknownHostException | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.logging.Handler | ||
import java.util.logging.LogRecord | ||
|
||
/** [DataSource] contract tests for [OkHttpDataSource]. */ | ||
@RunWith(AndroidJUnit4::class) | ||
class OkHttpDataSourceCancellationTest { | ||
val logs: MutableList<String> = mutableListOf() | ||
|
||
val okHttpClient: Call.Factory = OkHttpClient.Builder() | ||
.build() | ||
|
||
private var logging: Closeable? = null | ||
|
||
@After | ||
fun disableLogging() { | ||
logging?.close() | ||
} | ||
|
||
fun createDataSource(): DataSource { | ||
return OkHttpDataSource.Factory(okHttpClient).createDataSource() | ||
} | ||
|
||
@Test | ||
fun handlesFastCancellations() { | ||
assumeInternet() | ||
|
||
val interruptPoint = CountDownLatch(1) | ||
val interruptedPoint = CountDownLatch(1) | ||
|
||
logging = OkHttpDebugLogging.enableHttp2(handler = TestLogHandler { message -> | ||
if (message.matches(">>.*HEADERS".toRegex())) { | ||
interruptPoint.countDown() | ||
interruptedPoint.awaitUninterruptible() | ||
} | ||
}) | ||
|
||
val testThread = Thread.currentThread() | ||
|
||
Thread { | ||
interruptPoint.await() | ||
|
||
testThread.interrupt() | ||
|
||
interruptedPoint.countDown() | ||
}.start() | ||
|
||
val dataSource = createDataSource() | ||
val content = DataSpec.Builder() | ||
.setUri("https://storage.googleapis.com/exoplayer-test-media-1/gen-3/screens/dash-vod-single-segment/video-avc-baseline-480.mp4") | ||
.build() | ||
|
||
try { | ||
dataSource.open(content) | ||
fail() | ||
} catch (hdse: HttpDataSourceException) { | ||
// expected | ||
} | ||
|
||
// logs.forEach { | ||
// println("'$it'") | ||
// } | ||
|
||
// Check we started a request | ||
assertThat(logs).contains(">> 0x00000003 102 HEADERS END_STREAM|END_HEADERS") | ||
|
||
// If execute is used in OkHttpDataSource.open() then this RST_STREAM is not sent. | ||
assertThat(logs).contains(">> 0x00000003 4 RST_STREAM ") | ||
} | ||
|
||
private fun assumeInternet() { | ||
try { | ||
InetAddress.getByName("www.google.com") | ||
} catch (uhe: UnknownHostException) { | ||
assumeTrue("requires network", false) | ||
} | ||
} | ||
|
||
inner class TestLogHandler(val onMessage: (message: String) -> Unit) : Handler() { | ||
override fun publish(log: LogRecord) { | ||
val message = log.message | ||
|
||
logs.add(message) | ||
|
||
onMessage(message) | ||
} | ||
|
||
override fun flush() { | ||
} | ||
|
||
override fun close() { | ||
} | ||
} | ||
} | ||
|
||
private fun CountDownLatch.awaitUninterruptible() { | ||
var interrupted = false | ||
|
||
try { | ||
while (true) { | ||
try { | ||
await() | ||
return | ||
} catch (ie: InterruptedException) { | ||
interrupted = true | ||
} | ||
} | ||
} finally { | ||
if (interrupted) { | ||
Thread.currentThread().interrupt() | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...tp/src/androidTest/java/androidx/media3/datasource/okhttp/OkHttpDataSourceContractTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) 2020 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package androidx.media3.datasource.okhttp | ||
|
||
import android.net.Uri | ||
import androidx.media3.datasource.DataSource | ||
import androidx.media3.test.utils.DataSourceContractTest | ||
import androidx.media3.test.utils.HttpDataSourceTestEnv | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.google.common.collect.ImmutableList | ||
import okhttp3.Call | ||
import okhttp3.OkHttpClient | ||
import org.junit.Rule | ||
import org.junit.runner.RunWith | ||
|
||
/** [DataSource] contract tests for [OkHttpDataSource]. */ | ||
@RunWith(AndroidJUnit4::class) | ||
class OkHttpDataSourceContractTest : DataSourceContractTest() { | ||
@get:Rule | ||
var httpDataSourceTestEnv = HttpDataSourceTestEnv() | ||
|
||
override fun createDataSource(): DataSource { | ||
val okHttpClient: Call.Factory = OkHttpClient.Builder() | ||
.build() | ||
|
||
return OkHttpDataSource.Factory(okHttpClient).createDataSource() | ||
} | ||
|
||
override fun getTestResources(): ImmutableList<TestResource> { | ||
return httpDataSourceTestEnv.servedResources | ||
} | ||
|
||
override fun getNotFoundUri(): Uri { | ||
return Uri.parse(httpDataSourceTestEnv.nonexistentUrl) | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...aries/datasource_okhttp/src/androidTest/java/androidx/media3/datasource/okhttp/logging.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (C) 2019 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package androidx.media3.datasource.okhttp | ||
|
||
import okhttp3.internal.concurrent.TaskRunner | ||
import okhttp3.internal.http2.Http2 | ||
import java.io.Closeable | ||
import java.util.concurrent.CopyOnWriteArraySet | ||
import java.util.logging.ConsoleHandler | ||
import java.util.logging.Handler | ||
import java.util.logging.Level | ||
import java.util.logging.LogRecord | ||
import java.util.logging.Logger | ||
import java.util.logging.SimpleFormatter | ||
import kotlin.reflect.KClass | ||
|
||
object OkHttpDebugLogging { | ||
// Keep references to loggers to prevent their configuration from being GC'd. | ||
private val configuredLoggers = CopyOnWriteArraySet<Logger>() | ||
|
||
fun enableHttp2(handler: Handler = logHandler()) = enable(Http2::class, handler) | ||
|
||
fun enableTaskRunner(handler: Handler = logHandler()) = enable(TaskRunner::class, handler) | ||
|
||
fun logHandler() = ConsoleHandler().apply { | ||
level = Level.FINE | ||
formatter = object : SimpleFormatter() { | ||
override fun format(record: LogRecord) = | ||
String.format("[%1\$tF %1\$tT] %2\$s %n", record.millis, record.message) | ||
} | ||
} | ||
|
||
fun enable(loggerClass: String, handler: Handler = logHandler()): Closeable { | ||
val logger = Logger.getLogger(loggerClass) | ||
if (configuredLoggers.add(logger)) { | ||
logger.addHandler(handler) | ||
logger.level = Level.FINEST | ||
} | ||
return Closeable { | ||
logger.removeHandler(handler) | ||
} | ||
} | ||
|
||
fun enable(loggerClass: KClass<*>, handler: Handler = logHandler()) = | ||
enable(loggerClass.java.name, handler) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can delete this, since it's covered in normal tests.