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");
}
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");
}
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();
}
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");
}
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());
}
Aggregations