Fix ApacheHttpAsync race condition

This commit is contained in:
Nikolay Martynov 2019-05-17 13:49:46 -04:00
parent 920b9b5ad3
commit c5dbd7d0d7
2 changed files with 5 additions and 5 deletions

View File

@ -1,6 +1,5 @@
import datadog.trace.agent.test.base.HttpClientTest
import datadog.trace.instrumentation.apachehttpasyncclient.ApacheHttpAsyncClientDecorator
import io.opentracing.util.GlobalTracer
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.nio.client.HttpAsyncClients
import org.apache.http.message.BasicHeader
@ -34,10 +33,7 @@ class ApacheHttpAsyncClientNullCallbackTest extends HttpClientTest<ApacheHttpAsy
Future future = client.execute(request, null)
future.get()
if (callback != null) {
// Request span is closed asynchronously even in regards to returned future so we have to wait here.
if (GlobalTracer.get().activeSpan() != null) {
blockUntilChildSpansFinished(1)
}
blockUntilChildSpansFinished(1)
callback()
}
return 200

View File

@ -200,6 +200,10 @@ public abstract class AgentTestRunner extends Specification {
public void blockUntilChildSpansFinished(final int numberOfSpans) throws InterruptedException {
final DDSpan span = (DDSpan) io.opentracing.util.GlobalTracer.get().activeSpan();
if (span == null) {
// If there is no active span avoid getting an NPE
return;
}
final PendingTrace pendingTrace = span.context().getTrace();
while (pendingTrace.size() < numberOfSpans) {