Java Code Examples for org.springframework.test.web.servlet.MvcResult#getAsyncResult()

The following examples show how to use org.springframework.test.web.servlet.MvcResult#getAsyncResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestAsyncService.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@Test
public void testController () throws Exception {

    MvcResult result = mockMvc.perform(get("/callSelectDept/359.json"))
   .andExpect(request().asyncStarted())
   .andReturn();
    
    result.getRequest().getAsyncContext().setTimeout(5000);
    result.getAsyncResult();
    
    result= mockMvc.perform(asyncDispatch(result))
        .andExpect(status().isOk())
   	.andExpect(header().string("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE))
   	.andReturn();
   
    System.out.println(result.getResponse().getContentAsString());
    
}
 
Example 2
Source File: TestAsyncService.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@Test
public void testController () throws Exception {

    MvcResult result = mockMvc.perform(get("/callSelectDept/359.json"))
   .andExpect(request().asyncStarted())
   .andReturn();
    
    result.getRequest().getAsyncContext().setTimeout(5000);
    result.getAsyncResult();
    
    result= mockMvc.perform(asyncDispatch(result))
        .andExpect(status().isOk())
   	.andExpect(header().string("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE))
   	.andReturn();
   
    System.out.println(result.getResponse().getContentAsString());
    
}
 
Example 3
Source File: RoutingSlipControllerTest.java    From blog-non-blocking-rest-service-with-spring-mvc with Apache License 2.0 6 votes vote down vote up
/**
     * Same test as testRoutingSlipNonBlockingLambda that ensures that no state is kept between calls to the routing-slip-non-blocking-lambda service
     *
     * @throws Exception
     */
    @Test
    public void testRoutingSlipNonBlockingLambda2() throws Exception {

        MvcResult mvcResult = this.mockMvc.perform(get("/routing-slip-non-blocking-lambda"))
                .andExpect(request().asyncStarted())
//            .andExpect(request().asyncResult(expectedResult))
                .andReturn();

        mvcResult.getAsyncResult();

        this.mockMvc.perform(asyncDispatch(mvcResult))
                .andExpect(status().isOk())
                .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
                .andExpect(content().string(expectedResult));
    }
 
Example 4
Source File: ProcessingControllerTest.java    From blog-non-blocking-rest-service-with-spring-mvc with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessNonBlocking() throws Exception {

    MvcResult mvcResult = this.mockMvc.perform(get("/process-non-blocking?minMs=2000&maxMs=2000"))
            .andExpect(request().asyncStarted())
            .andReturn();

    mvcResult.getAsyncResult();

    this.mockMvc.perform(asyncDispatch(mvcResult))
            .andExpect(status().isOk())
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(content().string(expectedResult));
}
 
Example 5
Source File: AggregatorControllerTest.java    From blog-non-blocking-rest-service-with-spring-mvc with Apache License 2.0 5 votes vote down vote up
@Test
public void testAggregatorNonBlockingLambdaTimeout() throws Exception {

    int minMs = (TIMEOUT_MS < 1000) ? 0 : TIMEOUT_MS - 1000;
    int maxMs = TIMEOUT_MS + 1000;
    int dbHits = 10;

    MvcResult mvcResult = this.mockMvc.perform(get("/aggregate-non-blocking-lambda?dbHits=" + dbHits + "&minMs=" + minMs + "&maxMs=" + maxMs))
            .andExpect(request().asyncStarted())
            .andReturn();

    mvcResult.getAsyncResult();

    this.mockMvc.perform(asyncDispatch(mvcResult))
            .andExpect(status().isOk())
            .andExpect(content().contentType("text/plain;charset=ISO-8859-1"));

    String result = mvcResult.getAsyncResult().toString();

    System.err.println("JSON: " + result);
    String[] psArr = result.split("\n");

    // Verify that we got some timeouts
    assertTrue("Expected at least one timeout to occur", psArr.length < dbHits);

    System.err.println("assert that no response time was over the timeout: " + TIMEOUT_MS);
    ObjectMapper mapper = new ObjectMapper();
    for (int i = 0; i < psArr.length; i++) {
        ProcessingStatus ps = mapper.readValue(psArr[i], ProcessingStatus.class);
        System.err.println("psArr: " + ps.getStatus() + " - " + ps.getProcessingTimeMs());
        assertTrue(ps.getProcessingTimeMs() < TIMEOUT_MS);
    }
}
 
Example 6
Source File: AggregatorControllerTest.java    From blog-non-blocking-rest-service-with-spring-mvc with Apache License 2.0 5 votes vote down vote up
@Test
public void testAggregatorNonBlockingLambda() throws Exception {

    MvcResult mvcResult = this.mockMvc.perform(get("/aggregate-non-blocking-lambda?minMs=2000&maxMs=2000"))
            .andExpect(request().asyncStarted())
            .andReturn();

    mvcResult.getAsyncResult();

    this.mockMvc.perform(asyncDispatch(mvcResult))
            .andExpect(status().isOk())
            .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
            .andExpect(content().string(expectedResult));
}
 
Example 7
Source File: AggregatorControllerTest.java    From blog-non-blocking-rest-service-with-spring-mvc with Apache License 2.0 5 votes vote down vote up
@Test
public void testAggregatorNonBlockingCallbackTimeout() throws Exception {

    int minMs = (TIMEOUT_MS < 1000) ? 0 : TIMEOUT_MS - 1000;
    int maxMs = TIMEOUT_MS + 1000;
    int dbHits = 10;

    MvcResult mvcResult = this.mockMvc.perform(get("/aggregate-non-blocking-callback?dbHits=" + dbHits + "&minMs=" + minMs + "&maxMs=" + maxMs))
            .andExpect(request().asyncStarted())
            .andReturn();

    mvcResult.getAsyncResult();

    this.mockMvc.perform(asyncDispatch(mvcResult))
            .andExpect(status().isOk())
            .andExpect(content().contentType("text/plain;charset=ISO-8859-1"));

    String result = mvcResult.getAsyncResult().toString();

    System.err.println("JSON: " + result);
    String[] psArr = result.split("\n");

    // Verify that we got some timeouts
    assertTrue("Expected at least one timeout to occur", psArr.length < dbHits);

    System.err.println("assert that no response time was over the timeout: " + TIMEOUT_MS);
    ObjectMapper mapper = new ObjectMapper();
    for (int i = 0; i < psArr.length; i++) {
        ProcessingStatus ps = mapper.readValue(psArr[i], ProcessingStatus.class);
        System.err.println("psArr: " + ps.getStatus() + " - " + ps.getProcessingTimeMs());
        assertTrue(ps.getProcessingTimeMs() < TIMEOUT_MS);
    }
}
 
Example 8
Source File: AggregatorControllerTest.java    From blog-non-blocking-rest-service-with-spring-mvc with Apache License 2.0 5 votes vote down vote up
@Test
public void testAggregatorNonBlockingCallback() throws Exception {

    MvcResult mvcResult = this.mockMvc.perform(get("/aggregate-non-blocking-callback?minMs=2000&maxMs=2000"))
            .andExpect(request().asyncStarted())
            .andReturn();

    mvcResult.getAsyncResult();

    this.mockMvc.perform(asyncDispatch(mvcResult))
            .andExpect(status().isOk())
            .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
            .andExpect(content().string(expectedResult));
}
 
Example 9
Source File: RouterControllerTest.java    From blog-non-blocking-rest-service-with-spring-mvc with Apache License 2.0 5 votes vote down vote up
private void testNonBlocking(String url) throws Exception {
    MvcResult mvcResult = this.mockMvc.perform(get(url))
            .andExpect(request().asyncStarted())
            .andReturn();

    mvcResult.getAsyncResult();

    this.mockMvc.perform(asyncDispatch(mvcResult))
            .andExpect(status().isOk())
            .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
            .andExpect(content().string(expectedResult));
}
 
Example 10
Source File: RoutingSlipControllerTest.java    From blog-non-blocking-rest-service-with-spring-mvc with Apache License 2.0 5 votes vote down vote up
@Test
    public void testRoutingSlipNonBlockingLambda() throws Exception {

        MvcResult mvcResult = this.mockMvc.perform(get("/routing-slip-non-blocking-lambda"))
                .andExpect(request().asyncStarted())
//            .andExpect(request().asyncResult(expectedResult))
                .andReturn();

        mvcResult.getAsyncResult();

        this.mockMvc.perform(asyncDispatch(mvcResult))
                .andExpect(status().isOk())
                .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
                .andExpect(content().string(expectedResult));
    }
 
Example 11
Source File: RoutingSlipControllerTest.java    From blog-non-blocking-rest-service-with-spring-mvc with Apache License 2.0 5 votes vote down vote up
@Test
    public void testRoutingSlipNonBlockingStateMachine() throws Exception {

        MvcResult mvcResult = this.mockMvc.perform(get("/routing-slip-non-blocking-state-machine"))
                .andExpect(request().asyncStarted())
//            .andExpect(request().asyncResult(expectedResult))
                .andReturn();

        mvcResult.getAsyncResult();

        this.mockMvc.perform(asyncDispatch(mvcResult))
                .andExpect(status().isOk())
                .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
                .andExpect(content().string(expectedResult));
    }
 
Example 12
Source File: RestTemplateTraceAspectIntegrationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
private void whenARequestIsSentToAnAsyncEndpoint(String url) throws Exception {
	MvcResult mvcResult = this.mockMvc
			.perform(MockMvcRequestBuilders.get(url).accept(MediaType.TEXT_PLAIN))
			.andExpect(request().asyncStarted()).andReturn();
	mvcResult.getAsyncResult(SECONDS.toMillis(2));
	this.mockMvc.perform(asyncDispatch(mvcResult)).andDo(print())
			.andExpect(status().isOk());
}
 
Example 13
Source File: PrintingResultHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected void printAsyncResult(MvcResult result) throws Exception {
	HttpServletRequest request = result.getRequest();
	this.printer.printValue("Async started", request.isAsyncStarted());
	Object asyncResult = null;
	try {
		asyncResult = result.getAsyncResult(0);
	}
	catch (IllegalStateException ex) {
		// Not set
	}
	this.printer.printValue("Async result", asyncResult);
}
 
Example 14
Source File: PrintingResultHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected void printAsyncResult(MvcResult result) throws Exception {
	HttpServletRequest request = result.getRequest();
	this.printer.printValue("Async started", request.isAsyncStarted());
	Object asyncResult = null;
	try {
		asyncResult = result.getAsyncResult(0);
	}
	catch (IllegalStateException ex) {
		// Not set
	}
	this.printer.printValue("Async result", asyncResult);
}
 
Example 15
Source File: PrintingResultHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected void printAsyncResult(MvcResult result) throws Exception {
	HttpServletRequest request = result.getRequest();
	this.printer.printValue("Async started", request.isAsyncStarted());
	Object asyncResult = null;
	try {
		asyncResult = result.getAsyncResult(0);
	}
	catch (IllegalStateException ex) {
		// Not set
	}
	this.printer.printValue("Async result", asyncResult);
}
 
Example 16
Source File: MockMvcRequestBuilders.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Create a {@link RequestBuilder} for an async dispatch from the
 * {@link MvcResult} of the request that started async processing.
 * <p>Usage involves performing a request that starts async processing first:
 * <pre class="code">
 * MvcResult mvcResult = this.mockMvc.perform(get("/1"))
 *	.andExpect(request().asyncStarted())
 *	.andReturn();
 *  </pre>
 * <p>And then performing the async dispatch re-using the {@code MvcResult}:
 * <pre class="code">
 * this.mockMvc.perform(asyncDispatch(mvcResult))
 * 	.andExpect(status().isOk())
 * 	.andExpect(content().contentType(MediaType.APPLICATION_JSON))
 * 	.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
 * </pre>
 * @param mvcResult the result from the request that started async processing
 */
public static RequestBuilder asyncDispatch(final MvcResult mvcResult) {

	// There must be an async result before dispatching
	mvcResult.getAsyncResult();

	return new RequestBuilder() {
		@Override
		public MockHttpServletRequest buildRequest(ServletContext servletContext) {
			MockHttpServletRequest request = mvcResult.getRequest();
			request.setDispatcherType(DispatcherType.ASYNC);
			request.setAsyncStarted(false);
			return request;
		}
	};
}
 
Example 17
Source File: MockMvcRequestBuilders.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Create a {@link RequestBuilder} for an async dispatch from the
 * {@link MvcResult} of the request that started async processing.
 * <p>Usage involves performing a request that starts async processing first:
 * <pre class="code">
 * MvcResult mvcResult = this.mockMvc.perform(get("/1"))
 *	.andExpect(request().asyncStarted())
 *	.andReturn();
 *  </pre>
 * <p>And then performing the async dispatch re-using the {@code MvcResult}:
 * <pre class="code">
 * this.mockMvc.perform(asyncDispatch(mvcResult))
 * 	.andExpect(status().isOk())
 * 	.andExpect(content().contentType(MediaType.APPLICATION_JSON))
 * 	.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
 * </pre>
 * @param mvcResult the result from the request that started async processing
 */
public static RequestBuilder asyncDispatch(final MvcResult mvcResult) {

	// There must be an async result before dispatching
	mvcResult.getAsyncResult();

	return servletContext -> {
		MockHttpServletRequest request = mvcResult.getRequest();
		request.setDispatcherType(DispatcherType.ASYNC);
		request.setAsyncStarted(false);
		return request;
	};
}
 
Example 18
Source File: MockMvcRequestBuilders.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Create a {@link RequestBuilder} for an async dispatch from the
 * {@link MvcResult} of the request that started async processing.
 * <p>Usage involves performing a request that starts async processing first:
 * <pre class="code">
 * MvcResult mvcResult = this.mockMvc.perform(get("/1"))
 *	.andExpect(request().asyncStarted())
 *	.andReturn();
 *  </pre>
 * <p>And then performing the async dispatch re-using the {@code MvcResult}:
 * <pre class="code">
 * this.mockMvc.perform(asyncDispatch(mvcResult))
 * 	.andExpect(status().isOk())
 * 	.andExpect(content().contentType(MediaType.APPLICATION_JSON))
 * 	.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
 * </pre>
 * @param mvcResult the result from the request that started async processing
 */
public static RequestBuilder asyncDispatch(final MvcResult mvcResult) {

	// There must be an async result before dispatching
	mvcResult.getAsyncResult();

	return servletContext -> {
		MockHttpServletRequest request = mvcResult.getRequest();
		request.setDispatcherType(DispatcherType.ASYNC);
		request.setAsyncStarted(false);
		return request;
	};
}