Added test for bootstrap random host selection.

This commit is contained in:
CalDescent 2021-10-06 18:23:17 +01:00
parent 2f6a8f793b
commit 8d6dffb3ff
2 changed files with 32 additions and 4 deletions

View File

@ -368,10 +368,7 @@ public class Bootstrap {
}
private void downloadToPath(Path path) throws DataException {
// Select a random host from bootstrapHosts
String[] hosts = Settings.getInstance().getBootstrapHosts();
int index = new SecureRandom().nextInt(hosts.length);
String bootstrapHost = hosts[index];
String bootstrapHost = this.getRandomHost();
String bootstrapFilename = this.getFilename();
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 {
ReentrantLock blockchainLock = Controller.getInstance().getBlockchainLock();

View File

@ -22,6 +22,7 @@ import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
@ -181,6 +182,28 @@ public class BootstrapTests extends Common {
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 {
try {
Path path = Paths.get(String.format("%s%s", Settings.getInstance().getBootstrapFilenamePrefix(), "bootstrap-archive.7z"));