diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/reactor/SpanSubscriberTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/reactor/SpanSubscriberTests.java index c2253b52f..7aae149ff 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/reactor/SpanSubscriberTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/reactor/SpanSubscriberTests.java @@ -135,7 +135,6 @@ public class SpanSubscriberTests { final AtomicReference spanInOperation = new AtomicReference<>(); log.info("Hello"); - try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(span)) { Flux.just(1, 2, 3).publishOn(Schedulers.single()).log("reactor.1") .map(d -> d + 1).map(d -> d + 1).publishOn(Schedulers.newSingle("secondThread")).log("reactor.2") @@ -171,6 +170,44 @@ public class SpanSubscriberTests { then(this.tracer.currentSpan()).isNull(); } + @Test + public void checkSequenceOfOperations() { + Span parentSpan = this.tracer.nextSpan().name("foo").start(); + log.info("Hello"); + try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(parentSpan)) { + final Long traceId = Mono.fromCallable(tracer::currentSpan) + .map(span -> span.context().traceId()) + .block(); + then(traceId).isNotNull(); + + final Long secondTraceId = Mono.fromCallable(tracer::currentSpan) + .map(span -> span.context().traceId()) + .block(); + then(secondTraceId).isEqualTo(traceId); // different trace ids here + } + } + + @Test + public void checkTraceIdDuringZipOperation() { + Span initSpan = this.tracer.nextSpan().name("foo").start(); + final AtomicReference spanInOperation = new AtomicReference<>(); + final AtomicReference spanInZipOperation = new AtomicReference<>(); + + try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(initSpan)) { + Mono.fromCallable(tracer::currentSpan) + .map(span -> span.context().traceId()) + .doOnNext(spanInOperation::set) + .zipWith( + Mono.fromCallable(tracer::currentSpan) + .map(span -> span.context().traceId()) + .doOnNext(spanInZipOperation::set)) + .block(); + } + + then(spanInZipOperation).hasValue(initSpan.context().traceId()); // ok here + then(spanInOperation).hasValue(initSpan.context().traceId()); // Expecting to have value: <1L> but did not. + } + @AfterClass public static void cleanup() { Hooks.resetOnLastOperator();