2 * Sone - WebOfTrustPingerTest.kt - Copyright © 2019 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.wot
20 import com.google.common.eventbus.*
21 import net.pterodactylus.sone.core.event.*
22 import net.pterodactylus.sone.freenet.plugin.*
23 import net.pterodactylus.sone.utils.*
24 import org.hamcrest.MatcherAssert.*
25 import org.hamcrest.Matchers.*
26 import java.util.concurrent.atomic.*
27 import java.util.function.*
31 * Unit test for [WebOfTrustPinger].
33 class WebOfTrustPingerTest {
35 private val eventBus = EventBus()
36 private val webOfTrustReachable = AtomicBoolean()
37 private val webOfTrustReacher = Runnable { webOfTrustReachable.get().onFalse { throw PluginException() } }
38 private val rescheduled = AtomicBoolean()
39 private val reschedule: Consumer<Runnable> = Consumer { if (it == pinger) rescheduled.set(true) }
40 private val pinger = WebOfTrustPinger(eventBus, webOfTrustReacher, reschedule)
43 fun `pinger sends wot appeared event when run first and wot is reachable`() {
44 webOfTrustReachable.set(true)
45 val appearedReceived = AtomicBoolean()
46 eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) })
48 assertThat(appearedReceived.get(), equalTo(true))
52 fun `pinger reschedules when wot is reachable`() {
53 webOfTrustReachable.set(true)
55 assertThat(rescheduled.get(), equalTo(true))
59 fun `pinger sends wot disappeared event when run first and wot is not reachable`() {
60 val appearedReceived = AtomicBoolean()
61 eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) })
63 assertThat(appearedReceived.get(), equalTo(false))
67 fun `pinger reschedules when wot is not reachable`() {
69 assertThat(rescheduled.get(), equalTo(true))
73 fun `pinger sends wot disappeared event when run twice and wot is not reachable on second call`() {
74 val disappearedReceived = AtomicBoolean()
75 eventBus.register(WebOfTrustDisappearedCatcher { disappearedReceived.set(true) })
76 webOfTrustReachable.set(true)
78 webOfTrustReachable.set(false)
80 assertThat(disappearedReceived.get(), equalTo(true))
84 fun `pinger sends wot appeared event only once`() {
85 webOfTrustReachable.set(true)
86 val appearedReceived = AtomicBoolean()
87 eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) })
89 appearedReceived.set(false)
91 assertThat(appearedReceived.get(), equalTo(false))
95 fun `pinger sends wot disappeared event only once`() {
96 val disappearedReceived = AtomicBoolean()
97 eventBus.register(WebOfTrustDisappearedCatcher { disappearedReceived.set(true) })
99 disappearedReceived.set(false)
101 assertThat(disappearedReceived.get(), equalTo(false))
106 private class WebOfTrustAppearedCatcher(private val received: () -> Unit) {
108 fun webOfTrustAppeared(@Suppress("UNUSED_PARAMETER") webOfTrustAppeared: WebOfTrustAppeared) {
113 private class WebOfTrustDisappearedCatcher(private val received: () -> Unit) {
115 fun webOfTrustDisappeared(@Suppress("UNUSED_PARAMETER") webOfTrustDisappeared: WebOfTrustDisappeared) {