X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2FAsyncFreenetInterfaceTest.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2FAsyncFreenetInterfaceTest.kt;h=915a4b550326f02a65d0d2914784069e69394b61;hb=85f599866faef4142c1700702af4aa57ee9daaf8;hp=0000000000000000000000000000000000000000;hpb=76f50d5fcdd6bf662f5776ee9a13ae014dde92f1;p=Sone.git 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 index 0000000..915a4b5 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/AsyncFreenetInterfaceTest.kt @@ -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 . + */ + +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 { + 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() + } + } + } + +}