mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Update StatsDataSource.lastOpenedUri & responseHeader in finally
This ensures these values are still updated even if the delegate `DataSource.open()` throws an exception (e.g. an HTTP 404). PiperOrigin-RevId: 685687810
This commit is contained in:
parent
37cd008c01
commit
4a40fa6451
2 changed files with 75 additions and 4 deletions
|
|
@ -83,10 +83,18 @@ public final class StatsDataSource implements DataSource {
|
||||||
// Reassign defaults in case dataSource.open throws an exception.
|
// Reassign defaults in case dataSource.open throws an exception.
|
||||||
lastOpenedUri = dataSpec.uri;
|
lastOpenedUri = dataSpec.uri;
|
||||||
lastResponseHeaders = Collections.emptyMap();
|
lastResponseHeaders = Collections.emptyMap();
|
||||||
long availableBytes = dataSource.open(dataSpec);
|
try {
|
||||||
lastOpenedUri = Assertions.checkNotNull(getUri());
|
return dataSource.open(dataSpec);
|
||||||
lastResponseHeaders = getResponseHeaders();
|
} finally {
|
||||||
return availableBytes;
|
// TODO: b/373321956 - Remove this null-tolerance when we've fixed all DataSource
|
||||||
|
// implementations to return a non-null URI after a failed open() call and before close()
|
||||||
|
// (and updated the DataSourceContractTest to enforce this).
|
||||||
|
Uri upstreamUri = getUri();
|
||||||
|
if (upstreamUri != null) {
|
||||||
|
lastOpenedUri = upstreamUri;
|
||||||
|
}
|
||||||
|
lastResponseHeaders = getResponseHeaders();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2024 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;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
import static org.junit.Assert.assertThrows;
|
||||||
|
|
||||||
|
import android.net.Uri;
|
||||||
|
import androidx.media3.test.utils.FakeDataSet;
|
||||||
|
import androidx.media3.test.utils.FakeDataSource;
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
|
import java.io.IOException;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
public final class StatsDataSourceTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getLastOpenedUri_openSucceeds_returnsRedirectedUriAfterClosure() throws Exception {
|
||||||
|
Uri redirectedUri = Uri.parse("bar");
|
||||||
|
FakeDataSet fakeDataSet = new FakeDataSet();
|
||||||
|
fakeDataSet.setRandomData(redirectedUri, /* length= */ 10);
|
||||||
|
StatsDataSource statsDataSource =
|
||||||
|
new StatsDataSource(
|
||||||
|
new ResolvingDataSource(
|
||||||
|
new FakeDataSource(fakeDataSet),
|
||||||
|
dataSpec -> dataSpec.buildUpon().setUri(redirectedUri).build()));
|
||||||
|
|
||||||
|
statsDataSource.open(new DataSpec(Uri.parse("foo")));
|
||||||
|
statsDataSource.close();
|
||||||
|
|
||||||
|
assertThat(statsDataSource.getLastOpenedUri()).isEqualTo(redirectedUri);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getLastOpenedUri_openFails_returnsRedirectedUriAfterClosure() throws Exception {
|
||||||
|
Uri redirectedUri = Uri.parse("bar");
|
||||||
|
StatsDataSource statsDataSource =
|
||||||
|
new StatsDataSource(
|
||||||
|
new ResolvingDataSource(
|
||||||
|
new FakeDataSource(),
|
||||||
|
dataSpec -> dataSpec.buildUpon().setUri(redirectedUri).build()));
|
||||||
|
|
||||||
|
assertThrows(IOException.class, () -> statsDataSource.open(new DataSpec(Uri.parse("foo"))));
|
||||||
|
statsDataSource.close();
|
||||||
|
|
||||||
|
assertThat(statsDataSource.getLastOpenedUri()).isEqualTo(redirectedUri);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue