forked from Qortal/qortal
Added test for bootstrap random host selection.
This commit is contained in:
parent
2f6a8f793b
commit
8d6dffb3ff
@ -368,10 +368,7 @@ public class Bootstrap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void downloadToPath(Path path) throws DataException {
|
private void downloadToPath(Path path) throws DataException {
|
||||||
// Select a random host from bootstrapHosts
|
String bootstrapHost = this.getRandomHost();
|
||||||
String[] hosts = Settings.getInstance().getBootstrapHosts();
|
|
||||||
int index = new SecureRandom().nextInt(hosts.length);
|
|
||||||
String bootstrapHost = hosts[index];
|
|
||||||
String bootstrapFilename = this.getFilename();
|
String bootstrapFilename = this.getFilename();
|
||||||
String bootstrapUrl = String.format("%s/%s", bootstrapHost, bootstrapFilename);
|
String bootstrapUrl = String.format("%s/%s", bootstrapHost, bootstrapFilename);
|
||||||
|
|
||||||
@ -420,6 +417,14 @@ public class Bootstrap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getRandomHost() {
|
||||||
|
// Select a random host from bootstrapHosts
|
||||||
|
String[] hosts = Settings.getInstance().getBootstrapHosts();
|
||||||
|
int index = new SecureRandom().nextInt(hosts.length);
|
||||||
|
String bootstrapHost = hosts[index];
|
||||||
|
return bootstrapHost;
|
||||||
|
}
|
||||||
|
|
||||||
public void importFromPath(Path path) throws InterruptedException, DataException, IOException {
|
public void importFromPath(Path path) throws InterruptedException, DataException, IOException {
|
||||||
|
|
||||||
ReentrantLock blockchainLock = Controller.getInstance().getBlockchainLock();
|
ReentrantLock blockchainLock = Controller.getInstance().getBlockchainLock();
|
||||||
|
@ -22,6 +22,7 @@ import java.nio.file.NoSuchFileException;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
@ -181,6 +182,28 @@ public class BootstrapTests extends Common {
|
|||||||
repository.saveChanges();
|
repository.saveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRandomHost() {
|
||||||
|
String[] bootstrapHosts = Settings.getInstance().getBootstrapHosts();
|
||||||
|
List<String> uniqueHosts = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int i=0; i<1000; i++) {
|
||||||
|
Bootstrap bootstrap = new Bootstrap();
|
||||||
|
String randomHost = bootstrap.getRandomHost();
|
||||||
|
assertNotNull(randomHost);
|
||||||
|
|
||||||
|
if (!uniqueHosts.contains(randomHost)){
|
||||||
|
uniqueHosts.add(randomHost);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure we have more than one bootstrap host in the settings
|
||||||
|
assertTrue(Arrays.asList(bootstrapHosts).size() > 1);
|
||||||
|
|
||||||
|
// Ensure that all have been given the opportunity to be used
|
||||||
|
assertEquals(uniqueHosts.size(), Arrays.asList(bootstrapHosts).size());
|
||||||
|
}
|
||||||
|
|
||||||
private void deleteBootstraps() throws IOException {
|
private void deleteBootstraps() throws IOException {
|
||||||
try {
|
try {
|
||||||
Path path = Paths.get(String.format("%s%s", Settings.getInstance().getBootstrapFilenamePrefix(), "bootstrap-archive.7z"));
|
Path path = Paths.get(String.format("%s%s", Settings.getInstance().getBootstrapFilenamePrefix(), "bootstrap-archive.7z"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user