From b1a34d2892d1cd3271528b8dc71965c846bdc074 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 5 Apr 2016 09:37:51 +0200 Subject: [PATCH] Fixed the broken test --- .../rxjava/SleuthRxJavaIntegrationTests.java | 42 ++++++++----------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/rxjava/SleuthRxJavaIntegrationTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/rxjava/SleuthRxJavaIntegrationTests.java index 9742552be..9a51f7f86 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/rxjava/SleuthRxJavaIntegrationTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/rxjava/SleuthRxJavaIntegrationTests.java @@ -21,20 +21,19 @@ import org.springframework.cloud.sleuth.sampler.AlwaysSampler; import org.springframework.cloud.sleuth.trace.TestSpanContextHolder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.stereotype.Component; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import rx.Observable; -import rx.Subscriber; import rx.functions.Action0; import rx.plugins.SleuthRxJavaPlugins; import rx.schedulers.Schedulers; -import static com.jayway.awaitility.Awaitility.await; import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {SleuthRxJavaIntegrationTests.TestConfig.class}) +@DirtiesContext public class SleuthRxJavaIntegrationTests { @Autowired Tracer tracer; @@ -53,27 +52,22 @@ public class SleuthRxJavaIntegrationTests { TestSpanContextHolder.removeCurrentSpan(); } - @BeforeClass - public static void setUp() { - if (!(SleuthRxJavaPlugins.getInstance().getSchedulersHook() instanceof SleuthRxJavaSchedulersHook)) { - SleuthRxJavaPlugins.resetPlugins(); - } - } - @AfterClass + @BeforeClass public static void cleanUp() { SleuthRxJavaPlugins.resetPlugins(); } @Test public void should_create_new_span_when_rx_java_action_is_executed_and_there_was_no_span() { - Observable.create((Subscriber action) -> { - action.onNext((Action0) () -> caller = new StringBuilder("actual_action")); - }).subscribeOn(Schedulers.newThread()).subscribe(Action0::call); + Observable.defer(() -> Observable.just( + (Action0) () -> this.caller = new StringBuilder("actual_action") + )).subscribeOn(Schedulers.newThread()).toBlocking() + .subscribe(Action0::call); - await().until(() -> then(caller.toString()).isEqualTo("actual_action")); + then(this.caller.toString()).isEqualTo("actual_action"); then(this.tracer.getCurrentSpan()).isNull(); - await().until(() -> then(this.listener.getEvents().size()).isEqualTo(1)); + then(this.listener.getEvents()).hasSize(1); then(this.listener.getEvents().get(0)).hasNameEqualTo("rxjava"); then(this.listener.getEvents().get(0)).hasATag(Span.SPAN_LOCAL_COMPONENT_TAG_NAME, "rxjava"); then(this.listener.getEvents().get(0)).isALocalComponentSpan(); @@ -84,23 +78,23 @@ public class SleuthRxJavaIntegrationTests { Span spanInCurrentThread = this.tracer.createSpan("current_span"); this.tracer.addTag(Span.SPAN_LOCAL_COMPONENT_TAG_NAME, "current_span"); - Observable.create((Subscriber action) -> { - action.onNext((Action0) () -> caller = new StringBuilder("actual_action")); - }).subscribeOn(Schedulers.newThread()).subscribe(Action0::call); + Observable.defer(() -> Observable.just( + (Action0) () -> this.caller = new StringBuilder("actual_action") + )).subscribeOn(Schedulers.newThread()).toBlocking() + .subscribe(Action0::call); - await().until(() -> then(caller.toString()).isEqualTo("actual_action")); + then(this.caller.toString()).isEqualTo("actual_action"); then(this.tracer.getCurrentSpan()).isNotNull(); //making sure here that no new spans were created or reported as closed - await().until(() -> then(this.listener.getEvents().size()).isEqualTo(0)); + then(this.listener.getEvents()).isEmpty(); then(spanInCurrentThread).hasNameEqualTo(spanInCurrentThread.getName()); then(spanInCurrentThread).hasATag(Span.SPAN_LOCAL_COMPONENT_TAG_NAME, "current_span"); then(spanInCurrentThread).isALocalComponentSpan(); } - @Component - public static class Listener implements SpanReporter { + static class Listener implements SpanReporter { - List events = new ArrayList<>(); + private List events = new ArrayList<>(); public List getEvents() { return this.events; @@ -117,7 +111,7 @@ public class SleuthRxJavaIntegrationTests { public static class TestConfig { @Bean - Listener listener() { + SpanReporter listener() { return new Listener(); }