Add unit test to verify that accessible content is determined at GC time.

PiperOrigin-RevId: 261377496
Change-Id: I05f5f1d6a86d3a4baa072e25584703a5fac30a30
diff --git a/src/main/java/com/google/android/libraries/feed/common/concurrent/testing/FakeTaskQueue.java b/src/main/java/com/google/android/libraries/feed/common/concurrent/testing/FakeTaskQueue.java
index 758cf68..8af3556 100644
--- a/src/main/java/com/google/android/libraries/feed/common/concurrent/testing/FakeTaskQueue.java
+++ b/src/main/java/com/google/android/libraries/feed/common/concurrent/testing/FakeTaskQueue.java
@@ -25,9 +25,13 @@
   private boolean completeResetWasCalled = false;
 
   public FakeTaskQueue(FakeClock fakeClock, FakeThreadUtils fakeThreadUtils) {
+    this(fakeClock, FakeDirectExecutor.runTasksImmediately(fakeThreadUtils));
+  }
+
+  public FakeTaskQueue(FakeClock fakeClock, FakeDirectExecutor fakeDirectExecutor) {
     super(
         new FakeBasicLoggingApi(),
-        FakeDirectExecutor.runTasksImmediately(fakeThreadUtils),
+        fakeDirectExecutor,
         FakeMainThreadRunner.create(fakeClock),
         fakeClock);
   }
diff --git a/src/test/java/com/google/android/libraries/feed/feedsessionmanager/internal/SessionCacheTest.java b/src/test/java/com/google/android/libraries/feed/feedsessionmanager/internal/SessionCacheTest.java
index 14b28f9..5a504e4 100644
--- a/src/test/java/com/google/android/libraries/feed/feedsessionmanager/internal/SessionCacheTest.java
+++ b/src/test/java/com/google/android/libraries/feed/feedsessionmanager/internal/SessionCacheTest.java
@@ -24,6 +24,7 @@
 import com.google.android.libraries.feed.api.internal.common.testing.ContentIdGenerators;
 import com.google.android.libraries.feed.api.internal.modelprovider.ModelProvider.ViewDepthProvider;
 import com.google.android.libraries.feed.api.internal.store.Store;
+import com.google.android.libraries.feed.common.concurrent.testing.FakeDirectExecutor;
 import com.google.android.libraries.feed.common.concurrent.testing.FakeTaskQueue;
 import com.google.android.libraries.feed.common.concurrent.testing.FakeThreadUtils;
 import com.google.android.libraries.feed.common.time.TimingUtils;
@@ -31,6 +32,7 @@
 import com.google.android.libraries.feed.testing.modelprovider.FakeModelProvider;
 import com.google.android.libraries.feed.testing.store.FakeStore;
 import com.google.search.now.feed.client.StreamDataProto.SessionMetadata;
+import com.google.search.now.feed.client.StreamDataProto.StreamFeature;
 import com.google.search.now.feed.client.StreamDataProto.StreamPayload;
 import com.google.search.now.feed.client.StreamDataProto.StreamSession;
 import com.google.search.now.feed.client.StreamDataProto.StreamSessions;
@@ -101,6 +103,24 @@
   }
 
   @Test
+  public void testInitialization_accessibleContentShouldBeDeterminedAtGcTime() {
+    FakeDirectExecutor fakeDirectExecutor = FakeDirectExecutor.queueAllTasks(fakeThreadUtils);
+    fakeTaskQueue = new FakeTaskQueue(fakeClock, fakeDirectExecutor);
+    fakeStore = new FakeStore(fakeThreadUtils, fakeTaskQueue, fakeClock);
+    fakeThreadUtils.enforceMainThread(false);
+    fakeTaskQueue.initialize(() -> {});
+    sessionCache = getSessionCache();
+
+    Session session = populateSession(1, 2, /* commitToStore= */ true);
+    sessionCache.initialize();
+    setSessions(session);
+    fakeDirectExecutor.runAllTasks(); // Run GC.
+
+    assertThat(fakeDirectExecutor.hasTasks()).isFalse();
+    assertPayloads(/* featureCnt= */ 2);
+  }
+
+  @Test
   public void testInitialization_contentGcShouldKeepSharedStates() {
     fakeStore.setContent(
         "foo",
@@ -501,12 +521,20 @@
             .setContentId(rootId)
             .build());
     for (int i = 0; i < featureCnt; i++) {
+      String contentId = idGenerators.createFeatureContentId(i);
       head.add(
           StreamStructure.newBuilder()
               .setOperation(Operation.UPDATE_OR_APPEND)
-              .setContentId(idGenerators.createFeatureContentId(i))
+              .setContentId(contentId)
               .setParentContentId(rootId)
               .build());
+      if (commitToStore) {
+        fakeStore.setContent(
+            contentId,
+            StreamPayload.newBuilder()
+                .setStreamFeature(StreamFeature.getDefaultInstance())
+                .build());
+      }
     }
     session.setSessionId("stream:" + id);
     session.bindModelProvider(fakeModelProvider, mock(ViewDepthProvider.class));
@@ -534,4 +562,11 @@
           session.getSessionId(), /* creationTimeMillis= */ 0L, SCHEMA_VERSION, session);
     }
   }
+
+  private void assertPayloads(int featureCnt) {
+    for (int i = 0; i < featureCnt; i++) {
+      String contentId = idGenerators.createFeatureContentId(i);
+      assertThat(fakeStore.getContentById(contentId)).hasSize(1);
+    }
+  }
 }