🔀 Merge other next branch
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 7 Aug 2019 06:15:08 +0000 (08:15 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 7 Aug 2019 06:15:08 +0000 (08:15 +0200)
build.gradle
src/main/kotlin/net/pterodactylus/sone/freenet/AsyncFreenetInterface.kt [new file with mode: 0644]
src/main/kotlin/net/pterodactylus/sone/freenet/FreenetClient.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/freenet/AsyncFreenetInterfaceTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/freenet/FreenetClientTest.kt [new file with mode: 0644]

index a0861a6..9505ab6 100644 (file)
@@ -44,6 +44,8 @@ dependencies {
     provided group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.54'
 
     compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8'
+    compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.3.0-RC'
+
     compile group: 'net.pterodactylus', name: 'utils', version: '0.12.4'
     compile group: 'com.google.inject', name: 'guice', version: '4.2.2'
     compile group: 'com.google.guava', name: 'guava', version: '27.0.1-android'
diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/AsyncFreenetInterface.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/AsyncFreenetInterface.kt
new file mode 100644 (file)
index 0000000..8219d7e
--- /dev/null
@@ -0,0 +1,27 @@
+package net.pterodactylus.sone.freenet
+
+import freenet.client.*
+import freenet.keys.*
+import kotlinx.coroutines.*
+import net.pterodactylus.sone.core.*
+
+class AsyncFreenetInterface(private val freenetClient: FreenetClient) {
+
+       suspend fun fetchUri(freenetUri: FreenetURI): Fetched {
+               var currentUri = freenetUri
+               var result: FetchResult? = null
+               while (result == null) {
+                       try {
+                               result = withContext(Dispatchers.Default) { freenetClient.fetch(currentUri) }
+                       } catch (fetchException: FetchException) {
+                               if (fetchException.mode == FetchException.FetchExceptionMode.PERMANENT_REDIRECT) {
+                                       currentUri = fetchException.newURI
+                                       continue
+                               } else
+                                       throw fetchException
+                       }
+               }
+               return Fetched(currentUri, result)
+       }
+
+}
diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/FreenetClient.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/FreenetClient.kt
new file mode 100644 (file)
index 0000000..8fbbe57
--- /dev/null
@@ -0,0 +1,20 @@
+package net.pterodactylus.sone.freenet
+
+import freenet.client.*
+import freenet.keys.*
+
+/**
+ * Facade for Freenet’s [freenet.client.HighLevelSimpleClient] to allow testing.
+ */
+interface FreenetClient {
+
+       fun fetch(freenetKey: FreenetURI): FetchResult
+
+}
+
+class DefaultFreenetClient(private val highLevelSimpleClient: HighLevelSimpleClient) : FreenetClient {
+
+       override fun fetch(freenetKey: FreenetURI): FetchResult =
+                       highLevelSimpleClient.fetch(freenetKey)
+
+}
diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/AsyncFreenetInterfaceTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/AsyncFreenetInterfaceTest.kt
new file mode 100644 (file)
index 0000000..915a4b5
--- /dev/null
@@ -0,0 +1,80 @@
+/**
+ * Sone - AsyncFreenetInterfaceTest.kt - Copyright Â© 2019 David â€˜Bombe’ Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet
+
+import freenet.client.*
+import freenet.keys.*
+import freenet.support.io.*
+import kotlinx.coroutines.*
+import net.pterodactylus.sone.core.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import java.util.concurrent.atomic.*
+import kotlin.test.*
+
+class AsyncFreenetInterfaceTest {
+
+       @Test
+       fun `returned deferred is completed by success`() = runBlocking {
+               val result = FetchResult(ClientMetadata(), NullBucket())
+               val freenetClient = object : FreenetClient {
+                       override fun fetch(freenetKey: FreenetURI) = result
+               }
+               val freenetInterface = AsyncFreenetInterface(freenetClient)
+               val fetched = async { freenetInterface.fetchUri(FreenetURI("KSK@GPL.txt")) }
+
+               withTimeout(1000) {
+                       assertThat(fetched.await(), equalTo(Fetched(FreenetURI("KSK@GPL.txt"), result)))
+               }
+       }
+
+       @Test
+       fun `permanent redircts are being followed`() = runBlocking {
+               val result = FetchResult(ClientMetadata(), NullBucket())
+               val freenetClient = object : FreenetClient {
+                       val redirected = AtomicBoolean(false)
+                       override fun fetch(freenetKey: FreenetURI) =
+                                       if (redirected.compareAndSet(false, true))
+                                               throw FetchException(FetchException.FetchExceptionMode.PERMANENT_REDIRECT, FreenetURI("KSK@GPLv3.txt"))
+                                       else result
+               }
+               val freenetInterface = AsyncFreenetInterface(freenetClient)
+               val fetched = async { freenetInterface.fetchUri(FreenetURI("KSK@GPL.txt")) }
+
+               withTimeout(1000) {
+                       assertThat(fetched.await(), equalTo(Fetched(FreenetURI("KSK@GPLv3.txt"), result)))
+               }
+       }
+
+       @Test
+       fun `fetch errors are being re-thrown`() = runBlocking<Unit> {
+               val freenetClient = object : FreenetClient {
+                       override fun fetch(freenetKey: FreenetURI) =
+                                       throw FetchException(FetchException.FetchExceptionMode.ALL_DATA_NOT_FOUND)
+               }
+               val freenetInterface = AsyncFreenetInterface(freenetClient)
+               val fetched = supervisorScope { async { freenetInterface.fetchUri(FreenetURI("KSK@GPL.txt")) } }
+
+               withTimeout(1000) {
+                       assertFailsWith(FetchException::class) {
+                               fetched.await()
+                       }
+               }
+       }
+
+}
diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/FreenetClientTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/FreenetClientTest.kt
new file mode 100644 (file)
index 0000000..f6d5883
--- /dev/null
@@ -0,0 +1,23 @@
+package net.pterodactylus.sone.freenet
+
+import freenet.client.*
+import freenet.keys.*
+import freenet.support.io.*
+import net.pterodactylus.sone.test.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import kotlin.test.*
+
+class FreenetClientTest {
+
+       private val highLevelSimpleClient = mock<HighLevelSimpleClient>()
+       private val freenetClient = DefaultFreenetClient(highLevelSimpleClient)
+
+       @Test
+       fun `fetch method calls method on hlsc`() {
+               val fetchResult = FetchResult(ClientMetadata(), NullBucket())
+               whenever(highLevelSimpleClient.fetch(FreenetURI("KSK@GPL.txt"))).thenReturn(fetchResult)
+               assertThat(freenetClient.fetch(FreenetURI("KSK@GPL.txt")), equalTo(fetchResult))
+       }
+
+}