gpt4 book ai didi

spring-mvc - 如何测试 DeferredResult timeoutResult

转载 作者:行者123 更新时间:2023-12-03 22:29:50 24 4
gpt4 key购买 nike

我正在实现 long polling as per the Spring blog from some time ago .

这里我转换的方法与以前具有相同的响应签名,但不是立即响应,它现在使用长轮询:

private Map<String, DeferredResult<ResponseEntity<?>>> requests = new ConcurrentHashMap<>();

@RequestMapping(value = "/{uuid}", method = RequestMethod.GET)
public DeferredResult<ResponseEntity<?>> poll(@PathVariable("uuid") final String uuid) {
// Create & store a new instance
ResponseEntity<?> pendingOnTimeout = ResponseEntity.accepted().build();
DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>(TWENTYFIVE_SECONDS, pendingOnTimeout);
requests.put(uuid, deferredResult);

// Clean up poll requests when done
deferredResult.onCompletion(() -> {
requests.remove(deferredResult);
});

// Set result if already available
Task task = taskHolder.retrieve(uuid);
if (task == null)
deferredResult.setResult(ResponseEntity.status(HttpStatus.GONE).build());
else
// Done (or canceled): Redirect to retrieve file contents
if (task.getFutureFile().isDone())
deferredResult.setResult(ResponseEntity.created(RetrieveController.uri(uuid)).build());

// Return result
return deferredResult;
}

我特别想返回 pendingOnTimeout当请求时间过长时响应(我之前立即返回),以防止代理切断请求。

现在我想我已经按原样工作了,但我想编写一个单元测试来确认这一点。然而,我使用 MockMvc(通过 webAppContextSetup)的所有尝试都未能为我提供一种断言我得到 accepted 的方法。标题。例如,当我尝试以下操作时:
@Test
public void pollPending() throws Exception {
MvcResult result = mockMvc.perform(get("/poll/{uuid}", uuidPending)).andReturn();
mockMvc.perform(asyncDispatch(result))
.andExpect(status().isAccepted());
}

我得到以下堆栈跟踪:

java.lang.IllegalStateException: Async result for handler [public org.springframework.web.context.request.async.DeferredResult> nl.bioprodict.blast.api.PollController.poll(java.lang.String)] was not set during the specified timeToWait=25000 at org.springframework.util.Assert.state(Assert.java:392) at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:143) at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:120) at org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch(MockMvcRequestBuilders.java:235) at nl.bioprodict.blast.docs.PollControllerDocumentation.pollPending(PollControllerDocumentation.java:53) ...



我发现所有与此相关的 Spring 框架测试似乎都使用了模拟: https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/test/java/org/springframework/web/context/request/async/WebAsyncManagerTimeoutTests.java

如何测试 DeferredResult timeoutResult 的正确处理?

最佳答案

在我的情况下,经过 spring 源代码并设置超时(10000 毫秒)并获得异步结果后,为我解决了这个问题,因为;

 mvcResult.getRequest().getAsyncContext().setTimeout(10000);
mvcResult.getAsyncResult();

我的整个测试代码是;
MvcResult mvcResult = this.mockMvc.perform(
post("<SOME_RELATIVE_URL>")
.contentType(MediaType.APPLICATION_JSON)
.content(<JSON_DATA>))
***.andExpect(request().asyncStarted())***
.andReturn();

***mvcResult.getRequest().getAsyncContext().setTimeout(10000);***
***mvcResult.getAsyncResult();***

this.mockMvc
.perform(asyncDispatch(mvcResult))
.andDo(print())
.andExpect(status().isOk());

希望能帮助到你..

关于spring-mvc - 如何测试 DeferredResult timeoutResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34343231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com