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

Commit b702cb84 authored by Ang Li's avatar Ang Li
Browse files

Add test for DeadObjectException when toggling logcat

Adds a new unit test to verify that when a `DeadObjectException` is thrown by a client during a call to `toggleLogcat`, the exception is caught, the failure is logged, and no `RuntimeException` is propagated.

This ensures the service remains stable even if a registered client process dies unexpectedly.

Please help fill out the survey for feedback: https://forms.gle/Qz9q5boo4h4tESyd7

Original Change: ag/35136128
Bug: 431235865
Flag: TEST_ONLY

Change-Id: I484a264647ad8ce3ec40c3cf724333ec56e37940
parent cd66423b
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import static org.mockito.Mockito.never;
import static java.io.File.createTempFile;
import static java.nio.file.Files.createTempDirectory;

import android.os.DeadObjectException;
import android.os.IBinder;
import android.os.RemoteException;
import android.platform.test.annotations.Presubmit;
@@ -338,4 +339,32 @@ public class ProtoLogConfigurationServiceTest {
        Mockito.verify(mMockClient).toggleLogcat(eq(true),
                Mockito.argThat(it -> it.length == 1 && it[0].equals(TEST_GROUP)));
    }

    @Test
    public void doesNotThrowWhenClientDiesDuringToggle() throws RemoteException {
        // Verifies that a DeadObjectException is caught and handled gracefully.
        final var service = new ProtoLogConfigurationServiceImpl();
        final var mockPrintWriter = Mockito.mock(PrintWriter.class);

        // Register a client.
        final RegisterClientArgs args = new RegisterClientArgs();
        args.groups = new String[] { TEST_GROUP };
        args.groupsDefaultLogcatStatus = new boolean[] { false };
        service.registerClient(mMockClient, args);

        // Configure the mock client to throw DeadObjectException when toggleLogcat is called,
        // simulating a client process that has died.
        Mockito.doThrow(new DeadObjectException("Client died"))
                .when(mMockClient).toggleLogcat(anyBoolean(), any());

        // Call the method under test. This should not throw an exception.
        service.enableProtoLogToLogcat(mockPrintWriter, TEST_GROUP);

        // Verify that the service attempted to call the client.
        Mockito.verify(mMockClient).toggleLogcat(eq(true),
                Mockito.argThat(it -> it.length == 1 && it[0].equals(TEST_GROUP)));

        // Verify that the failure was reported to the PrintWriter.
        Mockito.verify(mockPrintWriter).println("- Failed (client may have died)");
    }
}