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

Commit 3228b880 authored by Pablo Gamito's avatar Pablo Gamito
Browse files

Update tests

We now throw when we cannot get the log message for a protolog

Flag: TEST_ONLY
Test: atest TracingTests
Change-Id: Ib0308c8da07aba0f6c1f54fef0fe31eff689ddec
parent ab70dbe7
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ android_test {
        "apct-perftests-utils",
        "collector-device-lib",
        "platform-test-annotations",
        "perfetto_trace_java_protos",
    ],
    test_suites: [
        "device-tests",
+32 −5
Original line number Diff line number Diff line
@@ -17,10 +17,12 @@ package com.android.internal.protolog;

import android.app.Activity;
import android.os.Bundle;
import android.os.ServiceManager.ServiceNotFoundException;
import android.perftests.utils.Stats;

import androidx.test.InstrumentationRegistry;

import com.android.internal.protolog.common.IProtoLog;
import com.android.internal.protolog.common.IProtoLogGroup;
import com.android.internal.protolog.common.LogLevel;

@@ -31,6 +33,8 @@ import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import perfetto.protos.ProtologCommon;

import java.util.ArrayList;
import java.util.Collection;

@@ -65,24 +69,48 @@ public class ProtoLogPerfTest {
        };
    }

    private IProtoLog mProcessedProtoLogger;
    private static final String MOCK_TEST_FILE_PATH = "mock/file/path";
    private static final perfetto.protos.Protolog.ProtoLogViewerConfig VIEWER_CONFIG =
            perfetto.protos.Protolog.ProtoLogViewerConfig.newBuilder()
                .addGroups(
                        perfetto.protos.Protolog.ProtoLogViewerConfig.Group.newBuilder()
                                .setId(1)
                                .setName(TestProtoLogGroup.TEST_GROUP.toString())
                                .setTag(TestProtoLogGroup.TEST_GROUP.getTag())
                ).addMessages(
                        perfetto.protos.Protolog.ProtoLogViewerConfig.MessageData.newBuilder()
                                .setMessageId(123)
                                .setMessage("My Test Debug Log Message %b")
                                .setLevel(ProtologCommon.ProtoLogLevel.PROTOLOG_LEVEL_DEBUG)
                                .setGroupId(1)
                                .setLocation("com/test/MyTestClass.java:123")
                ).build();

    @BeforeClass
    public static void init() {
        ProtoLog.init(TestProtoLogGroup.values());
    }

    @Before
    public void setUp() {
    public void setUp() throws ServiceNotFoundException {
        TestProtoLogGroup.TEST_GROUP.setLogToProto(mLogToProto);
        TestProtoLogGroup.TEST_GROUP.setLogToLogcat(mLogToLogcat);

        mProcessedProtoLogger = new ProcessedPerfettoProtoLogImpl(
                MOCK_TEST_FILE_PATH,
                () -> new AutoClosableProtoInputStream(VIEWER_CONFIG.toByteArray()),
                () -> {},
                TestProtoLogGroup.values()
        );
    }

    @Test
    public void log_Processed_NoArgs() {
        final var protoLog = ProtoLog.getSingleInstance();
        final var perfMonitor = new PerfMonitor();

        while (perfMonitor.keepRunning()) {
            protoLog.log(
            mProcessedProtoLogger.log(
                    LogLevel.INFO, TestProtoLogGroup.TEST_GROUP, 123,
                    0, (Object[]) null);
        }
@@ -90,11 +118,10 @@ public class ProtoLogPerfTest {

    @Test
    public void log_Processed_WithArgs() {
        final var protoLog = ProtoLog.getSingleInstance();
        final var perfMonitor = new PerfMonitor();

        while (perfMonitor.keepRunning()) {
            protoLog.log(
            mProcessedProtoLogger.log(
                    LogLevel.INFO, TestProtoLogGroup.TEST_GROUP, 123,
                    0b1110101001010100,
                    new Object[]{"test", 1, 2, 3, 0.4, 0.5, 0.6, true});
+2 −1
Original line number Diff line number Diff line
@@ -61,7 +61,8 @@ public class ProcessedPerfettoProtoLogImpl extends PerfettoProtoLogImpl {
                cacheUpdater, groups);
    }

    private ProcessedPerfettoProtoLogImpl(
    @VisibleForTesting
    public ProcessedPerfettoProtoLogImpl(
            @NonNull String viewerConfigFilePath,
            @NonNull ViewerConfigInputStreamProvider viewerConfigInputStreamProvider,
            @NonNull Runnable cacheUpdater,
+16 −25
Original line number Diff line number Diff line
@@ -40,7 +40,6 @@ import android.tools.traces.io.ResultWriter;
import android.tools.traces.monitors.PerfettoTraceMonitor;
import android.tools.traces.protolog.ProtoLogTrace;
import android.tracing.perfetto.DataSource;
import android.util.proto.ProtoInputStream;

import androidx.test.platform.app.InstrumentationRegistry;

@@ -74,7 +73,7 @@ import java.util.concurrent.atomic.AtomicInteger;
@SuppressWarnings("ConstantConditions")
@Presubmit
@RunWith(JUnit4.class)
public class PerfettoProtoLogImplTest {
public class ProcessedPerfettoProtoLogImplTest {
    private static final String TEST_PROTOLOG_DATASOURCE_NAME = "test.android.protolog";
    private static final String MOCK_VIEWER_CONFIG_FILE = "my/mock/viewer/config/file.pb";
    private final File mTracingDirectory = InstrumentationRegistry.getInstrumentation()
@@ -100,7 +99,7 @@ public class PerfettoProtoLogImplTest {

    private static ProtoLogViewerConfigReader sReader;

    public PerfettoProtoLogImplTest() throws IOException {
    public ProcessedPerfettoProtoLogImplTest() throws IOException {
    }

    @BeforeClass
@@ -151,7 +150,8 @@ public class PerfettoProtoLogImplTest {
        ViewerConfigInputStreamProvider viewerConfigInputStreamProvider = Mockito.mock(
                ViewerConfigInputStreamProvider.class);
        Mockito.when(viewerConfigInputStreamProvider.getInputStream())
                .thenAnswer(it -> new ProtoInputStream(sViewerConfigBuilder.build().toByteArray()));
                .thenAnswer(it -> new AutoClosableProtoInputStream(
                        sViewerConfigBuilder.build().toByteArray()));

        sCacheUpdater = () -> {};
        sReader = Mockito.spy(new ProtoLogViewerConfigReader(viewerConfigInputStreamProvider));
@@ -171,15 +171,10 @@ public class PerfettoProtoLogImplTest {
        sProtoLogConfigurationService =
                new ProtoLogConfigurationServiceImpl(dataSourceBuilder, tracer);

        if (android.tracing.Flags.clientSideProtoLogging()) {
            sProtoLog = new PerfettoProtoLogImpl(
                    MOCK_VIEWER_CONFIG_FILE, sReader, () -> sCacheUpdater.run(),
                    TestProtoLogGroup.values(), dataSourceBuilder, sProtoLogConfigurationService);
        } else {
            sProtoLog = new PerfettoProtoLogImpl(
                    viewerConfigInputStreamProvider, sReader, () -> sCacheUpdater.run(),
                    TestProtoLogGroup.values(), dataSourceBuilder, sProtoLogConfigurationService);
        }
        sProtoLog = new ProcessedPerfettoProtoLogImpl(
                MOCK_VIEWER_CONFIG_FILE, viewerConfigInputStreamProvider, sReader,
                () -> sCacheUpdater.run(), TestProtoLogGroup.values(), dataSourceBuilder,
                sProtoLogConfigurationService);

        busyWaitForDataSourceRegistration(TEST_PROTOLOG_DATASOURCE_NAME);
    }
@@ -398,18 +393,17 @@ public class PerfettoProtoLogImplTest {
    }

    @Test
    public void log_logcatEnabledNoMessage() {
    public void log_logcatEnabledNoMessageThrows() {
        when(sReader.getViewerString(anyLong())).thenReturn(null);
        PerfettoProtoLogImpl implSpy = Mockito.spy(sProtoLog);
        TestProtoLogGroup.TEST_GROUP.setLogToLogcat(true);
        TestProtoLogGroup.TEST_GROUP.setLogToProto(false);

        var assertion = assertThrows(RuntimeException.class, () ->
                implSpy.log(LogLevel.INFO, TestProtoLogGroup.TEST_GROUP, 1234, 4321,
                new Object[]{5});

        verify(implSpy).passToLogcat(eq(TestProtoLogGroup.TEST_GROUP.getTag()), eq(
                LogLevel.INFO), eq("UNKNOWN MESSAGE args = (5)"));
        verify(sReader).getViewerString(eq(1234L));
                    new Object[]{5}));
        Truth.assertThat(assertion).hasMessageThat()
                .contains("Failed to decode message for logcat");
    }

    @Test
@@ -539,16 +533,12 @@ public class PerfettoProtoLogImplTest {
        PerfettoTraceMonitor traceMonitor = PerfettoTraceMonitor.newBuilder()
                .enableProtoLog(TEST_PROTOLOG_DATASOURCE_NAME)
                .build();
        long before;
        long after;
        try {
            traceMonitor.start();
            before = SystemClock.elapsedRealtimeNanos();
            sProtoLog.log(
                    LogLevel.INFO, TestProtoLogGroup.TEST_GROUP, messageHash,
                    0b01100100,
                    new Object[]{"test", 1, 0.1, true});
            after = SystemClock.elapsedRealtimeNanos();
        } finally {
            traceMonitor.stop(mWriter);
        }
@@ -606,7 +596,8 @@ public class PerfettoProtoLogImplTest {
        Truth.assertThat(stacktrace).doesNotContain(DataSource.class.getSimpleName() + ".java");
        Truth.assertThat(stacktrace)
                .doesNotContain(ProtoLogImpl.class.getSimpleName() + ".java");
        Truth.assertThat(stacktrace).contains(PerfettoProtoLogImplTest.class.getSimpleName());
        Truth.assertThat(stacktrace)
                .contains(ProcessedPerfettoProtoLogImplTest.class.getSimpleName());
        Truth.assertThat(stacktrace).contains("stackTraceTrimmed");
    }

+5 −5
Original line number Diff line number Diff line
@@ -47,12 +47,12 @@ public class ProtoLogTest {
    }

    @Test
    public void throwOnRegisteringDuplicateGroup() {
        final var assertion = assertThrows(RuntimeException.class,
                () -> ProtoLog.init(TEST_GROUP_1, TEST_GROUP_1, TEST_GROUP_2));
    public void deduplicatesRegisteringDuplicateGroup() {
        ProtoLog.init(TEST_GROUP_1, TEST_GROUP_1, TEST_GROUP_2);

        Truth.assertThat(assertion).hasMessageThat().contains("" + TEST_GROUP_1.getId());
        Truth.assertThat(assertion).hasMessageThat().contains("duplicate");
        final var instance = ProtoLog.getSingleInstance();
        Truth.assertThat(instance.getRegisteredGroups())
                .containsExactly(TEST_GROUP_1, TEST_GROUP_2);
    }

    @Test