Removed time condition on HostBalancer initialization in JUnit test.

Its initialization in main application usage remains asynchronous.
pull/157/head
luccioman 7 years ago
parent 8b572b7337
commit 46b5249c20

@ -71,10 +71,31 @@ public class HostBalancer implements Balancer {
private final Set<String> roundRobinHostHashes; private final Set<String> roundRobinHostHashes;
private final int onDemandLimit; private final int onDemandLimit;
/**
* Create a new instance and asynchronously fills the queue by scanning the hostsPath directory.
* @param hostsPath path with persisted hosts queues
* @param onDemandLimit
* @param exceed134217727
*/
public HostBalancer( public HostBalancer(
final File hostsPath, final File hostsPath,
final int onDemandLimit, final int onDemandLimit,
final boolean exceed134217727) { final boolean exceed134217727) {
this(hostsPath, onDemandLimit, exceed134217727, true);
}
/**
* Create a new instance and fills the queue by scanning the hostsPath directory.
* @param hostsPath
* @param onDemandLimit
* @param exceed134217727
* @param asyncInit when true, queue filling from file system is launched asynchronously
*/
public HostBalancer(
final File hostsPath,
final int onDemandLimit,
final boolean exceed134217727,
final boolean asyncInit) {
this.hostsPath = hostsPath; this.hostsPath = hostsPath;
this.onDemandLimit = onDemandLimit; this.onDemandLimit = onDemandLimit;
this.exceed134217727 = exceed134217727; this.exceed134217727 = exceed134217727;
@ -83,17 +104,33 @@ public class HostBalancer implements Balancer {
if (!(hostsPath.exists())) hostsPath.mkdirs(); // make the path if (!(hostsPath.exists())) hostsPath.mkdirs(); // make the path
this.queues = new ConcurrentHashMap<String, HostQueue>(); this.queues = new ConcurrentHashMap<String, HostQueue>();
this.roundRobinHostHashes = new HashSet<String>(); this.roundRobinHostHashes = new HashSet<String>();
init(); // return without wait but starts a thread to fill the queues init(asyncInit); // return without wait but starts a thread to fill the queues
} }
/** /**
* fills the queue by scanning the hostsPath directory in a thread to * Fills the queue by scanning the hostsPath directory.
* @param async when true, launch in a dedicated thread to
* return immediately (as large unfinished crawls may take longer to load) * return immediately (as large unfinished crawls may take longer to load)
*/ */
private void init() { private void init(final boolean async) {
if(async) {
Thread t = new Thread("HostBalancer.init") { Thread t = new Thread("HostBalancer.init") {
@Override @Override
public void run() { public void run() {
runInit();
}
};
t.start();
} else {
runInit();
}
}
/**
* Fills the queue by scanning the hostsPath directory.
*/
private void runInit() {
final String[] hostlist = hostsPath.list(); final String[] hostlist = hostsPath.list();
for (String hoststr : hostlist) { for (String hoststr : hostlist) {
try { try {
@ -112,10 +149,6 @@ public class HostBalancer implements Balancer {
} }
} }
} }
};
t.start();
}
@Override @Override
public synchronized void close() { public synchronized void close() {
@ -196,6 +229,11 @@ public class HostBalancer implements Balancer {
return c; return c;
} }
/**
* @return true when the URL is queued is this or any other HostBalancer
* instance (as {@link #depthCache} is shared between all HostBalancer
* instances)
*/
@Override @Override
public boolean has(final byte[] urlhashb) { public boolean has(final byte[] urlhashb) {
if (depthCache.has(urlhashb)) return true; if (depthCache.has(urlhashb)) return true;

@ -8,7 +8,8 @@ import net.yacy.cora.util.SpaceExceededException;
import net.yacy.crawler.retrieval.Request; import net.yacy.crawler.retrieval.Request;
import net.yacy.crawler.robots.RobotsTxt; import net.yacy.crawler.robots.RobotsTxt;
import net.yacy.data.WorkTables; import net.yacy.data.WorkTables;
import static net.yacy.kelondro.util.FileUtils.deletedelete; import net.yacy.kelondro.util.FileUtils;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -17,14 +18,15 @@ public class HostBalancerTest {
final File queuesRoot = new File("test/DATA/INDEX/QUEUES"); final File queuesRoot = new File("test/DATA/INDEX/QUEUES");
final File datadir = new File("test/DATA"); final File datadir = new File("test/DATA");
private static final boolean EXCEED_134217727 = true;
private static final int ON_DEMAND_LIMIT = 1000;
/** /**
* Test of reopen existing HostBalancer cache to test/demonstrate issue with * Test of reopen existing HostBalancer cache to test/demonstrate issue with
* HostQueue for file: protocol * HostQueue for file: protocol
*/ */
@Test @Test
public void testReopen() throws IOException, SpaceExceededException, InterruptedException { public void testReopen() throws IOException, SpaceExceededException, InterruptedException {
boolean exceed134217727 = true;
int onDemandLimit = 1000;
String hostDir = "C:\\filedirectory"; String hostDir = "C:\\filedirectory";
// prepare one urls for push test // prepare one urls for push test
@ -32,10 +34,9 @@ public class HostBalancerTest {
DigestURL url = new DigestURL(urlstr); DigestURL url = new DigestURL(urlstr);
Request req = new Request(url, null); Request req = new Request(url, null);
deletedelete(queuesRoot); // start clean test FileUtils.deletedelete(queuesRoot); // start clean test
HostBalancer hb = new HostBalancer(queuesRoot, onDemandLimit, exceed134217727); HostBalancer hb = new HostBalancer(queuesRoot, ON_DEMAND_LIMIT, EXCEED_134217727, false);
Thread.sleep(100); // wait for file operation
hb.clear(); hb.clear();
Thread.sleep(100); Thread.sleep(100);
@ -58,8 +59,7 @@ public class HostBalancerTest {
Thread.sleep(200); // wait a bit for file operation Thread.sleep(200); // wait a bit for file operation
hb = new HostBalancer(queuesRoot, onDemandLimit, exceed134217727); // reopen balancer hb = new HostBalancer(queuesRoot, ON_DEMAND_LIMIT, EXCEED_134217727, false); // reopen balancer
Thread.sleep(200); // wait a bit for file operation
assertEquals("size after reopen (with one existing url)", 1, hb.size()); // expect size=1 from previous push assertEquals("size after reopen (with one existing url)", 1, hb.size()); // expect size=1 from previous push
assertTrue("check existance of pushed url", hb.has(url.hash())); // check url exists (it fails as after reopen internal queue.hosthash is wrong) assertTrue("check existance of pushed url", hb.has(url.hash())); // check url exists (it fails as after reopen internal queue.hosthash is wrong)

Loading…
Cancel
Save