Swallow exceptions in TestContentProvider when writing to a pipe fails

The existing code results in flaky tests, where sometimes the write
fails (with "EPIPE (broken pipe)") and the exception propagates out
and causes the test to never complete and time out.

Swallowing the exception resolves this flakiness.

#minor-release

PiperOrigin-RevId: 372909415
This commit is contained in:
ibaker 2021-05-10 13:48:26 +01:00 committed by Oliver Woodman
parent 5517fd7936
commit 7a3d8b5ef0

View File

@ -23,6 +23,8 @@ import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.system.ErrnoException;
import android.system.OsConstants;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.TestUtil;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -110,12 +112,17 @@ public final class TestContentProvider extends ContentProvider
String mimeType, String mimeType,
@Nullable Bundle opts, @Nullable Bundle opts,
@Nullable Object args) { @Nullable Object args) {
try { try (FileOutputStream outputStream = new FileOutputStream(output.getFileDescriptor())) {
byte[] data = TestUtil.getByteArray(getContext(), getFileName(uri)); byte[] data = TestUtil.getByteArray(getContext(), getFileName(uri));
FileOutputStream outputStream = new FileOutputStream(output.getFileDescriptor());
outputStream.write(data); outputStream.write(data);
outputStream.close();
} catch (IOException e) { } catch (IOException e) {
if (e.getCause() instanceof ErrnoException
&& ((ErrnoException) e.getCause()).errno == OsConstants.EPIPE) {
// Swallow the exception if it's caused by a broken pipe - this indicates the reader has
// closed the pipe and is therefore no longer interested in the data being written.
// [See internal b/186728171].
return;
}
throw new RuntimeException("Error writing to pipe", e); throw new RuntimeException("Error writing to pipe", e);
} }
} }