Search in sources :

Example 16 with ResourceDownloadManager

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

the class ResourceDownloadManagerTest method drainOfferAgainButErrorTest.

@Test
public void drainOfferAgainButErrorTest() throws Exception {
    // given
    ResourceDownloadManager sut = new ResourceDownloadManager();
    InputStream inputStream = mock(InputStream.class);
    OutputStream outputStream = mock(OutputStream.class);
    // when
    AtomicBoolean finished = new AtomicBoolean();
    sut.drainAsync(inputStream, outputStream, response -> {
        finished.set(true);
        throw new RuntimeException("just test");
    });
    // then
    while (!finished.get()) {
        EzyThreads.sleep(3);
    }
    sut.stop();
    sut.destroy();
    verify(inputStream, times(1)).read(any());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 17 with ResourceDownloadManager

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

the class ResourceDownloadManagerTest method drainFailedDueToMaxResourceUploadCapacity.

@Test
public void drainFailedDueToMaxResourceUploadCapacity() throws Exception {
    // given
    info("start drainFailedDueToMaxResourceUploadCapacity");
    ResourceDownloadManager sut = new ResourceDownloadManager(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(MaxResourceDownloadCapacity.class);
    sut.stop();
    info("finsihed drainFailedDueToMaxResourceUploadCapacity");
}
Also used : ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 18 with ResourceDownloadManager

use of com.tvd12.ezyhttp.core.resources.ResourceDownloadManager 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 19 with ResourceDownloadManager

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

the class FileUploadService method download.

public void download(RequestArguments requestArguments, String file) throws Exception {
    ResourceRequestHandler handler = new ResourceRequestHandler("files/" + file, "files/" + file, EzyFileUtil.getFileExtension(file), resourceDownloadManager);
    handler.handle(requestArguments);
}
Also used : ResourceRequestHandler(com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler)

Aggregations

ResourceDownloadManager (com.tvd12.ezyhttp.core.resources.ResourceDownloadManager)18 Test (org.testng.annotations.Test)17 BaseTest (com.tvd12.test.base.BaseTest)10 ResourceRequestHandler (com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler)5 ResourceResolver (com.tvd12.ezyhttp.server.core.resources.ResourceResolver)5 EzyBeanContext (com.tvd12.ezyfox.bean.EzyBeanContext)4 ApplicationContext (com.tvd12.ezyhttp.server.core.ApplicationContext)4 RequestArguments (com.tvd12.ezyhttp.server.core.request.RequestArguments)3 UserService (com.tvd12.ezyhttp.server.core.test.service.UserService)3 ViewContextBuilder (com.tvd12.ezyhttp.server.core.view.ViewContextBuilder)3 AsyncContext (javax.servlet.AsyncContext)3 ServletOutputStream (javax.servlet.ServletOutputStream)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 ApplicationContextBuilder (com.tvd12.ezyhttp.server.core.ApplicationContextBuilder)2 EzyHttpApplication (com.tvd12.ezyhttp.server.core.EzyHttpApplication)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 EzyFutureMap (com.tvd12.ezyfox.concurrent.EzyFutureMap)1 EzyResultCallback (com.tvd12.ezyfox.concurrent.callback.EzyResultCallback)1 EzyAnywayInputStreamLoader (com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader)1 RequestHandler (com.tvd12.ezyhttp.server.core.handler.RequestHandler)1