Downloader test Ok
This commit is contained in:
@@ -14,7 +14,7 @@ import java.util.concurrent.*;
|
||||
public class DownloaderImpl implements Downloader {
|
||||
|
||||
private static final ExecutorService pool = Executors.newCachedThreadPool();
|
||||
private static final String BASE_DIRECTORY = "/var/opt/fias";
|
||||
private static final String BASE_DIRECTORY = "/tmp/"; //todo path
|
||||
private static final ConcurrentMap<String,CompletableFuture<File>> locks = new ConcurrentHashMap();
|
||||
|
||||
@Override
|
||||
@@ -23,7 +23,9 @@ public class DownloaderImpl implements Downloader {
|
||||
CompletableFuture<File> start = new CompletableFuture<>();
|
||||
|
||||
//syncronized
|
||||
final CompletableFuture<File> feature = locks.putIfAbsent(url, start);
|
||||
final CompletableFuture<File> add = locks.putIfAbsent(url, start);
|
||||
|
||||
final CompletableFuture<File> feature = add!=null ? add : locks.get(url);
|
||||
|
||||
//return earlier runned feature
|
||||
if(feature != start) return feature;
|
||||
@@ -39,6 +41,7 @@ public class DownloaderImpl implements Downloader {
|
||||
path = Paths.get(BASE_DIRECTORY, conn.getFile());
|
||||
|
||||
} catch (IOException e) {
|
||||
//todo log
|
||||
feature.obtrudeException(e);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package me.bearns.fias.webapi;
|
||||
|
||||
import me.bearns.fias.exceptions.CommonException;
|
||||
import me.bearns.fias.exceptions.DownloadException;
|
||||
import me.bearns.fias.service.Updater;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -16,7 +17,7 @@ public class API {
|
||||
void update(){
|
||||
try {
|
||||
service.update();
|
||||
} catch (DownloadException e) {
|
||||
} catch (CommonException e) {
|
||||
//TODO
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@@ -41,17 +42,20 @@ public class DownloaderTest {
|
||||
try {
|
||||
return fileCompletableFuture.get();
|
||||
} catch (Exception e) {
|
||||
//todo log
|
||||
return null; //on error
|
||||
}
|
||||
});
|
||||
|
||||
assert fileStream.allMatch(Objects::nonNull);
|
||||
final List<File> result = fileStream.collect(Collectors.toList());
|
||||
|
||||
assert fileStream.count() == 3;
|
||||
assert result.stream().allMatch(Objects::nonNull);
|
||||
|
||||
assert fileStream.allMatch(File::exists);
|
||||
assert result.size() == list.size();
|
||||
|
||||
assert fileStream.allMatch(file -> file.length() > 0);
|
||||
assert result.stream().allMatch(File::exists);
|
||||
|
||||
assert result.stream().allMatch(file -> file.length() > 0);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user