From 82e65ceca50554faf82575b319ff19be71ab9fa7 Mon Sep 17 00:00:00 2001 From: datadog-bits <263423550+datadog-bits@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:03:58 +0000 Subject: [PATCH] Wait for the delegate swap in DeferredProfilingContextIntegrationTest The deferred factory registers the process context before constructing the integration, and the delegate is swapped only after construction returns. Awaiting the registration latch could therefore wake the test thread before construction completed, failing the constructions == 1 assertion. Await whenAvailable instead, which fires after the swap, and drop the now-unused registration latch. Co-Authored-By: Claude Opus 5.5 (1M context) Co-authored-by: cgcote <5115638+cgcote@users.noreply.github.com> --- .../DeferredProfilingContextIntegrationTest.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) 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(); } }