From 9b3014dd796adb80278dc6e022b67b66387759a2 Mon Sep 17 00:00:00 2001 From: tonihei Date: Tue, 26 Jan 2021 17:06:39 +0000 Subject: [PATCH] Remove randomness from adaptive bitrate tests. - The order of sample stream (and thus the order in which loads are triggered) currently depends on a Set and thus on the hash codes of the objects that change with every run. Changing to a List solves this problem. - The FakeAdaptiveDataSet directly created a static Random (with random seed) to compute the variation of chunk sizes. Changing this to an injected Random object that can always be initialized with the same seed also removed this randomness from the tests. PiperOrigin-RevId: 353878661 --- .../com/google/android/exoplayer2/ExoPlayerTest.java | 5 +++-- .../exoplayer2/testutil/FakeAdaptiveDataSet.java | 11 ++++++----- .../exoplayer2/testutil/FakeAdaptiveMediaPeriod.java | 8 ++++---- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/library/core/src/test/java/com/google/android/exoplayer2/ExoPlayerTest.java b/library/core/src/test/java/com/google/android/exoplayer2/ExoPlayerTest.java index d163e704a5..4077b8f5b8 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/ExoPlayerTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/ExoPlayerTest.java @@ -115,6 +115,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -4644,7 +4645,7 @@ public final class ExoPlayerTest { // Use chunked data to ensure the player actually needs to continue loading and playing. FakeAdaptiveDataSet.Factory dataSetFactory = new FakeAdaptiveDataSet.Factory( - /* chunkDurationUs= */ 500_000, /* bitratePercentStdDev= */ 10.0); + /* chunkDurationUs= */ 500_000, /* bitratePercentStdDev= */ 10.0, new Random(0)); MediaSource chunkedMediaSource = new FakeAdaptiveMediaSource( new FakeTimeline(), @@ -4769,7 +4770,7 @@ public final class ExoPlayerTest { // Use chunked data to ensure the player actually needs to continue loading and playing. FakeAdaptiveDataSet.Factory dataSetFactory = new FakeAdaptiveDataSet.Factory( - /* chunkDurationUs= */ 500_000, /* bitratePercentStdDev= */ 10.0); + /* chunkDurationUs= */ 500_000, /* bitratePercentStdDev= */ 10.0, new Random(0)); MediaSource chunkedMediaSource = new FakeAdaptiveMediaSource( new FakeTimeline(), diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveDataSet.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveDataSet.java index 9e9642c1cb..376d683267 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveDataSet.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveDataSet.java @@ -37,10 +37,9 @@ public final class FakeAdaptiveDataSet extends FakeDataSet { */ public static final class Factory { - private static final Random random = new Random(); - private final long chunkDurationUs; private final double bitratePercentStdDev; + private final Random random; /** * Set up factory for {@link FakeAdaptiveDataSet}s with a chunk duration and the standard @@ -50,10 +49,12 @@ public final class FakeAdaptiveDataSet extends FakeDataSet { * @param bitratePercentStdDev The standard deviation used to generate the chunk sizes centered * around the average bitrate of the {@link Format}s. The standard deviation is given in * percent (of the average size). + * @param random The random number generator used to generate the chunk size variation. */ - public Factory(long chunkDurationUs, double bitratePercentStdDev) { + public Factory(long chunkDurationUs, double bitratePercentStdDev, Random random) { this.chunkDurationUs = chunkDurationUs; this.bitratePercentStdDev = bitratePercentStdDev; + this.random = random; } /** @@ -63,8 +64,8 @@ public final class FakeAdaptiveDataSet extends FakeDataSet { * @param mediaDurationUs The total duration of the fake data set in microseconds. */ public FakeAdaptiveDataSet createDataSet(TrackGroup trackGroup, long mediaDurationUs) { - return new FakeAdaptiveDataSet(trackGroup, mediaDurationUs, chunkDurationUs, - bitratePercentStdDev, random); + return new FakeAdaptiveDataSet( + trackGroup, mediaDurationUs, chunkDurationUs, bitratePercentStdDev, random); } } diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveMediaPeriod.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveMediaPeriod.java index 9b39056bdb..8db69b3dc7 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveMediaPeriod.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveMediaPeriod.java @@ -42,9 +42,9 @@ import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.Util; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Sets; import java.io.IOException; -import java.util.Set; +import java.util.ArrayList; +import java.util.List; import org.checkerframework.checker.nullness.compatqual.NullableType; /** @@ -63,7 +63,7 @@ public class FakeAdaptiveMediaPeriod private final Allocator allocator; private final long durationUs; @Nullable private final TransferListener transferListener; - private final Set> sampleStreams; + private final List> sampleStreams; @Nullable private Callback callback; private boolean prepared; @@ -82,7 +82,7 @@ public class FakeAdaptiveMediaPeriod this.allocator = allocator; this.durationUs = durationUs; this.transferListener = transferListener; - sampleStreams = Sets.newIdentityHashSet(); + sampleStreams = new ArrayList<>(); sequenceableLoader = new CompositeSequenceableLoader(new SequenceableLoader[0]); fakePreparationLoadTaskId = LoadEventInfo.getNewId(); }