package net.pterodactylus.sone.core
+import com.codahale.metrics.*
import com.google.common.base.*
import com.google.common.base.Optional
import com.google.common.eventbus.*
import org.mockito.stubbing.*
import java.lang.System.*
import java.util.*
+import kotlin.test.Test
/**
* Unit test for [SoneInserter] and its subclasses.
*/
class SoneInserterTest {
+ private val metricRegistry = MetricRegistry()
private val core = mock<Core>()
private val eventBus = mock<EventBus>()
private val freenetInterface = mock<FreenetInterface>()
@Test
fun `insertion delay is forwarded to sone inserter`() {
val eventBus = AsyncEventBus(directExecutor())
- eventBus.register(SoneInserter(core, eventBus, freenetInterface, "SoneId"))
+ eventBus.register(SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId"))
eventBus.post(InsertionDelayChangedEvent(15))
assertThat(SoneInserter.getInsertionDelay().get(), equalTo(15))
}
fun `isModified is true if modification detector says so`() {
val soneModificationDetector = mock<SoneModificationDetector>()
whenever(soneModificationDetector.isModified).thenReturn(true)
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
assertThat(soneInserter.isModified, equalTo(true))
}
@Test
fun `isModified is false if modification detector says so`() {
val soneModificationDetector = mock<SoneModificationDetector>()
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
assertThat(soneInserter.isModified, equalTo(false))
}
@Test
fun `last fingerprint is stored correctly`() {
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId")
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId")
soneInserter.lastInsertFingerprint = "last-fingerprint"
assertThat(soneInserter.lastInsertFingerprint, equalTo("last-fingerprint"))
}
@Test
fun `sone inserter stops when it should`() {
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId")
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId")
soneInserter.stop()
soneInserter.serviceRun()
}
val soneModificationDetector = mock<SoneModificationDetector>()
whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenReturn(finalUri)
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
doAnswer {
soneInserter.stop()
null
- }.`when`(core).touchConfiguration()
+ }.whenever(core).touchConfiguration()
soneInserter.serviceRun()
val soneEvents = ArgumentCaptor.forClass(SoneEvent::class.java)
verify(freenetInterface).insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))
val sone = createSone(insertUri)
val soneModificationDetector = mock<SoneModificationDetector>()
whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
soneInserter.stop()
finalUri
val insertUri = mock<FreenetURI>()
createSone(insertUri)
val soneModificationDetector = mock<SoneModificationDetector>()
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
Thread(Runnable {
try {
Thread.sleep(500)
val sone = createSone(insertUri)
val soneModificationDetector = mock<SoneModificationDetector>()
whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
val soneException = SoneException(Exception())
whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
soneInserter.stop()
@Test
fun `sone inserter exits if sone is unknown`() {
val soneModificationDetector = mock<SoneModificationDetector>()
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
whenever(core.getSone("SoneId")).thenReturn(null)
soneInserter.serviceRun()
@Test
fun `sone inserter catches exception and continues`() {
val soneModificationDetector = mock<SoneModificationDetector>()
- val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
val stopInserterAndThrowException = Answer<Optional<Sone>> {
soneInserter.stop()
throw NullPointerException()
nullValue())
}
+ @Test
+ fun `successful insert updates metrics`() {
+ val insertUri = mock<FreenetURI>()
+ val finalUri = mock<FreenetURI>()
+ createSone(insertUri)
+ val soneModificationDetector = mock<SoneModificationDetector>()
+ whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
+ whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenReturn(finalUri)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry,"SoneId", soneModificationDetector, 1)
+ doAnswer {
+ soneInserter.stop()
+ null
+ }.whenever(core).touchConfiguration()
+ soneInserter.serviceRun()
+ val histogram = metricRegistry.histogram("sone.insert.duration")
+ assertThat(histogram.count, equalTo(1L))
+ }
+
+ @Test
+ fun `unsuccessful insert does not update histogram but records error`() {
+ val insertUri = mock<FreenetURI>()
+ createSone(insertUri)
+ val soneModificationDetector = mock<SoneModificationDetector>()
+ whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true)
+ val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1)
+ whenever(freenetInterface.insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))).thenAnswer {
+ soneInserter.stop()
+ throw SoneException(Exception())
+ }
+ soneInserter.serviceRun()
+ val histogram = metricRegistry.histogram("sone.insert.duration")
+ assertThat(histogram.count, equalTo(0L))
+ val meter = metricRegistry.meter("sone.insert.errors")
+ assertThat(meter.count, equalTo(1L))
+ }
+
}