@ -16,10 +16,13 @@
@@ -16,10 +16,13 @@
package org.springframework.test.web.servlet ;
import java.util.concurrent.CountDownLatch ;
import java.util.concurrent.TimeUnit ;
import java.util.concurrent.atomic.AtomicReference ;
import org.springframework.mock.web.MockHttpServletRequest ;
import org.springframework.mock.web.MockHttpServletResponse ;
import org.springframework.util.Assert ;
import org.springframework.web.servlet.FlashMap ;
import org.springframework.web.servlet.HandlerInterceptor ;
import org.springframework.web.servlet.ModelAndView ;
@ -51,6 +54,8 @@ class DefaultMvcResult implements MvcResult {
@@ -51,6 +54,8 @@ class DefaultMvcResult implements MvcResult {
private final AtomicReference < Object > asyncResult = new AtomicReference < Object > ( RESULT_NONE ) ;
private CountDownLatch asyncDispatchLatch ;
/ * *
* Create a new instance with the given request and response .
@ -126,27 +131,31 @@ class DefaultMvcResult implements MvcResult {
@@ -126,27 +131,31 @@ class DefaultMvcResult implements MvcResult {
if ( this . mockRequest . getAsyncContext ( ) ! = null ) {
timeToWait = ( timeToWait = = - 1 ? this . mockRequest . getAsyncContext ( ) . getTimeout ( ) : timeToWait ) ;
}
if ( timeToWait > 0 ) {
long endTime = System . currentTimeMillis ( ) + timeToWait ;
while ( System . currentTimeMillis ( ) < endTime & & this . asyncResult . get ( ) = = RESULT_NONE ) {
try {
Thread . sleep ( 100 ) ;
}
catch ( InterruptedException ex ) {
Thread . currentThread ( ) . interrupt ( ) ;
throw new IllegalStateException ( "Interrupted while waiting for " +
"async result to be set for handler [" + this . handler + "]" , ex ) ;
}
}
if ( ! awaitAsyncDispatch ( timeToWait ) ) {
throw new IllegalStateException ( "Async result for handler [" + this . handler + "]" +
" was not set during the specified timeToWait=" + timeToWait ) ;
}
Object result = this . asyncResult . get ( ) ;
if ( result = = RESULT_NONE ) {
throw new IllegalStateException ( "Async result for handler [" + this . handler + "] " +
"was not set during the specified timeToWait=" + timeToWait ) ;
Assert . state ( result ! = RESULT_NONE , "Async result for handler [" + this . handler + "] was not set" ) ;
return this . asyncResult . get ( ) ;
}
/ * *
* True if is there a latch was not set , or the latch count reached 0 .
* /
private boolean awaitAsyncDispatch ( long timeout ) {
Assert . state ( this . asyncDispatchLatch ! = null ,
"The asynDispatch CountDownLatch was not set by the TestDispatcherServlet.\n" ) ;
try {
return this . asyncDispatchLatch . await ( timeout , TimeUnit . MILLISECONDS ) ;
}
catch ( InterruptedException e ) {
return false ;
}
return result ;
}
void setAsyncDispatchLatch ( CountDownLatch asyncDispatchLatch ) {
this . asyncDispatchLatch = asyncDispatchLatch ;
}
}