Polish AsyncTests

This commit is contained in:
Sam Brannen 2015-07-03 17:01:21 +02:00
parent 05f163b37d
commit 0e70630ac4
1 changed files with 39 additions and 51 deletions

View File

@ -13,23 +13,18 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.test.web.servlet.samples.standalone; package org.springframework.test.web.servlet.samples.standalone;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import java.io.StringWriter;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.Person; import org.springframework.test.web.Person;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.MvcResult;
@ -37,31 +32,32 @@ import org.springframework.ui.Model;
import org.springframework.util.concurrent.ListenableFuture; import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureTask; import org.springframework.util.concurrent.ListenableFutureTask;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult; import org.springframework.web.context.request.async.DeferredResult;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
/** /**
* Tests with asynchronous request handling. * Tests with asynchronous request handling.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @author Sam Brannen
*/ */
public class AsyncTests { public class AsyncTests {
private MockMvc mockMvc; private final AsyncController asyncController = new AsyncController();
private AsyncController asyncController; private final MockMvc mockMvc = standaloneSetup(this.asyncController).build();
@Before
public void setup() {
this.asyncController = new AsyncController();
this.mockMvc = standaloneSetup(this.asyncController).build();
}
@Test @Test
public void testCallable() throws Exception { public void callable() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("callable", "true")) MvcResult mvcResult = this.mockMvc.perform(get("/1").param("callable", "true"))
.andExpect(request().asyncStarted()) .andExpect(request().asyncStarted())
.andExpect(request().asyncResult(new Person("Joe"))) .andExpect(request().asyncResult(new Person("Joe")))
@ -74,7 +70,7 @@ public class AsyncTests {
} }
@Test @Test
public void testDeferredResult() throws Exception { public void deferredResult() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResult", "true")) MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResult", "true"))
.andExpect(request().asyncStarted()) .andExpect(request().asyncStarted())
.andReturn(); .andReturn();
@ -88,7 +84,7 @@ public class AsyncTests {
} }
@Test @Test
public void testDeferredResultWithSetValue() throws Exception { public void deferredResultWithSetValue() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithSetValue", "true")) MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithSetValue", "true"))
.andExpect(request().asyncStarted()) .andExpect(request().asyncStarted())
.andExpect(request().asyncResult(new Person("Joe"))) .andExpect(request().asyncResult(new Person("Joe")))
@ -101,7 +97,7 @@ public class AsyncTests {
} }
@Test @Test
public void testListenableFuture() throws Exception { public void listenableFuture() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("listenableFuture", "true")) MvcResult mvcResult = this.mockMvc.perform(get("/1").param("listenableFuture", "true"))
.andExpect(request().asyncStarted()) .andExpect(request().asyncStarted())
.andReturn(); .andReturn();
@ -115,7 +111,7 @@ public class AsyncTests {
} }
@Test // SPR-12597 @Test // SPR-12597
public void testCompletableFuture() throws Exception { public void completableFuture() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("completableFuture", "true")) MvcResult mvcResult = this.mockMvc.perform(get("/1").param("completableFuture", "true"))
.andExpect(request().asyncStarted()) .andExpect(request().asyncStarted())
.andReturn(); .andReturn();
@ -129,75 +125,67 @@ public class AsyncTests {
} }
@Test // SPR-12735 @Test // SPR-12735
public void testPrintAsyncResult() throws Exception { public void printAsyncResult() throws Exception {
StringWriter writer = new StringWriter();
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResult", "true")) MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResult", "true"))
.andDo(print()) .andDo(print(writer))
.andExpect(request().asyncStarted()) .andExpect(request().asyncStarted())
.andReturn(); .andReturn();
assertTrue(writer.toString().contains("Async started = true"));
writer = new StringWriter();
this.asyncController.onMessage("Joe"); this.asyncController.onMessage("Joe");
this.mockMvc.perform(asyncDispatch(mvcResult)) this.mockMvc.perform(asyncDispatch(mvcResult))
.andDo(print()) .andDo(print(writer))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
assertTrue(writer.toString().contains("Async started = false"));
} }
@RestController
@Controller @RequestMapping(path = "/{id}", produces = "application/json")
private static class AsyncController { private static class AsyncController {
private Collection<DeferredResult<Person>> deferredResults = private final Collection<DeferredResult<Person>> deferredResults =
new CopyOnWriteArrayList<DeferredResult<Person>>(); new CopyOnWriteArrayList<DeferredResult<Person>>();
private Collection<ListenableFutureTask<Person>> futureTasks = private final Collection<ListenableFutureTask<Person>> futureTasks =
new CopyOnWriteArrayList<ListenableFutureTask<Person>>(); new CopyOnWriteArrayList<ListenableFutureTask<Person>>();
@RequestMapping(value="/{id}", params="callable", produces="application/json") @RequestMapping(params = "callable")
@ResponseBody
public Callable<Person> getCallable(final Model model) { public Callable<Person> getCallable(final Model model) {
return new Callable<Person>() { return () -> new Person("Joe");
@Override
public Person call() throws Exception {
return new Person("Joe");
}
};
} }
@RequestMapping(value="/{id}", params="deferredResult", produces="application/json") @RequestMapping(params = "deferredResult")
@ResponseBody
public DeferredResult<Person> getDeferredResult() { public DeferredResult<Person> getDeferredResult() {
DeferredResult<Person> deferredResult = new DeferredResult<Person>(); DeferredResult<Person> deferredResult = new DeferredResult<Person>();
this.deferredResults.add(deferredResult); this.deferredResults.add(deferredResult);
return deferredResult; return deferredResult;
} }
@RequestMapping(value="/{id}", params="deferredResultWithSetValue", produces="application/json") @RequestMapping(params = "deferredResultWithSetValue")
@ResponseBody
public DeferredResult<Person> getDeferredResultWithSetValue() { public DeferredResult<Person> getDeferredResultWithSetValue() {
DeferredResult<Person> deferredResult = new DeferredResult<Person>(); DeferredResult<Person> deferredResult = new DeferredResult<Person>();
deferredResult.setResult(new Person("Joe")); deferredResult.setResult(new Person("Joe"));
return deferredResult; return deferredResult;
} }
@RequestMapping(value="/{id}", params="listenableFuture", produces="application/json") @RequestMapping(params = "listenableFuture")
@ResponseBody
public ListenableFuture<Person> getListenableFuture() { public ListenableFuture<Person> getListenableFuture() {
ListenableFutureTask<Person> futureTask = new ListenableFutureTask<Person>(new Callable<Person>() { ListenableFutureTask<Person> futureTask = new ListenableFutureTask<Person>(() -> new Person("Joe"));
@Override
public Person call() throws Exception {
return new Person("Joe");
}
});
this.futureTasks.add(futureTask); this.futureTasks.add(futureTask);
return futureTask; return futureTask;
} }
@RequestMapping(value="/{id}", params="completableFuture", produces="application/json") @RequestMapping(params = "completableFuture")
@ResponseBody
public CompletableFuture<Person> getCompletableFuture() { public CompletableFuture<Person> getCompletableFuture() {
CompletableFuture<Person> future = new CompletableFuture<Person>(); CompletableFuture<Person> future = new CompletableFuture<Person>();
future.complete(new Person("Joe")); future.complete(new Person("Joe"));