Search in sources :

Example 16 with ResourceUploadManager

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

the class ResourceUploadManagerTest method drainFailedDueToOverUploadSizeTest.

@Test
public void drainFailedDueToOverUploadSizeTest() {
    // 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
    Throwable e = Asserts.assertThrows(() -> sut.drain(inputStream, outputStream, 1));
    // then
    Asserts.assertEqualsType(e, MaxUploadSizeException.class);
    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 17 with ResourceUploadManager

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

the class ResourceUploadManagerTest method drainFailedDueToOutputStreamThrowable.

@Test
public void drainFailedDueToOutputStreamThrowable() throws Exception {
    // given
    ResourceUploadManager sut = new ResourceUploadManager();
    int size = ResourceUploadManager.DEFAULT_BUFFER_SIZE * 3;
    byte[] inputBytes = RandomUtil.randomByteArray(size);
    InputStream inputStream = new ByteArrayInputStream(inputBytes);
    OutputStream outputStream = mock(OutputStream.class);
    Throwable exception = new StackOverflowError("just test");
    doThrow(exception).when(outputStream).write(any(byte[].class), anyInt(), anyInt());
    // when
    Throwable e = Asserts.assertThrows(() -> sut.drain(inputStream, outputStream));
    // then
    Asserts.assertThat(e.getCause()).isEqualsType(StackOverflowError.class);
    sut.stop();
    verify(outputStream, times(1)).write(any(byte[].class), anyInt(), anyInt());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) 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 18 with ResourceUploadManager

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

the class ResourceUploadManagerTest method drainOfferAgainGetMaxQueueCapacityTest.

@Test
public void drainOfferAgainGetMaxQueueCapacityTest() 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);
    // when
    AtomicReference<Exception> exceptionRef = new AtomicReference<>();
    for (int i = 0; i < 100; ++i) {
        try {
            sut.drainAsync(inputStream, outputStream, new EzyResultCallback<Boolean>() {

                @Override
                public void onResponse(Boolean response) {
                }

                @Override
                public void onException(Exception e) {
                    exceptionRef.set(e);
                }
            });
        } catch (Exception ignored) {
        }
    }
    // then
    while (exceptionRef.get() == null) {
        EzyThreads.sleep(3);
    }
    Asserts.assertEqualsType(exceptionRef.get(), MaxResourceUploadCapacity.class);
    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) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException) IOException(java.io.IOException) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 19 with ResourceUploadManager

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

the class ResourceUploadManagerTest method drainFailedDueToOutputStreamDueToError.

@Test
public void drainFailedDueToOutputStreamDueToError() throws Exception {
    // given
    ResourceUploadManager sut = new ResourceUploadManager();
    BlockingQueue<Object> queue = FieldUtil.getFieldValue(sut, "queue");
    queue.offer(new Object());
    InputStream inputStream = mock(InputStream.class);
    OutputStream outputStream = mock(OutputStream.class);
    // when
    sut.drain(inputStream, outputStream);
    // then
    sut.stop();
}
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) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 20 with ResourceUploadManager

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

the class ResourceUploadManagerTest method drainFailedDueToMaxResourceUploadCapacity.

@Test
public void drainFailedDueToMaxResourceUploadCapacity() throws Exception {
    // given
    ResourceUploadManager sut = new ResourceUploadManager(1, 1, 1024);
    InputStream inputStream = mock(InputStream.class);
    when(inputStream.read(any(byte[].class))).thenReturn(10);
    OutputStream outputStream = mock(OutputStream.class);
    sut.stop();
    Thread.sleep(200);
    // when
    sut.drainAsync(inputStream, outputStream, it -> {
    });
    Throwable e = Asserts.assertThrows(() -> sut.drain(inputStream, outputStream));
    // then
    Asserts.assertThat(e).isEqualsType(MaxResourceUploadCapacity.class);
    sut.stop();
}
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) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

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