Search in sources :

Example 6 with ResourceDownloadManager

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

the class ResourceDownloadManagerTest method drainButFutureNull.

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

Example 7 with ResourceDownloadManager

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

the class ResourceDownloadManagerTest method drainTest.

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

Example 8 with ResourceDownloadManager

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

the class ResourceDownloadManagerTest method drainFailedDueToOutputStream.

@Test
public void drainFailedDueToOutputStream() throws Exception {
    // given
    info("start drainFailedDueToOutputStream");
    ResourceDownloadManager sut = new ResourceDownloadManager();
    int size = ResourceDownloadManager.DEFAULT_BUFFER_SIZE * 3;
    byte[] inputBytes = RandomUtil.randomByteArray(size);
    InputStream inputStream = new ByteArrayInputStream(inputBytes);
    OutputStream outputStream = mock(OutputStream.class);
    IOException exception = new IOException("just test");
    doThrow(exception).when(outputStream).write(any(byte[].class), anyInt(), anyInt());
    // when
    Throwable throwable = Asserts.assertThrows(() -> sut.drain(inputStream, outputStream));
    // then
    sut.stop();
    Asserts.assertEqualsType(throwable, IOException.class);
    info("finish drainFailedDueToOutputStream");
    sut.destroy();
}
Also used : ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 9 with ResourceDownloadManager

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

the class ResourceDownloadManagerTest method activeFalse.

@Test
public void activeFalse() throws Exception {
    // given
    info("start activeFalse");
    ResourceDownloadManager sut = new ResourceDownloadManager();
    FieldUtil.setFieldValue(sut, "active", false);
    int size = ResourceDownloadManager.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();
    info("finish activeFalse");
}
Also used : ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 10 with ResourceDownloadManager

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

the class ResourceDownloadManagerTest method drainFailedDueToOutputStreamThrowable.

@Test
public void drainFailedDueToOutputStreamThrowable() throws Exception {
    // given
    ResourceDownloadManager sut = new ResourceDownloadManager();
    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 : ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

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