Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7f68eb72 authored by Sal Savage's avatar Sal Savage
Browse files

Check handler before trying to wait for looper

Problem: Tests that tried to put the state machine in a disconnected
state would run into a race condition where the the test logic was
racing to grab the looper object to wait on it before the class under
test cleaned up the handler via the normal state machine quit logic. If
the class under test cleaned up quickly, the test would fail with an NPE
when it made a getHandler() call.

Solution: Null check the handler. If its null, then we've got no waiting
to do. We can assert the state after to make sure we're in the state
we're supposed to be in.

Bug: 386715308
Flag: EXEMPT, test only change
Test: atest com.android.bluetooth.mapclient.MapClientStateMachineTest
Test: atest com.android.bluetooth.pbapclient.PbapClientServiceTest
Change-Id: Iaef6ec1c7eb6f0658c5470e105b9fd18bd4c2009
parent 6e6df06b
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -282,7 +282,6 @@ public class MapClientStateMachineTest {
    /** Test that default state is STATE_CONNECTING */
    @Test
    public void testDefaultConnectingState() {
        Log.i(TAG, "in testDefaultConnectingState");
        Assert.assertEquals(BluetoothProfile.STATE_CONNECTING, mMceStateMachine.getState());
    }

@@ -292,7 +291,6 @@ public class MapClientStateMachineTest {
     */
    @Test
    public void testStateTransitionFromConnectingToDisconnected() {
        Log.i(TAG, "in testStateTransitionFromConnectingToDisconnected");
        setupSdpRecordReceipt();
        Message msg = Message.obtain(mHandler, MceStateMachine.MSG_MAS_DISCONNECTED);
        mMceStateMachine.sendMessage(msg);
@@ -311,7 +309,6 @@ public class MapClientStateMachineTest {
    /** Test transition from STATE_CONNECTING --> (receive MSG_MAS_CONNECTED) --> STATE_CONNECTED */
    @Test
    public void testStateTransitionFromConnectingToConnected() {
        Log.i(TAG, "in testStateTransitionFromConnectingToConnected");
        setupSdpRecordReceipt();

        int expectedFromState = BluetoothProfile.STATE_CONNECTING;
@@ -326,7 +323,6 @@ public class MapClientStateMachineTest {
     */
    @Test
    public void testStateTransitionFromConnectedToDisconnected() {
        Log.i(TAG, "in testStateTransitionFromConnectedWithMasDisconnected");

        setupSdpRecordReceipt();
        // transition to the connected state
@@ -1176,7 +1172,10 @@ public class MapClientStateMachineTest {
    }

    private void assertCurrentStateAfterScheduledTask(int expectedState) {
        TestUtils.waitForLooperToFinishScheduledTask(mMceStateMachine.getHandler().getLooper());
        Handler handler = mMceStateMachine.getHandler();
        if (handler != null) {
            TestUtils.waitForLooperToFinishScheduledTask(handler.getLooper());
        }
        assertThat(mMceStateMachine.getState()).isEqualTo(expectedState);
    }

+3 −1
Original line number Diff line number Diff line
@@ -534,8 +534,10 @@ public class PbapClientServiceTest {
        // Clean up and wait for it to complete
        PbapClientStateMachine sm = mMockDeviceMap.get(mRemoteDevice);
        assertThat(sm).isNotNull();

        Looper looper = sm.getHandler().getLooper();
        sm.disconnect();
        TestUtils.waitForLooperToFinishScheduledTask(sm.getHandler().getLooper());
        TestUtils.waitForLooperToFinishScheduledTask(looper);
    }

    // connect (device null) -> false