Fix JFR lock wait test (#8494)

This commit is contained in:
Lauri Tulmin 2023-05-16 09:42:07 +03:00 committed by GitHub
parent eb77991e5d
commit 30fc14e247
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 4 deletions

View File

@ -6,7 +6,10 @@
package io.opentelemetry.instrumentation.runtimemetrics.java17;
import static io.opentelemetry.instrumentation.runtimemetrics.java17.internal.Constants.MILLISECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
@ -19,12 +22,31 @@ class JfrCpuLockTest {
@Test
void shouldHaveLockEvents() throws Exception {
// This should generate some events
System.gc();
synchronized (this) {
Thread.sleep(1000);
AtomicBoolean done = new AtomicBoolean(false);
synchronized (done) {
new Thread(
() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException exception) {
Thread.currentThread().interrupt();
}
synchronized (done) {
done.set(true);
done.notifyAll();
}
})
.start();
long waitTime = Duration.ofSeconds(10).toMillis();
long endTime = System.currentTimeMillis() + Duration.ofSeconds(10).toMillis();
while (!done.get() && waitTime > 0) {
done.wait(waitTime);
waitTime = endTime - System.currentTimeMillis();
}
}
assertThat(done.get()).isEqualTo(true);
jfrExtension.waitAndAssertMetrics(
metric ->
metric