From b7f0567e2a3e88f2327dfdb045a1b0e4bca9dcac Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 5 Nov 2019 19:46:12 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Add=20way=20to=20start=20fetch=20whi?= =?utf8?q?le=20skipping=20one=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/core/SoneRescuer.java | 9 ++++++++ .../net/pterodactylus/sone/core/SoneRescuerTest.kt | 27 ++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) 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 } -- 2.7.4