Search in sources :

Example 6 with EzyResultCallback

use of com.tvd12.ezyfox.concurrent.callback.EzyResultCallback in project ezyhttp by youngmonkeys.

the class ResourceDownloadManager method drainAsync.

public void drainAsync(InputStream from, OutputStream to, EzyResultCallback<Boolean> callback) {
    Entry entry = new Entry(from, to);
    EzyCallableFutureTask future = new EzyCallableFutureTask(callback);
    drain(entry, future);
}
Also used : EzyCallableFutureTask(com.tvd12.ezyfox.concurrent.EzyCallableFutureTask)

Example 7 with EzyResultCallback

use of com.tvd12.ezyfox.concurrent.callback.EzyResultCallback in project ezyhttp by youngmonkeys.

the class ResourceDownloadManagerTest method drainOfferAgainGetMaxQueueCapacityTest.

@Test
public void drainOfferAgainGetMaxQueueCapacityTest() throws Exception {
    // given
    ResourceDownloadManager sut = new ResourceDownloadManager(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(), MaxResourceDownloadCapacity.class);
    sut.stop();
    sut.destroy();
    verify(inputStream, atLeastOnce()).read(any());
    verify(outputStream, atLeastOnce()).write(any(), anyInt(), anyInt());
}
Also used : ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 8 with EzyResultCallback

use of com.tvd12.ezyfox.concurrent.callback.EzyResultCallback 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)

Aggregations

Test (org.testng.annotations.Test)6 ResourceUploadManager (com.tvd12.ezyhttp.core.resources.ResourceUploadManager)5 AsyncContext (javax.servlet.AsyncContext)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 EzyExceptionVoid (com.tvd12.ezyfox.function.EzyExceptionVoid)4 FileUploader (com.tvd12.ezyhttp.server.core.resources.FileUploader)4 File (java.io.File)4 Part (javax.servlet.http.Part)4 MaxUploadSizeException (com.tvd12.ezyhttp.core.exception.MaxUploadSizeException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 BaseTest (com.tvd12.test.base.BaseTest)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 EzyCallableFutureTask (com.tvd12.ezyfox.concurrent.EzyCallableFutureTask)1 EzyProcessor.processWithLogException (com.tvd12.ezyfox.util.EzyProcessor.processWithLogException)1 ResourceDownloadManager (com.tvd12.ezyhttp.core.resources.ResourceDownloadManager)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1