From: David ‘Bombe’ Roden Date: Wed, 11 Dec 2019 19:42:40 +0000 (+0100) Subject: 🚧 Add web of trust pinger and events X-Git-Tag: v81^2~5^2~39 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ceac45f8b8a241e8f9809d70eeda7897f5e280b7;p=Sone.git 🚧 Add web of trust pinger and events --- diff --git a/src/main/kotlin/net/pterodactylus/sone/core/event/WebOfTrustAppeared.kt b/src/main/kotlin/net/pterodactylus/sone/core/event/WebOfTrustAppeared.kt new file mode 100644 index 0000000..97daff1 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/core/event/WebOfTrustAppeared.kt @@ -0,0 +1,23 @@ +/** + * Sone - WebOfTrustAppeared.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.core.event + +/** + * Event that signals that the web of trust is reachable. + */ +class WebOfTrustAppeared diff --git a/src/main/kotlin/net/pterodactylus/sone/core/event/WebOfTrustDisappeared.kt b/src/main/kotlin/net/pterodactylus/sone/core/event/WebOfTrustDisappeared.kt new file mode 100644 index 0000000..63b4a1f --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/core/event/WebOfTrustDisappeared.kt @@ -0,0 +1,23 @@ +/** + * Sone - WebOfTrustDisappeared.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.core.event + +/** + * Event that signals that the web of trust is not reachable. + */ +class WebOfTrustDisappeared diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustPinger.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustPinger.kt new file mode 100644 index 0000000..e090133 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustPinger.kt @@ -0,0 +1,50 @@ +/** + * Sone - WebOfTrustPinger.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.wot + +import com.google.common.eventbus.* +import net.pterodactylus.sone.core.event.* +import net.pterodactylus.sone.freenet.plugin.* +import java.util.concurrent.atomic.* + +/** + * [Runnable] that is scheduled via an [Executor][java.util.concurrent.Executor], + * checks whether the web of trust plugin can be communicated with, sends + * events if its status changes and reschedules itself. + */ +class WebOfTrustPinger(private val eventBus: EventBus, private val webOfTrustReacher: () -> Unit, private val reschedule: () -> Unit) : Runnable { + + private val lastState = AtomicBoolean(false) + + override fun run() { + try { + webOfTrustReacher() + if (!lastState.get()) { + eventBus.post(WebOfTrustAppeared()) + lastState.set(true) + } + } catch (e: PluginException) { + if (lastState.get()) { + eventBus.post(WebOfTrustDisappeared()) + lastState.set(false) + } + } + reschedule() + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustPingerTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustPingerTest.kt new file mode 100644 index 0000000..894b9ef --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustPingerTest.kt @@ -0,0 +1,117 @@ +/** + * Sone - WebOfTrustPinger.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.wot + +import com.google.common.eventbus.* +import net.pterodactylus.sone.core.event.* +import net.pterodactylus.sone.freenet.plugin.* +import net.pterodactylus.sone.utils.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import java.util.concurrent.atomic.* +import kotlin.test.* + +/** + * Unit test for [WebOfTrustPinger]. + */ +class WebOfTrustPingerTest { + + private val eventBus = EventBus() + private val webOfTrustReachable = AtomicBoolean() + private val webOfTrustReacher: () -> Unit = { webOfTrustReachable.get().onFalse { throw PluginException() } } + private val rescheduled = AtomicBoolean() + private val reschedule: () -> Unit = { rescheduled.set(true) } + private val pinger = WebOfTrustPinger(eventBus, webOfTrustReacher, reschedule) + + @Test + fun `pinger sends wot appeared event when run first and wot is reachable`() { + webOfTrustReachable.set(true) + val appearedReceived = AtomicBoolean() + eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) }) + pinger() + assertThat(appearedReceived.get(), equalTo(true)) + } + + @Test + fun `pinger reschedules when wot is reachable`() { + webOfTrustReachable.set(true) + pinger() + assertThat(rescheduled.get(), equalTo(true)) + } + + @Test + fun `pinger sends wot disappeared event when run first and wot is not reachable`() { + val appearedReceived = AtomicBoolean() + eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) }) + pinger() + assertThat(appearedReceived.get(), equalTo(false)) + } + + @Test + fun `pinger reschedules when wot is not reachable`() { + pinger() + assertThat(rescheduled.get(), equalTo(true)) + } + + @Test + fun `pinger sends wot disappeared event when run twice and wot is not reachable on second call`() { + val disappearedReceived = AtomicBoolean() + eventBus.register(WebOfTrustDisappearedCatcher { disappearedReceived.set(true) }) + webOfTrustReachable.set(true) + pinger() + webOfTrustReachable.set(false) + pinger() + assertThat(disappearedReceived.get(), equalTo(true)) + } + + @Test + fun `pinger sends wot appeared event only once`() { + webOfTrustReachable.set(true) + val appearedReceived = AtomicBoolean() + eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) }) + pinger() + appearedReceived.set(false) + pinger() + assertThat(appearedReceived.get(), equalTo(false)) + } + + @Test + fun `pinger sends wot disappeared event only once`() { + val disappearedReceived = AtomicBoolean() + eventBus.register(WebOfTrustDisappearedCatcher { disappearedReceived.set(true) }) + pinger() + disappearedReceived.set(false) + pinger() + assertThat(disappearedReceived.get(), equalTo(false)) + } + +} + +private class WebOfTrustAppearedCatcher(private val received: () -> Unit) { + @Subscribe + fun webOfTrustAppeared(webOfTrustAppeared: WebOfTrustAppeared) { + received() + } +} + +private class WebOfTrustDisappearedCatcher(private val received: () -> Unit) { + @Subscribe + fun webOfTrustDisappeared(webOfTrustDisappeared: WebOfTrustDisappeared) { + received() + } +}