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