X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneInserterTest.kt;h=d2bb3b49c95d1c1f5ce91dc2b9361a3e97ba2e37;hp=e54191572da3d30122e35041ef7d98770b8df96a;hb=aa83028a17a61b8a2103475fae4624fbfe4c9ca5;hpb=607feeb5ec4a5c6d75562456bdf0a10f8ad12ea8 diff --git a/src/test/kotlin/net/pterodactylus/sone/core/SoneInserterTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/SoneInserterTest.kt index e541915..d2bb3b4 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/SoneInserterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/SoneInserterTest.kt @@ -1,5 +1,6 @@ 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.* @@ -26,12 +27,14 @@ import org.mockito.hamcrest.MockitoHamcrest.* 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() private val eventBus = mock() private val freenetInterface = mock() @@ -46,7 +49,7 @@ class SoneInserterTest { @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)) } @@ -64,27 +67,27 @@ class SoneInserterTest { fun `isModified is true if modification detector says so`() { val soneModificationDetector = mock() 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() - 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() } @@ -97,7 +100,7 @@ class SoneInserterTest { val soneModificationDetector = mock() whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true) whenever(freenetInterface.insertDirectory(eq(insertUri), 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 @@ -119,7 +122,7 @@ class SoneInserterTest { val sone = createSone(insertUri) val soneModificationDetector = mock() 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>(), eq("index.html"))).thenAnswer { soneInserter.stop() finalUri @@ -140,7 +143,7 @@ class SoneInserterTest { val insertUri = mock() createSone(insertUri) val soneModificationDetector = mock() - 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) @@ -161,7 +164,7 @@ class SoneInserterTest { val sone = createSone(insertUri) val soneModificationDetector = mock() 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>(), eq("index.html"))).thenAnswer { soneInserter.stop() @@ -181,7 +184,7 @@ class SoneInserterTest { @Test fun `sone inserter exits if sone is unknown`() { val soneModificationDetector = mock() - 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() @@ -190,7 +193,7 @@ class SoneInserterTest { @Test fun `sone inserter catches exception and continues`() { val soneModificationDetector = mock() - val soneInserter = SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1) + val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1) val stopInserterAndThrowException = Answer> { soneInserter.stop() throw NullPointerException() @@ -236,4 +239,38 @@ class SoneInserterTest { nullValue()) } + @Test + fun `successful insert updates metrics`() { + val insertUri = mock() + val finalUri = mock() + createSone(insertUri) + val soneModificationDetector = mock() + whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true) + whenever(freenetInterface.insertDirectory(eq(insertUri), any>(), eq("index.html"))).thenReturn(finalUri) + val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry,"SoneId", soneModificationDetector, 1) + doAnswer { + soneInserter.stop() + null + }.`when`(core).touchConfiguration() + soneInserter.serviceRun() + val histogram = metricRegistry.histogram("sone.insert.duration") + assertThat(histogram.count, equalTo(1L)) + } + + @Test + fun `unsuccessful insert does not update metrics`() { + val insertUri = mock() + createSone(insertUri) + val soneModificationDetector = mock() + whenever(soneModificationDetector.isEligibleForInsert).thenReturn(true) + val soneInserter = SoneInserter(core, eventBus, freenetInterface, metricRegistry, "SoneId", soneModificationDetector, 1) + whenever(freenetInterface.insertDirectory(eq(insertUri), any>(), eq("index.html"))).thenAnswer { + soneInserter.stop() + throw SoneException(Exception()) + } + soneInserter.serviceRun() + val histogram = metricRegistry.histogram("sone.insert.duration") + assertThat(histogram.count, equalTo(0L)) + } + }