Skip to content

Commit 76639fb

Browse files
authored
Do not rely on timeout when testing smap entry cache (#9265)
1 parent 6065351 commit 76639fb

File tree

2 files changed

+10
-7
lines changed
  • dd-java-agent/agent-profiling/profiling-controller-openjdk/src

2 files changed

+10
-7
lines changed

dd-java-agent/agent-profiling/profiling-controller-openjdk/src/main/java/com/datadog/profiling/controller/openjdk/events/SmapEntryCache.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ static class AnnotatedRegion {
6262
this.smapsPath = smapsPath;
6363
}
6464

65+
// @VisibleForTesting
66+
void invalidate() {
67+
UPDATER.getAndSet(this, System.nanoTime() - (2 * ttl));
68+
}
69+
6570
@SuppressWarnings("unchecked")
6671
public List<SmapEntryEvent> getEvents() {
6772
long prevTimestamp = lastTimestamp;
@@ -296,9 +301,9 @@ private static Map<Long, String> getAnnotatedRegions() {
296301
return Collections.emptyMap();
297302
}
298303

299-
private static void collectEvents(List<SmapEntryEvent> events) {
304+
private void collectEvents(List<SmapEntryEvent> events) {
300305
try (BufferedReader br =
301-
new BufferedReader(new InputStreamReader(Files.newInputStream(SMAPS_PATH)), 64 * 1024)) {
306+
new BufferedReader(new InputStreamReader(Files.newInputStream(smapsPath)), 64 * 1024)) {
302307
readEvents(br, events);
303308
Map<Long, String> regions = getAnnotatedRegions();
304309
for (SmapEntryEvent e : events) {

dd-java-agent/agent-profiling/profiling-controller-openjdk/src/test/java/com/datadog/profiling/controller/openjdk/events/SmapEntryCacheTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ void getEvents() throws Exception {
1818
assumeTrue(OperatingSystem.isLinux());
1919
// We need at least Java 22 for the annotated regions
2020
assumeTrue(JavaVirtualMachine.isJavaVersionAtLeast(22));
21-
SmapEntryCache smapEntryCache = new SmapEntryCache(Duration.ofMillis(100));
21+
SmapEntryCache smapEntryCache =
22+
new SmapEntryCache(Duration.ofHours(1)); // set up a really long expiration duration
2223
List<SmapEntryEvent> events1 = smapEntryCache.getEvents();
2324
List<SmapEntryEvent> events2 = smapEntryCache.getEvents();
2425
// the cache is using double buffered event list so we can use identity comparison
2526
assertSame(events1, events2);
2627

27-
long ts = System.nanoTime();
28-
while (System.nanoTime() - ts < 150_000_000L) { // make sure the cache is expired
29-
Thread.sleep(200);
30-
}
28+
smapEntryCache.invalidate(); // pretend expiring the cache
3129
events1 = smapEntryCache.getEvents();
3230
assertNotSame(events1, events2);
3331
}

0 commit comments

Comments
 (0)