Search in sources :

Example 6 with ResourceUploadManager

use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.

the class ResourceUploadManagerTest method drainOfferAgainGetMaxQueueCapacityAndFutureNullTest.

@Test
public void drainOfferAgainGetMaxQueueCapacityAndFutureNullTest() throws Exception {
    // given
    ResourceUploadManager sut = new ResourceUploadManager(1, 1, 1);
    InputStream inputStream = mock(InputStream.class);
    when(inputStream.read(any())).thenReturn(1);
    OutputStream outputStream = mock(OutputStream.class);
    EzyFutureMap<?> futureMap = FieldUtil.getFieldValue(sut, "futureMap");
    // when
    for (int i = 0; i < 1000; ++i) {
        try {
            sut.drainAsync(inputStream, outputStream, response -> {
            });
        } catch (Exception ignored) {
        }
        futureMap.clear();
    }
    // then
    sut.stop();
    sut.destroy();
    verify(inputStream, atLeastOnce()).read(any());
    verify(outputStream, atLeastOnce()).write(any(), anyInt(), anyInt());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException) IOException(java.io.IOException) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 7 with ResourceUploadManager

use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.

the class ResourceUploadManagerTest method drainButFutureNull.

@SuppressWarnings("rawtypes")
@Test
public void drainButFutureNull() throws Exception {
    // given
    ResourceUploadManager sut = new ResourceUploadManager(100, 1, 1024);
    EzyFutureMap futureMap = FieldUtil.getFieldValue(sut, "futureMap");
    InputStream inputStream = mock(InputStream.class);
    when(inputStream.read(any(byte[].class))).thenAnswer(it -> {
        Thread.sleep(200);
        return 0;
    });
    OutputStream outputStream = mock(OutputStream.class);
    // when
    sut.drainAsync(inputStream, outputStream, it -> {
    });
    futureMap.clear();
    // then
    Thread.sleep(300);
    sut.stop();
}
Also used : EzyFutureMap(com.tvd12.ezyfox.concurrent.EzyFutureMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 8 with ResourceUploadManager

use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.

the class ResourceUploadManagerTest method drainWithMaxUploadSizeTest.

@Test
public void drainWithMaxUploadSizeTest() throws Exception {
    // given
    ResourceUploadManager sut = new ResourceUploadManager();
    int size = ResourceUploadManager.DEFAULT_BUFFER_SIZE * 3;
    byte[] inputBytes = RandomUtil.randomByteArray(size);
    InputStream inputStream = new ByteArrayInputStream(inputBytes);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(size);
    // when
    sut.drain(inputStream, outputStream, Integer.MAX_VALUE);
    // then
    byte[] outputBytes = outputStream.toByteArray();
    Asserts.assertEquals(inputBytes, outputBytes);
    sut.stop();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 9 with ResourceUploadManager

use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.

the class ResourceUploadManagerTest method drainTest.

@Test
public void drainTest() throws Exception {
    // given
    ResourceUploadManager sut = new ResourceUploadManager();
    int size = ResourceUploadManager.DEFAULT_BUFFER_SIZE * 3;
    byte[] inputBytes = RandomUtil.randomByteArray(size);
    InputStream inputStream = new ByteArrayInputStream(inputBytes);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(size);
    // when
    sut.drain(inputStream, outputStream);
    // then
    byte[] outputBytes = outputStream.toByteArray();
    Asserts.assertEquals(inputBytes, outputBytes);
    sut.stop();
    sut.destroy();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 10 with ResourceUploadManager

use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.

the class FileUploaderTest method acceptFirstFailed.

@SuppressWarnings("unchecked")
@Test
public void acceptFirstFailed() throws Exception {
    // given
    AsyncContext asyncContext = mock(AsyncContext.class);
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getRequestURI()).thenReturn("hello-world");
    when(asyncContext.getRequest()).thenReturn(request);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(asyncContext.getResponse()).thenReturn(response);
    Part part = mock(Part.class);
    File outputFile = new File("test-output/files");
    EzyExceptionVoid callback = mock(EzyExceptionVoid.class);
    ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
    doAnswer(it -> {
        EzyResultCallback<Boolean> cb = it.getArgumentAt(3, EzyResultCallback.class);
        cb.onException(new Exception("just test"));
        return null;
    }).when(resourceUploadManager).drainAsync(any(), any(), any(long.class), any());
    FileUploader sut = new FileUploader(resourceUploadManager);
    // when
    sut.accept(asyncContext, part, outputFile, callback);
    // then
    verify(callback, times(0)).apply();
    verify(resourceUploadManager, times(1)).drainAsync(any(), any(), any(long.class), any());
    verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
    verify(request, times(1)).getRequestURI();
    verify(asyncContext, times(1)).getRequest();
    verify(asyncContext, times(1)).complete();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) EzyExceptionVoid(com.tvd12.ezyfox.function.EzyExceptionVoid) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) Part(javax.servlet.http.Part) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) AsyncContext(javax.servlet.AsyncContext) File(java.io.File) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Aggregations

ResourceUploadManager (com.tvd12.ezyhttp.core.resources.ResourceUploadManager)20 Test (org.testng.annotations.Test)19 InputStream (java.io.InputStream)14 BaseTest (com.tvd12.test.base.BaseTest)12 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 OutputStream (java.io.OutputStream)9 FileUploader (com.tvd12.ezyhttp.server.core.resources.FileUploader)7 AsyncContext (javax.servlet.AsyncContext)7 File (java.io.File)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 Part (javax.servlet.http.Part)5 EzyExceptionVoid (com.tvd12.ezyfox.function.EzyExceptionVoid)4 MaxUploadSizeException (com.tvd12.ezyhttp.core.exception.MaxUploadSizeException)4 IOException (java.io.IOException)4 FileUploadCallback (com.tvd12.ezyhttp.server.core.resources.FileUploadCallback)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 EzyFutureMap (com.tvd12.ezyfox.concurrent.EzyFutureMap)1