From: David ‘Bombe’ Roden Date: Tue, 5 Nov 2019 18:46:12 +0000 (+0100) Subject: ✨ Add way to start fetch while skipping one version X-Git-Tag: v81^2~54 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b7f0567e2a3e88f2327dfdb045a1b0e4bca9dcac;p=Sone.git ✨ Add way to start fetch while skipping one version --- diff --git a/src/main/java/net/pterodactylus/sone/core/SoneRescuer.java b/src/main/java/net/pterodactylus/sone/core/SoneRescuer.java index adc0925..6045127 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneRescuer.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneRescuer.java @@ -130,6 +130,15 @@ public class SoneRescuer extends AbstractService { notifySyncObject(); } + /** + * Starts the next fetch. + */ + public void startNextFetchWithSkip() { + currentEdition--; + fetching = true; + notifySyncObject(); + } + // // SERVICE METHODS // diff --git a/src/test/kotlin/net/pterodactylus/sone/core/SoneRescuerTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/SoneRescuerTest.kt index af8a402..1fd2553 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/SoneRescuerTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/SoneRescuerTest.kt @@ -5,12 +5,12 @@ import net.pterodactylus.sone.data.* import net.pterodactylus.sone.test.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.equalTo -import org.junit.* import org.mockito.ArgumentMatchers.any import org.mockito.ArgumentMatchers.eq import org.mockito.Mockito.doAnswer import org.mockito.Mockito.never import org.mockito.Mockito.verify +import kotlin.test.Test /** * Unit test for [SoneRescuer]. @@ -67,7 +67,7 @@ class SoneRescuerTest { @Test fun successfulInsert() { val fetchedSone = mock() - returnUriOnInsert(fetchedSone) + returnUriOnInsert(fetchedSone, currentEdition - 1) soneRescuer.startNextFetch() soneRescuer.serviceRun() verify(core).lockSone(eq(sone)) @@ -78,8 +78,21 @@ class SoneRescuerTest { } @Test + fun `starting fetch with skipping one edition skips one edition`() { + val fetchedSone = mock() + returnUriOnInsert(fetchedSone, currentEdition - 2) + soneRescuer.startNextFetchWithSkip() + soneRescuer.serviceRun() + verify(core).lockSone(eq(sone)) + verify(core).updateSone(eq(fetchedSone), eq(true)) + assertThat(soneRescuer.isLastFetchSuccessful, equalTo(true)) + assertThat(soneRescuer.isFetching, equalTo(false)) + assertThat(soneRescuer.currentEdition, equalTo(currentEdition - 2)) + } + + @Test fun nonSuccessfulInsertIsRecognized() { - returnUriOnInsert(null) + returnUriOnInsert(null, (currentEdition - 1)) soneRescuer.startNextFetch() soneRescuer.serviceRun() verify(core).lockSone(eq(sone)) @@ -89,20 +102,20 @@ class SoneRescuerTest { assertThat(soneRescuer.currentEdition, equalTo(currentEdition)) } - private fun returnUriOnInsert(fetchedSone: Sone?) { - val keyWithMetaStrings = setupFreenetUri() + private fun returnUriOnInsert(fetchedSone: Sone?, edition: Long) { + val keyWithMetaStrings = setupFreenetUri(edition) doAnswer { soneRescuer.stop() fetchedSone }.whenever(soneDownloader).fetchSone(eq(sone), eq(keyWithMetaStrings), eq(true)) } - private fun setupFreenetUri(): FreenetURI { + private fun setupFreenetUri(edition: Long): FreenetURI { val sskKey = mock() val keyWithDocName = mock() val keyWithMetaStrings = mock() whenever(keyWithDocName.setMetaString(eq(arrayOf("sone.xml")))).thenReturn(keyWithMetaStrings) - whenever(sskKey.setDocName(eq("Sone-" + (currentEdition - 1)))).thenReturn(keyWithDocName) + whenever(sskKey.setDocName(eq("Sone-" + edition))).thenReturn(keyWithDocName) whenever(sone.requestUri.setKeyType(eq("SSK"))).thenReturn(sskKey) return keyWithMetaStrings }