diff --git a/host-cache/src/main/groovy/com/muwire/hostcache/Crawler.groovy b/host-cache/src/main/groovy/com/muwire/hostcache/Crawler.groovy index c4c9c004..aa280bbb 100644 --- a/host-cache/src/main/groovy/com/muwire/hostcache/Crawler.groovy +++ b/host-cache/src/main/groovy/com/muwire/hostcache/Crawler.groovy @@ -6,14 +6,16 @@ class Crawler { final def pinger final def hostPool + final int parallel final Map inFlight = new HashMap<>() UUID currentUUID - Crawler(pinger, hostPool) { + Crawler(pinger, hostPool, int parallel) { this.pinger = pinger this.hostPool = hostPool + this.parallel = parallel } synchronized def handleCrawlerPong(pong, Destination source) { @@ -61,4 +63,16 @@ class Crawler { private static boolean parseBoolean(value) { return Boolean.parseBoolean(value.toString()) } + + synchronized def startCrawl() { + if (!inFlight.isEmpty()) { + inFlight.values().each { hostPool.fail(it) } + inFlight.clear() + } + currentUUID = UUID.randomUUID() + hostPool.getUnverified(parallel).each { + inFlight.put(it.destination, it) + pinger.ping(it, currentUUID) + } + } } diff --git a/host-cache/src/test/groovy/com/muwire/hostcache/CrawlerTest.groovy b/host-cache/src/test/groovy/com/muwire/hostcache/CrawlerTest.groovy index 7298db32..6b407346 100644 --- a/host-cache/src/test/groovy/com/muwire/hostcache/CrawlerTest.groovy +++ b/host-cache/src/test/groovy/com/muwire/hostcache/CrawlerTest.groovy @@ -1,5 +1,6 @@ package com.muwire.hostcache +import org.junit.After import org.junit.Before import org.junit.Test @@ -17,21 +18,58 @@ class CrawlerTest { def crawler + final Host host = new Host(destination: new Destination()) + + final int parallel = 5 + @Before void before() { pingerMock = new MockFor(Pinger) - pinger = pingerMock.proxyInstance() - hostPoolMock = new MockFor(HostPool) + } + + @After + void after() { + hostPoolMock.verify hostPool + pingerMock.verify pinger + } + + private def initCrawler() { + pinger = pingerMock.proxyInstance() hostPool = hostPoolMock.proxyInstance() + crawler = new Crawler(pinger, hostPool, parallel) - crawler = new Crawler(pinger, hostPool) } @Test void testBadJson() { + initCrawler() def unpingedHost = new Host(destination : new Destination()) crawler.handleCrawlerPong(null, new Destination()) - hostPoolMock.verify hostPool + } + + @Test + void testStartCrawl() { + hostPoolMock.demand.getUnverified { n -> + assert n == parallel + [host] + } + pingerMock.demand.ping { h,uuid -> assert h == host } + + initCrawler() + crawler.startCrawl() + + } + + @Test + void testFailsUnanswered() { + hostPoolMock.demand.getUnverified {n -> [host]} + hostPoolMock.demand.fail { h -> assert h == host } + hostPoolMock.demand.getUnverified {n -> [:]} + pingerMock.demand.ping {h,uuid -> } + initCrawler() + + crawler.startCrawl() + crawler.startCrawl() } }