英文:
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("myThreadDispatcher")
private Scheduler myThreadDispatcher;
@Autowired
private PrepareRequestInvoker prepareRequestInvoker;
@Autowired
private EventHandler eventHandler;
@Override
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();
}
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论