diff --git a/dd-java-agent/agent-bootstrap/src/test/java/datadog/trace/bootstrap/DeferredProfilingContextIntegrationTest.java b/dd-java-agent/agent-bootstrap/src/test/java/datadog/trace/bootstrap/DeferredProfilingContextIntegrationTest.java index 671188e913d..28398c1fe01 100644 --- a/dd-java-agent/agent-bootstrap/src/test/java/datadog/trace/bootstrap/DeferredProfilingContextIntegrationTest.java +++ b/dd-java-agent/agent-bootstrap/src/test/java/datadog/trace/bootstrap/DeferredProfilingContextIntegrationTest.java @@ -38,7 +38,6 @@ void reset() { FakeDatadogProfilingIntegration.constructions.set(0); FakeDatadogProfilingIntegration.constructionThread.set(null); FakeProcessContext.registrations.set(0); - FakeProcessContext.registered = new CountDownLatch(1); FakeDatadogProfilingIntegration.gate = new CountDownLatch(0); FakeDatadogProfilingIntegration.onAttachCalls.set(0); FakeDatadogProfilingIntegration.onDetachCalls.set(0); @@ -64,10 +63,12 @@ void deferredConstructionDoesNotRunOnTheCallingThread() throws Exception { assertSame(ProfilingScope.NO_OP, integration.newScope()); assertSame(Timing.NoOp.INSTANCE, integration.start(TimerType.QUEUEING)); + // wait for the delegate swap, which follows both registration and construction: the + // registration alone does not mean the construction has completed + CountDownLatch available = new CountDownLatch(1); + integration.whenAvailable(available::countDown); FakeDatadogProfilingIntegration.gate.countDown(); - assertTrue( - FakeProcessContext.registered.await(30, TimeUnit.SECONDS), - "deferred construction never ran"); + assertTrue(available.await(30, TimeUnit.SECONDS), "deferred construction never ran"); // the deferred work happened, and it happened on another thread assertEquals(1, FakeDatadogProfilingIntegration.constructions.get()); assertEquals(1, FakeProcessContext.registrations.get()); @@ -225,11 +226,9 @@ public Class loadClass(final String name) throws ClassNotFoundException { public static final class FakeProcessContext { static final AtomicInteger registrations = new AtomicInteger(); - static volatile CountDownLatch registered = new CountDownLatch(1); public static void register(final ConfigProvider configProvider) { registrations.incrementAndGet(); - registered.countDown(); } }