Skip to content

Commit 6c74e5c

Browse files
marcbaechingercopybara-github
authored andcommitted
Unflake MediaControllerListenerTest and MediaBrowserListenerTest
Addressing flakiness of `recursiveChangesFromListeners_reportConsistentValuesForAllListeners` and `onEvents_whenNewCommandIsCalledInsideOnEvents_isCalledFromNewLooperIterationSet` The test did not have any way to avoid that events are added to the list to assert after the count down latch was counted down. This created either failing asserts or a `ConcurrentModificationException`. Making sure to not produce further events or to not add further events to the data structure that is later used for asserting fixes the problem. PiperOrigin-RevId: 786257610
1 parent 0b17a67 commit 6c74e5c

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

libraries/test_session_current/src/androidTest/java/androidx/media3/session/MediaControllerListenerTest.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3931,9 +3931,14 @@ public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
39313931

39323932
@Override
39333933
public void onEvents(Player player, Player.Events events) {
3934-
// onEvents is called twice.
3935-
eventsList.add(events);
3936-
controller.setShuffleModeEnabled(true);
3934+
if (latch.getCount() > 0) {
3935+
eventsList.add(events);
3936+
if (events.contains(Player.EVENT_REPEAT_MODE_CHANGED)) {
3937+
// This event is expected to be delivered in the next listener iteration processed
3938+
// in a following Looper task.
3939+
controller.setShuffleModeEnabled(true);
3940+
}
3941+
}
39373942
latch.countDown();
39383943
}
39393944
};
@@ -4111,9 +4116,12 @@ public void recursiveChangesFromListeners_reportConsistentValuesForAllListeners(
41114116
new Player.Listener() {
41124117
@Override
41134118
public void onPlaybackStateChanged(@Player.State int playbackState) {
4114-
listener1States.add(playbackState);
4115-
if (playbackState == Player.STATE_READY) {
4116-
controller.stop();
4119+
if (latch.getCount() > 0) {
4120+
// avoid trailing events making the test flaky
4121+
listener1States.add(playbackState);
4122+
if (playbackState == Player.STATE_READY) {
4123+
controller.stop();
4124+
}
41174125
}
41184126
latch.countDown();
41194127
}
@@ -4122,7 +4130,10 @@ public void onPlaybackStateChanged(@Player.State int playbackState) {
41224130
new Player.Listener() {
41234131
@Override
41244132
public void onPlaybackStateChanged(@Player.State int playbackState) {
4125-
listener2States.add(playbackState);
4133+
if (latch.getCount() > 0) {
4134+
// avoid trailing events making the test flaky
4135+
listener2States.add(playbackState);
4136+
}
41264137
latch.countDown();
41274138
}
41284139
};

0 commit comments

Comments
 (0)