Search in sources :

Example 1 with DeferredLinesWriter

use of org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer.DeferredLinesWriter in project spring-boot by spring-projects.

the class SpringBootMockMvcBuilderCustomizerTests method whenCalledInParallelDeferredLinesWriterSeparatesOutputByThread.

@Test
void whenCalledInParallelDeferredLinesWriterSeparatesOutputByThread() throws Exception {
    AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
    MockServletContext servletContext = new MockServletContext();
    context.setServletContext(servletContext);
    context.register(ServletConfiguration.class, FilterConfiguration.class);
    context.refresh();
    CapturingLinesWriter delegate = new CapturingLinesWriter();
    new DeferredLinesWriter(context, delegate);
    CountDownLatch latch = new CountDownLatch(10);
    for (int i = 0; i < 10; i++) {
        Thread thread = new Thread(() -> {
            for (int j = 0; j < 1000; j++) {
                DeferredLinesWriter writer = DeferredLinesWriter.get(context);
                writer.write(Arrays.asList("1", "2", "3", "4", "5"));
                writer.writeDeferredResult();
                writer.clear();
            }
            latch.countDown();
        });
        thread.start();
    }
    latch.await(60, TimeUnit.SECONDS);
    assertThat(delegate.allWritten).hasSize(10000);
    assertThat(delegate.allWritten).allSatisfy((written) -> assertThat(written).containsExactly("1", "2", "3", "4", "5"));
}
Also used : DeferredLinesWriter(org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer.DeferredLinesWriter) AnnotationConfigServletWebApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext) CountDownLatch(java.util.concurrent.CountDownLatch) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.jupiter.api.Test)1 DeferredLinesWriter (org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer.DeferredLinesWriter)1 AnnotationConfigServletWebApplicationContext (org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext)1 MockServletContext (org.springframework.mock.web.MockServletContext)1