4040import org .apache .jackrabbit .oak .spi .commit .CommitInfo ;
4141import org .apache .jackrabbit .oak .spi .commit .EmptyHook ;
4242import org .apache .jackrabbit .oak .spi .state .NodeBuilder ;
43+ import org .awaitility .Awaitility ;
4344import org .jetbrains .annotations .NotNull ;
4445import org .jetbrains .annotations .Nullable ;
4546import org .junit .Rule ;
5051
5152import java .io .File ;
5253import java .io .IOException ;
53- import java .sql .Time ;
5454import java .util .HashMap ;
5555import java .util .HashSet ;
5656import java .util .LinkedHashSet ;
@@ -84,7 +84,7 @@ public void testDecorationSkippedForWrongArguments() {
8484 }
8585
8686 @ Test
87- public void viaFileStoreBuilder () throws InvalidFileStoreVersionException , IOException , CommitFailedException {
87+ public void viaFileStoreBuilder () throws InvalidFileStoreVersionException , IOException , CommitFailedException , InterruptedException {
8888 try (FileStore fileStore = FileStoreBuilder .fileStoreBuilder (folder .getRoot ())
8989 .build ()) {
9090 SegmentNodeStore nodeStore = SegmentNodeStoreBuilders .builder (fileStore ).build ();
@@ -101,10 +101,13 @@ public void viaFileStoreBuilder() throws InvalidFileStoreVersionException, IOExc
101101 .build ()) {
102102 SegmentId root = fileStore .getRevisions ().getPersistedHead ().getSegmentId ();
103103 Segment segment = root .getSegment ();
104- int referencedSegmentIdCount = segment .getReferencedSegmentIdCount ();
104+
105+ int expectedCacheSize = 1 + segment .getReferencedSegmentIdCount ();
106+ Awaitility .await ()
107+ .atMost (10 , TimeUnit .SECONDS )
108+ .untilAsserted (() -> assertEquals (expectedCacheSize , persistentCache .segments .size ()));
105109
106110 assertTrue (persistentCache .containsSegment (root .getMostSignificantBits (), root .getLeastSignificantBits ()));
107- assertEquals (1 + referencedSegmentIdCount , persistentCache .segments .size ());
108111 }
109112 }
110113
@@ -172,18 +175,20 @@ public void testPreloading() throws IOException, InvalidFileStoreVersionExceptio
172175 }
173176
174177 private void assertReferencedSegmentsLoaded (Set <UUID > referencedSegments , MemoryTestCache underlyingCache , SegmentPreloader preloadingCache ) throws InterruptedException {
175- long timeoutSec = 10 ;
176- long deadline = System .currentTimeMillis () + TimeUnit .SECONDS .toMillis (timeoutSec );
177178 Set <UUID > segments = new HashSet <>(referencedSegments );
178- while (!segments .isEmpty () && System .currentTimeMillis () < deadline ) {
179- segments .removeIf (segment ->
180- underlyingCache .containsSegment (segment .getMostSignificantBits (), segment .getLeastSignificantBits ())
181- && preloadingCache .containsSegment (segment .getMostSignificantBits (), segment .getLeastSignificantBits ()));
182- TimeUnit .MILLISECONDS .sleep (10 );
183- }
179+ int timeoutSec = 10 ;
180+ Awaitility
181+ .await ()
182+ .atMost (timeoutSec , TimeUnit .SECONDS )
183+ .untilAsserted (() -> {
184+ segments .removeIf (uuid ->
185+ underlyingCache .containsSegment (uuid .getMostSignificantBits (), uuid .getLeastSignificantBits ())
186+ && preloadingCache .containsSegment (uuid .getMostSignificantBits (), uuid .getLeastSignificantBits ()));
187+ assertEquals ("Not all referenced segments have been preloaded within " + timeoutSec + " seconds" ,
188+ Set .of (), segments );
189+ });
190+
184191
185- assertEquals ("Not all referenced segments have been preloaded within " + timeoutSec + " seconds" ,
186- Set .of (), segments );
187192 }
188193
189194 private static Map <UUID , Set <UUID >> computeFullGraph (TarFiles tarFiles ) throws IOException {
0 commit comments