Why does StepVerifier.verifyComplete() is going into infinite loop when I use Scheduler and the test completes when I remove scheduler?

huangapple go评论61阅读模式
英文:

Why does StepVerifier.verifyComplete() is going into infinite loop when I use Scheduler and the test completes when I remove scheduler?

问题

我正在尝试测试使用 Reactor Mono 的编排流程。当我使用 StepVerifier 测试流程时,测试永远不会完成。如果我删除 publishOn(myThreadDispatcher),则测试会完成。我尝试使用 StepVerifier.withVirtualTime,但线程会过早完成并返回 onComplete 而不是 OnNext。请告诉我是否需要添加或更正任何内容。

@Component
public class MyOrchestrator {

    @Autowired
    @Qualifier("myThreadDispatcher")
    private Scheduler myThreadDispatcher;

    @Autowired
    private PrepareRequestInvoker prepareRequestInvoker;

    @Autowired
    private EventHandler eventHandler;

    public Mono<Result> orchestrate(FooRequest fooRequest) {
        return Mono.just(fooRequest)
                .publishOn(myThreadDispatcher)
                .flatMap(fooRequest -> prepareRequestInvoker.invoke(fooRequest))
                .flatMap(queryResult -> eventHandler.orchestrate(mockResult, fooRequest));
    }
}

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.management.*", "javax.crypto.*"})
class MyTest {

    @Mock
    private Scheduler myThreadDispatcher;

    @Mock
    private PrepareRequestInvoker prepareRequestInvoker;

    @Mock
    private EventHandler eventHandler;

    @InjectMocks
    private MyOrchestrator orchestrator;

    Result mockResult = Result.from("test");

    Mono<Result> resultMono = Mono.fromCompletionStage(CompletableFuture.completedFuture(mockResult));

    @Test
    public void testMono() {
        prepareTest(createRequest());
        Mockito.when(prepareRequestInvoker.invoke(fooRequest)).thenReturn(resultMono);
        Mockito.when(eventHandler.orchestrate(mockResult, fooRequest)).thenReturn(resultMono);
        Mono<Result> monoToVerify = orchestrator.orchestrate(fooRequest);

        StepVerifier.withVirtualTime(monoToVerify)
                .expectNext(Result.from("test"))
                .verifyComplete();
    }
}

请注意,上述代码中的双引号已经更正为正常的双引号,以确保代码的正确性。

英文:

I am trying to test orchestrator flow which is using Reactor Mono. When I am testing the flow using StepVerifier the test was never finished. If I remove publishOn(myThreadDispatcher) then the test is completing. I tried StepVerifier withVirtualTime but the thread is finishing too early and returning onComplete instead of OnNext. Please let me know if anything needs to be added or corrected.

@Component
public class MyOrchestrator {
@Autowired
@Qualifier(&quot;myThreadDispatcher&quot;)
private Scheduler myThreadDispatcher;
@Autowired
private PrepareRequestInvoker prepareRequestInvoker;
@Autowired
private EventHandler eventHandler;
@Override
public Mono&lt;Result&gt; orchestrate(FooRequest fooRequest) {
return Mono.just(fooRequest)
.publishOn(myThreadDispatcher)
.flatMap(fooRequest -&gt; prepareRequestInvoker.invoke(fooRequest))
.flatMap(queryResult -&gt; eventHandler.orchestrate(mockResult, fooRequest));
}
}
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({&quot;javax.management.*&quot;, &quot;javax.crypto.*&quot;})
class MyTest{
@Mock
private Scheduler myThreadDispatcher;
@Mock
private PrepareRequestInvoker prepareRequestInvoker;
@Mock
private EventHandler eventHandler;
@InjectMocks
private MyOrchestrator orchestrator;
Result mockResult = Result.from(&quot;test&quot;);
Mono&lt;Result&gt; resultMono = Mono.fromCompletionStage(CompletableFuture.completedFuture(mockResult));
@Test
public void testMono(){
prepareTest(createRequest());
Mockito.when(prepareRequestInvoker.invoke(fooRequest)).thenReturn(resultMono);
Mockito.when(eventHandler.orchestrate(mockResult,fooRequest)).thenReturn(resultMono);
Mono&lt;Result&gt; monoToVerify= orchestrator.orchestrate(fooRequest);
StepVerifier.withVirtualTime(monoToVerify)
.expectNext(Result.from(&quot;test&quot;))
.verifyComplete();
}
}

答案1

得分: 1

我通过使用@Spy并在模拟调度器的位置注入真实对象来解决了这个问题。感谢@xerx593提供的一些启发。

英文:

I solved the issue by using @Spy and injecting real object in place of mock scheduler. Thanks @xerx593 for throwing some light.

huangapple
  • 本文由 发表于 2023年6月2日 14:37:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定