wip on crawler and tests

This commit is contained in:
Zlatin Balevsky
2018-07-11 14:24:04 +01:00
parent b4c42a05a2
commit c6b32b81df
2 changed files with 57 additions and 5 deletions

View File

@@ -6,14 +6,16 @@ class Crawler {
final def pinger
final def hostPool
final int parallel
final Map<Destination, Host> 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)
}
}
}

View File

@@ -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)
hostPool = hostPoolMock.proxyInstance()
}
@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()
}
}