2 * Sone - AsyncFreenetInterfaceTest.kt - Copyright © 2019–2020 David ‘Bombe’ Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.freenet
20 import freenet.client.*
22 import freenet.support.io.*
23 import kotlinx.coroutines.*
24 import net.pterodactylus.sone.core.*
25 import org.hamcrest.MatcherAssert.*
26 import org.hamcrest.Matchers.*
27 import java.util.concurrent.atomic.*
30 class AsyncFreenetInterfaceTest {
33 fun `returned deferred is completed by success`() = runBlocking {
34 val result = FetchResult(ClientMetadata(), NullBucket())
35 val freenetClient = object : FreenetClient {
36 override fun fetch(freenetKey: FreenetURI) = result
38 val freenetInterface = AsyncFreenetInterface(freenetClient)
39 val fetched = async { freenetInterface.fetchUri(FreenetURI("KSK@GPL.txt")) }
42 assertThat(fetched.await(), equalTo(Fetched(FreenetURI("KSK@GPL.txt"), result)))
47 fun `permanent redircts are being followed`() = runBlocking {
48 val result = FetchResult(ClientMetadata(), NullBucket())
49 val freenetClient = object : FreenetClient {
50 val redirected = AtomicBoolean(false)
51 override fun fetch(freenetKey: FreenetURI) =
52 if (redirected.compareAndSet(false, true))
53 throw FetchException(FetchException.FetchExceptionMode.PERMANENT_REDIRECT, FreenetURI("KSK@GPLv3.txt"))
56 val freenetInterface = AsyncFreenetInterface(freenetClient)
57 val fetched = async { freenetInterface.fetchUri(FreenetURI("KSK@GPL.txt")) }
60 assertThat(fetched.await(), equalTo(Fetched(FreenetURI("KSK@GPLv3.txt"), result)))
65 fun `fetch errors are being re-thrown`() = runBlocking<Unit> {
66 val freenetClient = object : FreenetClient {
67 override fun fetch(freenetKey: FreenetURI) =
68 throw FetchException(FetchException.FetchExceptionMode.ALL_DATA_NOT_FOUND)
70 val freenetInterface = AsyncFreenetInterface(freenetClient)
71 val fetched = supervisorScope { async { freenetInterface.fetchUri(FreenetURI("KSK@GPL.txt")) } }
74 assertFailsWith(FetchException::class) {