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

Commit ad6eb1a2 authored by Riddle Hsu's avatar Riddle Hsu
Browse files

Reduce concurrent issues of WmTests

Wait for the async initialization of PerfettoProtoLogImpl to
avoid breaking mocking operation by concurrent access.

Also only initialize once to avoid leaking the threads created
by mBackgroundLoggingService.

Bug: 395656832
Flag: EXEMPT TEST_ONLY
Test: WmTests

Change-Id: Ib3387e9f03c2dd7e892e6cf5dea2c328575bdad4
parent 40aa32b3
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -79,6 +79,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@@ -864,5 +865,24 @@ public abstract class PerfettoProtoLogImpl extends IProtoLogClient.Stub implemen
            throw new RuntimeException("Both mMessageString and mMessageHash should never be null");
        }
    }

    /**
     * This is only used by unit tests to wait until {@link #connectToConfigurationService} is
     * done. Because unit tests are sensitive to concurrent accesses.
     */
    @VisibleForTesting
    public static void waitForInitialization() {
        final IProtoLog currentInstance = ProtoLog.getSingleInstance();
        if (!(currentInstance instanceof PerfettoProtoLogImpl protoLog)) {
            return;
        }
        try {
            protoLog.mBackgroundLoggingService.submit(() -> {
                Log.i(LOG_TAG, "Complete initialization");
            }).get();
        } catch (InterruptedException | ExecutionException e) {
            Log.e(LOG_TAG, "Failed to wait for tracing service", e);
        }
    }
}
+5 −1
Original line number Diff line number Diff line
@@ -78,6 +78,7 @@ import android.view.SurfaceControl;

import com.android.dx.mockito.inline.extended.StaticMockitoSession;
import com.android.internal.os.BackgroundThread;
import com.android.internal.protolog.PerfettoProtoLogImpl;
import com.android.internal.protolog.ProtoLog;
import com.android.internal.protolog.WmProtoLogGroups;
import com.android.server.AnimationThread;
@@ -187,7 +188,10 @@ public class SystemServicesTestRule implements TestRule {
    }

    private void setUp() {
        if (ProtoLog.getSingleInstance() == null) {
            ProtoLog.init(WmProtoLogGroups.values());
            PerfettoProtoLogImpl.waitForInitialization();
        }

        if (mOnBeforeServicesCreated != null) {
            mOnBeforeServicesCreated.run();