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

Commit 48e26151 authored by Hall Liu's avatar Hall Liu
Browse files

Fix flaky AnalytisTest by locking in EventRecord

AnalyticsTests were flaking with concurrent modification exceptions. Fix
this by locking mEvents properly in EventRecord.

Bug: 119226554
Test: leave it to presubmit
Change-Id: I4df0afe399fab148bbebcef51272c56f628ee18f
parent 6b0c47b2
Loading
Loading
Loading
Loading
+20 −16
Original line number Diff line number Diff line
@@ -180,7 +180,7 @@ public class EventManager {
            }
        }

        private final List<Event> mEvents = new LinkedList<>();
        private final List<Event> mEvents = Collections.synchronizedList(new LinkedList<>());
        private final Loggable mRecordEntry;

        public EventRecord(Loggable recordEntry) {
@@ -197,7 +197,7 @@ public class EventManager {
        }

        public List<Event> getEvents() {
            return mEvents;
            return new LinkedList<>(mEvents);
        }

        public List<EventTiming> extractEventTimings() {
@@ -207,11 +207,13 @@ public class EventManager {

            LinkedList<EventTiming> result = new LinkedList<>();
            Map<String, PendingResponse> pendingResponses = new HashMap<>();
            synchronized (mEvents) {
                for (Event event : mEvents) {
                    if (requestResponsePairs.containsKey(event.eventId)) {
                        // This event expects a response, so add that expected response to the maps
                        // of pending events.
                    for (EventManager.TimedEventPair p : requestResponsePairs.get(event.eventId)) {
                        for (EventManager.TimedEventPair p : requestResponsePairs.get(
                                event.eventId)) {
                            pendingResponses.put(p.mResponse, new PendingResponse(event.eventId,
                                    event.time, p.mTimeoutMillis, p.mName));
                        }
@@ -225,6 +227,7 @@ public class EventManager {
                        }
                    }
                }
            }

            return result;
        }
@@ -233,7 +236,8 @@ public class EventManager {
            pw.print(mRecordEntry.getDescription());

            pw.increaseIndent();
            for (Event event : mEvents) {
            // Iterate over copy of events so that this doesn't hold the lock for too long.
            for (Event event : getEvents()) {
                pw.print(event.timestampString);
                pw.print(" - ");
                pw.print(event.eventId);