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

Commit ab504955 authored by tanaykhemani's avatar tanaykhemani
Browse files

Use android.util.LruCache instead of a custom definition

MediaRouterMetricLogger uses a custom implementation of LRU Cache.
Replacing that with android.util.LruCache and adding relevant tests.

Test: atest MediaRouterMetricLoggerTest
Bug: 433865311
Flag: EXEMPT Cleanup of existing code
Change-Id: I2c6bcd979bb32a1b300761c3caabdf2cc9b0b6ef
parent 1a7e47fe
Loading
Loading
Loading
Loading
+22 −32
Original line number Diff line number Diff line
@@ -30,11 +30,12 @@ import static com.android.server.media.MediaRouterStatsLog.MEDIA_ROUTER_EVENT_RE
import android.annotation.NonNull;
import android.media.MediaRoute2ProviderService;
import android.util.Log;
import android.util.LruCache;
import android.util.Slog;

import com.android.internal.annotations.VisibleForTesting;

import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Logs metrics for MediaRouter2.
@@ -47,11 +48,23 @@ final class MediaRouterMetricLogger {
    private static final int REQUEST_INFO_CACHE_CAPACITY = 100;

    /** LRU cache to store request info. */
    private final RequestInfoCache mRequestInfoCache;
    private final LruCache<Long, RequestInfo> mRequestInfoCache;

    /** Constructor for {@link MediaRouterMetricLogger}. */
    public MediaRouterMetricLogger() {
        mRequestInfoCache = new RequestInfoCache(REQUEST_INFO_CACHE_CAPACITY);
        mRequestInfoCache =
                new LruCache<>(REQUEST_INFO_CACHE_CAPACITY) {
                    @Override
                    protected void entryRemoved(
                            boolean evicted, Long key, RequestInfo oldValue, RequestInfo newValue) {
                        if (evicted) {
                            Slog.d(TAG, "Evicted request info: " + oldValue.mUniqueRequestId);
                            logOperationTriggered(
                                    oldValue.mEventType,
                                    MEDIA_ROUTER_EVENT_REPORTED__RESULT__RESULT_UNSPECIFIED);
                        }
                    }
                };
    }

    /**
@@ -179,6 +192,11 @@ final class MediaRouterMetricLogger {
        return mRequestInfoCache.size();
    }

    @VisibleForTesting
    public int getRequestInfoCacheCapacity() {
        return REQUEST_INFO_CACHE_CAPACITY;
    }

    private void logMediaRouterEvent(int eventType, int result) {
        MediaRouterStatsLog.write(
                MediaRouterStatsLog.MEDIA_ROUTER_EVENT_REPORTED, eventType, result);
@@ -188,34 +206,6 @@ final class MediaRouterMetricLogger {
        }
    }

    /** A cache for storing request info that evicts entries when it reaches its capacity. */
    class RequestInfoCache extends LinkedHashMap<Long, RequestInfo> {

        public final int capacity;

        /**
         * Constructor for {@link RequestInfoCache}.
         *
         * @param capacity The maximum capacity of the cache.
         */
        public RequestInfoCache(int capacity) {
            super(capacity, 1.0f, true);
            this.capacity = capacity;
        }

        @Override
        protected boolean removeEldestEntry(Map.Entry<Long, RequestInfo> eldest) {
            boolean shouldRemove = size() > capacity;
            if (shouldRemove) {
                Slog.d(TAG, "Evicted request info: " + eldest.getValue());
                logOperationTriggered(
                        eldest.getValue().mEventType,
                        MEDIA_ROUTER_EVENT_REPORTED__RESULT__RESULT_UNSPECIFIED);
            }
            return shouldRemove;
        }
    }

    /** Class to store request info. */
    static class RequestInfo {
        public final long mUniqueRequestId;
+31 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static android.media.MediaRoute2ProviderService.REASON_REJECTED;
import static android.media.MediaRoute2ProviderService.REASON_ROUTE_NOT_AVAILABLE;
import static android.media.MediaRoute2ProviderService.REASON_UNIMPLEMENTED;
import static android.media.MediaRoute2ProviderService.REASON_UNKNOWN_ERROR;

import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
import static com.android.server.media.MediaRouterStatsLog.MEDIA_ROUTER_EVENT_REPORTED;
import static com.android.server.media.MediaRouterStatsLog.MEDIA_ROUTER_EVENT_REPORTED__EVENT_TYPE__EVENT_TYPE_CREATE_SESSION;
@@ -35,10 +36,13 @@ import static com.android.server.media.MediaRouterStatsLog.MEDIA_ROUTER_EVENT_RE
import static com.android.server.media.MediaRouterStatsLog.MEDIA_ROUTER_EVENT_REPORTED__RESULT__RESULT_UNIMPLEMENTED;
import static com.android.server.media.MediaRouterStatsLog.MEDIA_ROUTER_EVENT_REPORTED__RESULT__RESULT_UNKNOWN_ERROR;
import static com.android.server.media.MediaRouterStatsLog.MEDIA_ROUTER_EVENT_REPORTED__RESULT__RESULT_UNSPECIFIED;

import static com.google.common.truth.Truth.assertThat;

import androidx.test.runner.AndroidJUnit4;

import com.android.modules.utils.testing.ExtendedMockitoRule;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -137,4 +141,31 @@ public class MediaRouterMetricLoggerTest {
                123, MEDIA_ROUTER_EVENT_REPORTED__EVENT_TYPE__EVENT_TYPE_CREATE_SESSION);
        assertThat(mLogger.getRequestCacheSize()).isEqualTo(1);
    }

    @Test
    public void addRequestInfo_whenCacheFull_evictsFromCacheAndLogsUnspecified() {
        assertThat(mLogger.getRequestCacheSize()).isEqualTo(0);

        // Fill the cache to capacity.
        int cacheCapacity = mLogger.getRequestInfoCacheCapacity();
        for (int i = 0; i < cacheCapacity; i++) {
            mLogger.addRequestInfo(
                    /* uniqueRequestId= */ i,
                    MEDIA_ROUTER_EVENT_REPORTED__EVENT_TYPE__EVENT_TYPE_CREATE_SESSION);
        }

        // Add one more request to trigger eviction.
        mLogger.addRequestInfo(
                /* uniqueRequestId= */ cacheCapacity,
                MEDIA_ROUTER_EVENT_REPORTED__EVENT_TYPE__EVENT_TYPE_CREATE_SESSION);

        // Verify cache size is correct and generic result gets logged.
        assertThat(mLogger.getRequestCacheSize()).isEqualTo(cacheCapacity);
        verify(
                () ->
                        MediaRouterStatsLog.write(
                                MEDIA_ROUTER_EVENT_REPORTED,
                                MEDIA_ROUTER_EVENT_REPORTED__EVENT_TYPE__EVENT_TYPE_CREATE_SESSION,
                                MEDIA_ROUTER_EVENT_REPORTED__RESULT__RESULT_UNSPECIFIED));
    }
}