-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix CacheWriter to handle potential DataSink write failures
- If DataSource.close fails then it's unknown whether the underlying file was written to the cache. We should assume that it has not been. - Always re-query cachedBytes at the start of CacheWriter.cache, since its current value may be incorrect if a previous failure was the result of a file not being written to the cache. PiperOrigin-RevId: 359039109
- Loading branch information
Showing
5 changed files
with
237 additions
and
45 deletions.
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
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
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
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
96 changes: 96 additions & 0 deletions
96
testutils/src/main/java/com/google/android/exoplayer2/testutil/FailOnCloseDataSink.java
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,96 @@ | ||
/* | ||
* Copyright 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. | ||
*/ | ||
package com.google.android.exoplayer2.testutil; | ||
|
||
import com.google.android.exoplayer2.C; | ||
import com.google.android.exoplayer2.upstream.DataSink; | ||
import com.google.android.exoplayer2.upstream.DataSpec; | ||
import com.google.android.exoplayer2.upstream.cache.Cache; | ||
import com.google.android.exoplayer2.upstream.cache.CacheDataSink; | ||
import java.io.IOException; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* A {@link DataSink} that can simulate caching the bytes being written to it, and then failing to | ||
* persist them when {@link #close()} is called. | ||
*/ | ||
public final class FailOnCloseDataSink implements DataSink { | ||
|
||
/** Factory to create a {@link FailOnCloseDataSink}. */ | ||
public static final class Factory implements DataSink.Factory { | ||
|
||
private final Cache cache; | ||
private final AtomicBoolean failOnClose; | ||
|
||
/** | ||
* Creates an instance. | ||
* | ||
* @param cache The cache to write to when not in fail-on-close mode. | ||
* @param failOnClose An {@link AtomicBoolean} whose value is read in each call to {@link #open} | ||
* to determine whether to enable fail-on-close for the read that's being started. | ||
*/ | ||
public Factory(Cache cache, AtomicBoolean failOnClose) { | ||
this.cache = cache; | ||
this.failOnClose = failOnClose; | ||
} | ||
|
||
@Override | ||
public DataSink createDataSink() { | ||
return new FailOnCloseDataSink(cache, failOnClose); | ||
} | ||
} | ||
|
||
private final CacheDataSink wrappedSink; | ||
private final AtomicBoolean failOnClose; | ||
private boolean currentReadFailOnClose; | ||
|
||
/** | ||
* Creates an instance. | ||
* | ||
* @param cache The cache to write to when not in fail-on-close mode. | ||
* @param failOnClose An {@link AtomicBoolean} whose value is read in each call to {@link #open} | ||
* to determine whether to enable fail-on-close for the read that's being started. | ||
*/ | ||
public FailOnCloseDataSink(Cache cache, AtomicBoolean failOnClose) { | ||
this.wrappedSink = new CacheDataSink(cache, /* fragmentSize= */ C.LENGTH_UNSET); | ||
this.failOnClose = failOnClose; | ||
} | ||
|
||
@Override | ||
public void open(DataSpec dataSpec) throws IOException { | ||
currentReadFailOnClose = failOnClose.get(); | ||
if (currentReadFailOnClose) { | ||
return; | ||
} | ||
wrappedSink.open(dataSpec); | ||
} | ||
|
||
@Override | ||
public void write(byte[] buffer, int offset, int length) throws IOException { | ||
if (currentReadFailOnClose) { | ||
return; | ||
} | ||
wrappedSink.write(buffer, offset, length); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
if (currentReadFailOnClose) { | ||
throw new IOException("Fail on close"); | ||
} | ||
wrappedSink.close(); | ||
} | ||
} |