use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.
the class ResourceUploadManagerTest method drainOfferAgainGetMaxQueueCapacityAndFutureNullTest.
@Test
public void drainOfferAgainGetMaxQueueCapacityAndFutureNullTest() 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);
EzyFutureMap<?> futureMap = FieldUtil.getFieldValue(sut, "futureMap");
// when
for (int i = 0; i < 1000; ++i) {
try {
sut.drainAsync(inputStream, outputStream, response -> {
});
} catch (Exception ignored) {
}
futureMap.clear();
}
// then
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 drainButFutureNull.
@SuppressWarnings("rawtypes")
@Test
public void drainButFutureNull() throws Exception {
// given
ResourceUploadManager sut = new ResourceUploadManager(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();
}
use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.
the class ResourceUploadManagerTest method drainWithMaxUploadSizeTest.
@Test
public void drainWithMaxUploadSizeTest() throws Exception {
// 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
sut.drain(inputStream, outputStream, Integer.MAX_VALUE);
// then
byte[] outputBytes = outputStream.toByteArray();
Asserts.assertEquals(inputBytes, outputBytes);
sut.stop();
}
use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.
the class ResourceUploadManagerTest method drainTest.
@Test
public void drainTest() throws Exception {
// 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
sut.drain(inputStream, outputStream);
// then
byte[] outputBytes = outputStream.toByteArray();
Asserts.assertEquals(inputBytes, outputBytes);
sut.stop();
sut.destroy();
}
use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.
the class FileUploaderTest method acceptFirstFailed.
@SuppressWarnings("unchecked")
@Test
public void acceptFirstFailed() throws Exception {
// given
AsyncContext asyncContext = mock(AsyncContext.class);
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getRequestURI()).thenReturn("hello-world");
when(asyncContext.getRequest()).thenReturn(request);
HttpServletResponse response = mock(HttpServletResponse.class);
when(asyncContext.getResponse()).thenReturn(response);
Part part = mock(Part.class);
File outputFile = new File("test-output/files");
EzyExceptionVoid callback = mock(EzyExceptionVoid.class);
ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
doAnswer(it -> {
EzyResultCallback<Boolean> cb = it.getArgumentAt(3, EzyResultCallback.class);
cb.onException(new Exception("just test"));
return null;
}).when(resourceUploadManager).drainAsync(any(), any(), any(long.class), any());
FileUploader sut = new FileUploader(resourceUploadManager);
// when
sut.accept(asyncContext, part, outputFile, callback);
// then
verify(callback, times(0)).apply();
verify(resourceUploadManager, times(1)).drainAsync(any(), any(), any(long.class), any());
verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
verify(request, times(1)).getRequestURI();
verify(asyncContext, times(1)).getRequest();
verify(asyncContext, times(1)).complete();
}
Aggregations