Skip to content

Commit 92ad096

Browse files
committed
OAK-11934 - segment preloading for PersistentCache
- use Awaitility for unit test
1 parent 70e124d commit 92ad096

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

oak-segment-tar/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@
369369
<artifactId>org.apache.sling.testing.osgi-mock.junit4</artifactId>
370370
<scope>test</scope>
371371
</dependency>
372+
<dependency>
373+
<groupId>org.awaitility</groupId>
374+
<artifactId>awaitility</artifactId>
375+
<version>4.2.1</version>
376+
<scope>test</scope>
377+
</dependency>
372378
<dependency>
373379
<groupId>org.osgi</groupId>
374380
<artifactId>org.osgi.service.event</artifactId>

oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/preloader/SegmentPreloaderTest.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
4141
import org.apache.jackrabbit.oak.spi.commit.EmptyHook;
4242
import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
43+
import org.awaitility.Awaitility;
4344
import org.jetbrains.annotations.NotNull;
4445
import org.jetbrains.annotations.Nullable;
4546
import org.junit.Rule;
@@ -50,7 +51,6 @@
5051

5152
import java.io.File;
5253
import java.io.IOException;
53-
import java.sql.Time;
5454
import java.util.HashMap;
5555
import java.util.HashSet;
5656
import 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

Comments
 (0)