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